简体   繁体   English

在iOS上未收到远程推送通知

[英]Not receiving remote push notifications on iOS

I'm currently rewriting my app from Objective-C to Swift and working on notifications. 我目前正在将我的应用程序从Objective-C重写为Swift,并正在处理通知。 For some reason Swift version of the app is not receiving any remote push notifications while Objective-C version does. 出于某种原因,该应用程序的Swift版本未收到Objective-C版本的任何远程推送通知。

Here's the code I'm using to register for notifications in AppDelegate: 这是我用来在AppDelegate中注册通知的代码:

    UNUserNotificationCenter.current().delegate = self
    let options: UNAuthorizationOptions = [.badge, .alert, .sound]
    UNUserNotificationCenter.current().requestAuthorization(options: options, completionHandler: { granted, error in
        print("access granted: \(granted)")
    })
    application.registerForRemoteNotifications()

I assume that the app successfully registers because didRegisterForRemoteNotificationsWithDeviceToken method gets called. 我认为该应用程序已成功注册,因为didRegisterForRemoteNotificationsWithDeviceToken方法被调用了。 But when I try to send the test notification using the token I got from that method, I don't get actual notification on device. 但是,当我尝试使用从该方法获得的令牌发送测试通知时,没有在设备上收到实际的通知。

Also none of these methods get called: 这些方法也没有被调用:

func pushRegistry(_ registry: PKPushRegistry, didReceiveIncomingPushWith payload: PKPushPayload, for type: PKPushType) {

}

func userNotificationCenter(_ center: UNUserNotificationCenter, openSettingsFor notification: UNNotification?) {

}

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

}

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

}

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {

}

func application(_ application: UIApplication, handleActionWithIdentifier identifier: String?, forRemoteNotification userInfo: [AnyHashable : Any], completionHandler: @escaping () -> Void) {

}

What am I doing wrong? 我究竟做错了什么?

you say that you are not receiving so let's first make sure that those methods you mention before that are not being called are comming from here extension AppDelegate: UNUserNotificationCenterDelegate (By the way when you do implement the didReceive method make sure you call the completionHandler() at the end) 你说你没有收到让我们首先确保那些你没有被调用之前提到的方法都是从这里正在添加extension AppDelegate: UNUserNotificationCenterDelegate (顺便说一下,当你实现didReceive方法确保您调用completionHandler()在末尾)

When you do this: 执行此操作时:

application.registerForRemoteNotifications() application.registerForRemoteNotifications()

Make sure to run it from the main thread , as it can be called if not specified from the background and that might fail. 确保从主线程运行它,因为如果未从后台指定它,则可以调用它,否则可能会失败。 You can do so by doing 你可以这样做

DispatchQueue.main.async { 
  UIApplication.shared.registerForRemoteNotifications()
}

I'll assume that you are getting the device token this way or something similar: 我假设您以这种方式或类似的方式获取设备令牌

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        // 1. Convert device token to string
        let tokenParts = deviceToken.map { data -> String in
            return String(format: "%02.2hhx", data)
        }
        let token = tokenParts.joined()
        // 2. Print device token to use for PNs payloads
        print("Device Token: \(token)")
}

Finally implement this method to see if there are any errors while registering the device 最后实现此方法,以查看注册设备时是否有任何错误

func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
        // 1. Print out error if PNs registration not successful
        print("Failed to register for remote notifications with error: \(error)")
 }

Oh by the way (just in case you have missed it) make sure you enabled in your project's target the push notification. 哦,顺便说一句(以防万一您错过了它),请确保已在项目目标中启用了推送通知。

Make sure you have enabled Push notification in your project setting capabilities. 确保在项目设置功能中启用了推送通知。 How to enable: go to target: -> select project -> go to capabilities -> go to Push Notification. 如何启用:转到目标:->选择项目->转到功能->转到推送通知。 enable it there. 在那里启用它。

Another thing is the certificate. 另一件事是证书。 While development you should not Production certificates. 在开发过程中,您不应该生产证书。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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