【问题标题】:didReceiveRemoteNotification or ReceivedRemoteNotification never firesdidReceiveRemoteNotification 或 ReceivedRemoteNotification 永远不会触发
【发布时间】:2016-01-03 19:56:12
【问题描述】:

由于某种原因 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 ,到目前为止一切正常。

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

但 didReceiveRemoteNotification 或 ReceivedRemoteNotification 内部没有任何内容被触发。

我查看了许多 stackoverflow 帖子,但似乎都没有相同的问题,或者解决方案,在尝试了不同帖子中的各种组合之后,我似乎无法让它恢复工作。

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

【问题讨论】:

  • 你能展示你认为不起作用的方法的实现吗? didRegisterForRemoteNotificationsWithDeviceToken 和 didFailToRegisterForRemoteNotificationsWithError 似乎不准确,因为它们缺少参数并且大写与声明不匹配。
  • 现在我只在 didReceiveRemoteNotification 和 ReceivedRemoteNotification 中做 print("notification recieved") 在 didRegisterForRemoteNotificationsWithDeviceToken 我必须遵循:let deviceTokenStr = DeviceTokenToString(deviceToken) print(deviceTokenStr) 其中 DeviceTokenToString 是一个小函数,可以剥离 和空格
  • 请在您的问题中包含完整的实现。另外,看看控制台,不是调试控制台,而是设备控制台。它可能会给您一些可以提供帮助的错误。
  • 问题中包含的控制台日志行

标签: ios swift swift2 apple-push-notifications


【解决方案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
    }
}

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-06
    • 2014-05-15
    • 2021-05-22
    • 2015-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多