didReceiveRemoteNotification 或 ReceivedRemoteNotification 从不触发

didReceiveRemoteNotification or ReceivedRemoteNotification never fires

出于某种原因,didReceiveRemoteNotification 或 ReceivedRemoteNotification 从未触发;当我前一段时间实现它时,它很好,至少当我在应用程序处于后台时测试它时是这样。现在 2 周后,我的应用程序更加成熟,我回到了推送通知部分,却发现在特定时刻没有触发事件。设备正在接收推送通知弹出窗口,当我单击它时,应用程序打开,但 didFinishLaunchingWithOptions launchOptions 为 nil。

什么可能导致它停止工作?

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 返回推送令牌,在模拟器中它调用 didFailToRegisterForRemoteNotificationsWithError ,到目前为止一切正常。

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

但 didReceiveRemoteNotification 或 ReceivedRemoteNotification 中的任何内容都不会被触发。

我看了很多 Whosebug 帖子,但是 none 似乎有同样的问题,或者解决方案,在不同帖子中尝试了各种组合之后,我似乎无法得到它恢复工作了。

apn 负载是基本的 "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 测试器添加的日志

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 时,消息确实到达 phone,尽管设备控制台日志中没有显示任何内容,也没有调试输出

整理出来,如果其他人遇到同样的问题,会在这里回答。

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

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

删除此变通办法后,通知再次发挥作用。