iOS推送通知到来,但未调用代码

时间:2019-03-27 17:20:14

标签: swift apple-push-notifications

在我的项目中,我同时拥有didReceiveRemoteNotification方法和UNUserNotificationCenter didReceivewillPresent方法。在某种情况下,我观​​察到推送通知到达了设备,但是没有调用任何方法。我怀疑边缘情况是间歇性的网络连接。我的假设正确吗?我应该如何处理呢?

这是推送内容的主体:

{
    "aps": {
        "content-available": 1,
        "alert": "...",
        "badge": "1",
        "sound": "mailsent.wav"
    },
    ...
}

这是didReceiveRemoteNotification方法:

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    if let type = ... {
        switch type {
        case "...":
            self.handle(userinfo: userInfo, completion: { (value) -> (Void) in
                if (value == true) {
                    completionHandler(.newData)
                } else {
                    completionHandler(.failed)
                }
            })
            break
        default:
            completionHandler(.newData)
            break
        }
    } else {
        completionHandler(.noData)
    }
}

这是didReceivewillPresent方法:

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Swift.Void) {
    ...
    completionHandler()
}

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Swift.Void) {
    ...
    completionHandler(.alert)
}

1 个答案:

答案 0 :(得分:0)

您的代码确定,问题在于除非您在收到通知时该应用程序处于活动状态或用户轻按该应用程序,否则它将不会被调用,请在该应用程序运行时尝试发送通知,以便您可以对其进行正确检查。