简体   繁体   中英

iOS Push notification coming but code not called

In my project I have both the didReceiveRemoteNotification method as well as the UNUserNotificationCenter didReceive and willPresent methods. In a certain edge case I observed the push notification coming to the device but no methods were called. I suspect that the edge case is intermittent network connectivity. Is my assumption correct? How should I go about handling this?

Here is the body of the push itself:

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

Here is the didReceiveRemoteNotification method:

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)
    }
}

And here are the didReceive and willPresent methods:

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)
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM