极光Push推送消息

pom.xml引入极光的SDK:

看自己需要引入那个版本的SDK。

<dependency>
    <groupId>cn.jpush.api</groupId>
    <artifactId>jpush-client</artifactId>
    <version>3.3.4</version>
</dependency>
<dependency>
        <groupId>cn.jpush.api</groupId>
        <artifactId>jiguang-common</artifactId>
        <version>1.1.1</version>
    </dependency>
    <dependency>
        <groupId>io.netty</groupId>
        <artifactId>netty-all</artifactId>
        <version>4.1.6.Final</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.3</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.7</version>
    </dependency>

核心代码:

public class PushService {

   private static  final Logger LOG= LoggerFactory.getLogger(PushService.class);
   private  static  final  String masterSecret="自己去申请一个";
   private static final String appKey="自己去申请一个";



   /**
    * 所有用户
    * @param alert
    * @return
    */
   public static PushPayload buildPushObject_all_all_alert(String alert) {
    return PushPayload.alertAll(alert);
   }

   /**
    * 所有平台,推送目标为registrationID
    * @param alert 内容
    * @param extras 附加选项
    * @param registrationID
    * @return
    */
   private static PushPayload buildPushObject_all_alias_alert(String alert, Map<String,String> extras, List<String> registrationID) {
      return PushPayload.newBuilder()
            .setPlatform(Platform.android_ios())//构建平台:所有平台:安卓、ios、WindowsPhone
            .setAudience(Audience.registrationId(registrationID))//构建推送目标
            .setNotification( Notification
                  .newBuilder()
                  .setAlert(alert)
                  .addPlatformNotification(
                        AndroidNotification.newBuilder().addExtras(extras).build())
                  .addPlatformNotification(
                        IosNotification.newBuilder().addExtras(extras).build())
                  .build()
            )
            .setOptions(Options.newBuilder().setApnsProduction(true).build())//是否是正式环境,默认开发环境
            .build();
   }

   /**
    *
    * @param content 消息內容
    * @param extras 附加选项
    * @param registrationID
    */
   public static void SendWithRegistrationID(String content,Map<String,String> extras, List<String> registrationID) {
//    ClientConfig clientConfig = ClientConfig.getInstance();
//    JPushClient jpushClient = new JPushClient(masterSecret,appKey, null, clientConfig);
      JPushClient jpushClient = new JPushClient(masterSecret,appKey);
      try {
         PushPayload payload=buildPushObject_all_alias_alert(content,extras,registrationID);
         payload.resetOptionsTimeToLive(86400*2L);//离线状态下保存时间
         PushResult result = jpushClient.sendPush(payload);
         //SDK版本如果使用3.2.15以上的话需要在请求结束后close进程:jpushClient.close();
      } catch (APIConnectionException e) {
         e.printStackTrace();
         LOG.error("Connection error. Should retry later. {}", e);
      } catch (APIRequestException e) {
         e.printStackTrace();
      }
   }

   //模拟调用 
   public static void main(String[] args) {
      List<String> id=new ArrayList<>();//极光的唯一RegistrationID
      id.add("11111111");
      id.add("22222222");
      SendWithRegistrationID("测试发送",new HashMap<>(),id);
   }

需要注意:

如果使用 NettyHttpClient(v3.2.15 版本新增),需要在响应返回后手动调用一下 NettyHttpClient 中的 close 方法,否则进程不会退出。代码示例:

...
try {
    PushResult result = jpushClient.sendPush(payload);
   
    // 请求结束后,调用 NettyHttpClient 中的 close 方法,否则进程不会退出。
    jpushClient.close();
} catch(InterruptedException e) {
    e.printStackTrace();
}

官方文档:https://github.com/jpush/jpush-api-java-client

转载于:https://my.oschina.net/648885471/blog/1801495

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值