基于netty4实现的苹果通知推送服务(APNs)Java客户端

简单说下实现苹果通知推送服务(APNs)客户端的一些要注意的地方:

因此发送者要缓存已经发送的Notification,最好设置Notification identifier为增长的整数序列,当收到Error response里,从缓存里取出比Error response的identifier要大的Notification,再次重新发送;

对于一台设备,APNs服务器只存储一条Notification,所以如果设备不在线,连续发送多条消息的话,后面的会覆盖前面的;Apple的文档里有提到可以设置Notification的Priority = 5,具体是什么意思不太明白。实际测试是当屏幕关闭,在省电时才会接收到的。如果是屏幕亮着,是不会接收到消息的。而且这种消息是没有声音提示的。貌似意义不大。

特点:

Notification发送者可以自己定义设置发送的Queue,自己灵活处理阻塞,超时等问题。

把Queue暴露给发送者,这严格来说是一个不好的设计。因为在shutdown里,,有可能别的线程还在put notification到queue里。

但是因为APNs协议本身,包括消息推送机制本身就是一个不完全靠谱的东东,考虑到发送者处理阻塞,消息积压的便利性,因此把Queue暴露给发送者。

代码地址:

https://github.com/hengyunabc/zpush

例子:

public class MainExample {public static void main(String[] args) throws InterruptedException {Environment environment = Environment.Product;String password = "123456";String keystore = "/home/hengyunabc/test/apptype/app_type_1/productAPNS.p12";PushManager pushManager = new PushManagerImpl(keystore, password, environment);//set a push queueBlockingQueue<Notification> queue = new LinkedBlockingQueue<Notification>(8192);pushManager.setQueue(queue );//waiting for SSL handshake successpushManager.start().sync();//build a notificationString token = "5f6aa01d8e3358949b7c25d461bb78ad740f4707462c7eafbebcf74fa5ddb387";Notification notification = new NotificationBuilder().setToken(token).setBadge(1).setPriority(5).setAlertBody("xxxxx").build();//put notification into the queuequeue.put(notification);TimeUnit.SECONDS.sleep(10);//get statistic infoStatistic statistic = pushManager.getStatistic();System.out.println(statistic);}}

版权声明:本文为博主原创文章,未经博主允许不得转载。

当你下定决心准备出发时,最困难的时刻就已经过去了。那么,出发吧。

基于netty4实现的苹果通知推送服务(APNs)Java客户端

相关文章:

你感兴趣的文章:

标签云: