首先是jar,这里因为我项目中用的是Maven,需要jar的可自行去官网下载jar
在Maven中的pom.xml文件中添加

<dependency>
    <groupId>cn.jpush.api</groupId>
    <artifactId>jpush-client</artifactId>
    <version>3.2.17</version>
</dependency>
<dependency>
    <groupId>cn.jpush.api</groupId>
    <artifactId>jiguang-common</artifactId>
    <version>1.0.3</version>
</dependency>

JPushExample .java

/**

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

/**
 * 全平台推送
 * @param parm
 */
public static void jpushAll(Map<String, String> 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<String, String> 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<String, String> 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();
        }    
   }
}
   
     
本文作者:      
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明出处!
扫码关注不迷路!

发表评论

电子邮件地址不会被公开。 必填项已用*标注