javaPNS处理APNS返回结果

 javaPNS处理APNS返回结果
 当我们通过javaPNS发送通知给APNS后,如果token失效或者程序已被目标用户删除等原因可能导致发送失败。通过javaPNS返回的结果我们可以处理保存的设备token来防止下次发送给无效的设备。
 APNS有两种不同的错误报告系统,这两种系统工作方式不同而且报告的错误种类也不同。第一种是 — error-response packets —当我们通过javaPNS发送通知后立即可以从PushNotification中获取
 到,另一种是Feedback Service,这个需要我们定期从Apple那里获取失效的设备信息列表。我们不能只依靠一种方式来处理失效设备信息,必须两种方式结合使用。
 
 第一种方式  从返回的PushNotification中获取错误信息。
 通过isSuccessful()我们可以获知发送是否成功,如果没有成功则通过调用pushedNotification.getException()来获取失败的原因
 通过(exception.printStackTrace())打印异常。这里需要注意:此异常并不会抛出,而是存在于PushedNotification中。
 如果错误是呗Apple服务器报告的error-response packet,我们可以通过pushedNotification.getResponse().方法获取到。
 下面是具体的操作步骤:
 import javapns.*;
 import javapns.notification.*;
        
 public class PushTest {
       public static void main(String[] args) {
               String[] devices = {"MyToken111111111111111111111111111111111111111111111111111111111", 
                                   "MyToken222222222222222222222222222222222222222222222222222222222"}; 
               try {
                        List<PushedNotification> notifications = Push.alert("Hello World!", 
                                                        "keystore.p12", "keystore_password", false, devices); 
                        for (PushedNotification notification : notifications) {
                                if (notification.isSuccessful()) {
                                        /* Apple accepted the notification and should deliver it */  
                                        System.out.println("Push notification sent successfully to: " +
                                                                        notification.getDevice().getToken());
                                        /* Still need to query the Feedback Service regularly */  
                                } else {
                                        String invalidToken = notification.getDevice().getToken();
                                        /* Add code here to remove invalidToken from your database */  
        
                                        /* Find out more about what the problem was */  
                                        Exception theProblem = notification.getException();
                                        theProblem.printStackTrace();
        
                                        /* If the problem was an error-response packet returned by Apple, get it */  
                                        ResponsePacket theErrorResponse = notification.getResponse();
                                        if (theErrorResponse != null) {
                                                System.out.println(theErrorResponse.getMessage());
                                        }
                                }
                        }
                        
                } catch (KeystoreException e) {
                        /* A critical problem occurred while trying to use your keystore */  
                        e.printStackTrace();
                        
                } catch (CommunicationException e) {
                        /* A critical communication error occurred while trying to contact Apple servers */  
                        e.printStackTrace();
                }
        }
 }
 

 第二种方式 通过Feedback Service方式
 javaPNS为我们提供了通过Feedback Service获取失效设备列表的一切。首先来看代码:
  import javapns.*;
        
 public class FeedbackTest {
 
       public static void main(String[] args) {
        
                List<Device> inactiveDevices = Push.feedback("keystore.p12", "keystore_password", false);
                /* remove inactive devices from your own list of devices */ 
    }
 }

 关于feedback的参数也像之前Push其他方法的参数一样。我们需要定期调用此方法来获取失效的设备并做相应的处理。关于feedback service还有几点小知识:并不能
 通过 经过多少次信息推送失败或者从设备被标记为不活跃(inactive)状态多久 这些信息来计算此token是否被加入到feedback service中,因为Apple有非常多的
 参数来决定是否将某个设备放入到feedback Service中。如果一个设备不能推送过去通知,可能是网络原因,设备故障或者配置错误等等其他原因,这些原因
 都可能导致设备暂时的不活跃(inactive),但是这些都是暂时的。   另外如果程序被删除了,那么该程序会直接被放到feedback service中,但是这有一个前提:
 就是设备中有其他的软件可以与APNS交互,这样它可以通知Apple Server某个程序被删除了,如果你的程序是该iphone里最后一个可以与APNS交互的程序,那么当你的
 程序被删除时,没有办法通知Apple server了,那么从feedback service中就无法获取到该设备了。
 
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值