Java集成极光推送

本文标题:Java集成极光推送

原始链接: https://shuibo.cn/jiguang-push.html

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。




 首先是jar,这里因为我项目中用的是Maven,需要jar的可自行去官网下载jar

 在Maven中的pom.xml文件中添加


	cn.jpush.api
	jpush-client
	3.2.17


	cn.jpush.api
	jiguang-common
	1.0.3

JPushExample .java

/**

极光推送
@author Bobby
*/
public class JPushExample {

/**
 * 全平台推送
 * @param parm
 */
public static void jpushAll(Map parm) {
       //创建JPushClient
       JPushClient jpushClient = new JPushClient(JPushConfig.MasterSecret, JPushConfig.AppKey);
       //创建option

       PushPayload payload = PushPayload.newBuilder()
            .setPlatform(Platform.all())  //所有平台的用户
            .setAudience(Audience.registrationId(parm.get("RegId")))//registrationId指定用户
            .setNotification(Notification.newBuilder()
                    .addPlatformNotification(IosNotification.newBuilder()
                            .setAlert(parm.get("msg"))
                            .setBadge(+1)
                            .setSound("happy")
                            .addExtras(parm)
                            .build())
                    .addPlatformNotification(AndroidNotification.newBuilder()
                            .addExtras(parm)
                            .setAlert(parm.get("msg"))
                            .build())
                    .build())
            .setOptions(Options.newBuilder().setApnsProduction(true).build())//指定开发环境
            .setMessage(Message.newBuilder().setMsgContent(parm.get("msg")).addExtras(parm).build())//自定义信息
            .build();
       try {
            PushResult pu = jpushClient.sendPush(payload);
        } catch (APIConnectionException e) {
            e.printStackTrace();
        } catch (APIRequestException e) {
            e.printStackTrace();
        }    
   }
   /**
    * android推送
    * @param parm
    */
   public static void jpushAndroid(Map parm) {
       //创建JPushClient
       JPushClient jpushClient = new JPushClient(JPushConfig.MasterSecret, JPushConfig.AppKey);
       //推送的关键,构造一个payload 
       PushPayload payload = PushPayload.newBuilder()
            .setPlatform(Platform.android())//指定android平台的用户
            .setAudience(Audience.all())//你项目中的所有用户
            .setNotification(Notification.android(parm.get("msg"), "XXX", parm))
            .setOptions(Options.newBuilder().setApnsProduction(false).build())
            //这里是指定开发环境,不用设置也没关系
            .setMessage(Message.content(parm.get("msg")))
            .build();

       try {
            PushResult pu = jpushClient.sendPush(payload);

        } catch (APIConnectionException e) {
            e.printStackTrace();
        } catch (APIRequestException e) {
            e.printStackTrace();
        }    
   }
   
   /**
    * 推送IOS
    * @param parm
    */
   public static  void jpushIOS(Map parm) {
       //创建JPushClient
       JPushClient jpushClient = new JPushClient(JPushConfig.MasterSecret, JPushConfig.AppKey);
       PushPayload payload = PushPayload.newBuilder()
            .setPlatform(Platform.ios())//ios平台的用户
            .setAudience(Audience.all())//所有用户
            .setNotification(Notification.newBuilder()
            .addPlatformNotification(IosNotification.newBuilder()
                            .setAlert(parm.get("msg"))
                            .setBadge(+1)
                            .setSound("happy")
                            .addExtras(parm)
                            .build())
                    .build())
            .setOptions(Options.newBuilder().setApnsProduction(false).build())
            .setMessage(Message.newBuilder().setMsgContent(parm.get("msg")).addExtras(parm).build())//自定义信息
            .build();

       try {
            PushResult pu = jpushClient.sendPush(payload);
        } catch (APIConnectionException e) {
            e.printStackTrace();
        } catch (APIRequestException e) {
            e.printStackTrace();
        }    
   }
}

Java集成极光推送_第1张图片

你可能感兴趣的:(Java集成极光推送)