didReceiveRemoteNotification或ReceivedRemoteNotification永远不会触发

时间:2016-01-03 19:56:12

标签: ios swift swift2 apple-push-notifications

由于某种原因,didReceiveRemoteNotification或ReceivedRemoteNotification永远不会触发;当我不久前实现它时,它很好,至少当我在应用程序处于后台时测试它时。现在2周后,我的应用程序更加成熟,我回到了推送通知部分,只是发现在任何特定时刻事件都被解雇了。设备正在接收推送通知弹出窗口,当我点击它时,应用程序打开,但didFinishLaunchingWithOptions launchOptions为零。

是什么导致这种情况停止工作?

  • 启用推送通知
  • 启用后台模式(远程通知和后台获取)
didFinishLaunchingWithOptions

中的

if application.respondsToSelector("registerUserNotificationSettings:") {

        let setting = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil);
        application.registerUserNotificationSettings(setting);
        application.registerForRemoteNotifications();

    }

已添加以下内容:

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
    deviceTokenStr = DeviceTokenToString(deviceToken)
    print(deviceTokenStr)
}

func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
    print("Device token for push notifications: FAIL")
}

然后是以下两个函数:

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
    print("notification received")
}

func application(application: UIApplication, ReceivedRemoteNotification userInfo: [NSObject : AnyObject]) {
    print("notification received")
}

didRegisterForRemoteNotificationsWithDeviceToken 正在返回push的令牌,并在模拟器中调用 didFailToRegisterForRemoteNotificationsWithError ,到目前为止所有运行正常。

当应用程序在后台,并指示服务器发送推送时,它会到达手机,然后单击它,将打开应用程序。

但是didReceiveRemoteNotification或ReceivedRemoteNotification内部都没有被解雇。

我查看了很多stackoverflow帖子,但是他们似乎都没有遇到同样的问题,或者解决方案,在不同帖子中尝试各种组合之后,我似乎无法恢复工作。 / p>

apn payload是基本的“aps”:

{
   "alert":"string message", 
   "sound":"default"
}

如果有人可以帮助我,找出它停止的原因,那会很棒。

提前致谢。

修改 添加内容到函数

当应用程序运行时,它只会在日志中显示推送令牌的64个字符,不会显示任何错误。

在设备控制台日志中,它会按时显示推送通知以及应用程序在后台的时间:

Jan  3 21:20:29 Marcs-iPhone SpringBoard[58] <Warning>: SBLockScreenNotificationListController: Attempting to remove a bulletin I don't have

当应用程序是活动应用程序时,控制台日志中没有任何反应。

编辑2: 日志从APN Tester添加

2016-01-03 21:42:59 +0000: Connected to server gateway.sandbox.push.apple.com 
2016-01-03 21:42:59 +0000: Set SSL connection 
2016-01-03 21:42:59 +0000: Set peer domain name gateway.sandbox.push.apple.com 
2016-01-03 21:42:59 +0000: Keychain Opened  
2016-01-03 21:42:59 +0000: Certificate  data  for Apple Push Services: com.xxxxxxx.xxxxxxxx initialized successfully 
2016-01-03 21:42:59 +0000: Sec Identity created 
2016-01-03 21:42:59 +0000: Client certificate created 
2016-01-03 21:42:59 +0000: Connected 
2016-01-03 21:42:59 +0000: Token: <xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx> 
2016-01-03 21:42:59 +0000: Written 92 bytes sending data to gateway.sandbox.push.apple.com:2195 
2016-01-03 21:42:59 +0000: Disconnected from server gateway.sandbox.push.apple.com:2195

执行发送的apn时,消息确实到达了手机,虽然设备控制台日志中没有显示,也没有调试输出

1 个答案:

答案 0 :(得分:1)

对其进行排序,并在此处回答以防其他人遇到同样的问题。

经过长时间的挖掘后,代码功能正常并且正常工作,我在项目中有一个解决方法来清除_handleNonLaunchSpecificActions:forScene异常问题。

extension UIApplication {
    func _handleNonLaunchSpecificActions(arg1: AnyObject, forScene arg2: AnyObject, withTransitionContext arg3: AnyObject, completion completionHandler: () -> Void) {
        //catching handleNonLaunchSpecificActions:forScene exception on app close
    }
}

删除此解决方法后,通知再次像魅力一样工作。