当我发送推送通知时,该应用程序位于前台willPresent中。从未调用didReceive。当应用程序在后台并且收到推送通知时,将显示警报,但应用程序从不调用didReceivewillPresent

在项目>功能中,我检查了背景模式Location updatesRemote notifications。位置更新适用于不相关的代码。

我还启用了推送通知。收到他们的来信后,它的工作正常。

我已经在UNUserNotificationCenter中实现了AppDelegate通知内容,请参见下文:

import UserNotifications
...

class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
...

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        ...
        registerForPushNotifications(application: application)
        ...
    }

    // MARK: - Push Notifications

    func registerForPushNotifications(application: UIApplication) {
        let notificationCenter = UNUserNotificationCenter.current()
        notificationCenter.delegate = self
        notificationCenter.requestAuthorization(options: [.badge, .sound, .alert], completionHandler: {(granted, error) in
            if (granted)
            {
                UIApplication.shared.registerForRemoteNotifications()
            }
            else{
                //Do stuff if unsuccessful...
                print("Unsuccessful in registering for push notifications")
            }
        })
    }

    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        //convert the deviceToken to a string
        let deviceTokenString = deviceToken.map { String(format: "%02.2hhx", arguments: [$0]) }.joined()

        let ud = UserDefaults.standard
        ud.set(deviceTokenString, forKey: "deviceToken")
    }

    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        //Handle the notification
        print("User Info = ",notification.request.content.userInfo)
        completionHandler([.alert, .badge, .sound])
    }

    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        //handle the notification
        print("Push notification: ",response.notification.request.content.userInfo)

        ... code to import the notification data into Core Data.

        completionHandler()
    }

...
}

有什么想法为什么不调用didReceive吗?

这是收到的推送通知:
[AnyHashable("aps"): {
    alert = "[name] has added you as a friend.";
    category = "NEWS_CATEGORY";
    "related_user_id" = 55;
    sound = default;
    type = "friend_request";
}]

iOS 10.1,Swift 3。

最佳答案

我发现在获得许可后,需要设置委托(delegate)。

否则,当您第一次在应用程序中吃午餐时,您会在获得权限之前注册委托(delegate),并且didReceive函数不会触发-这仅在您第一次运行应用程序时发生,杀死并重新启动后就可以了。

见下文:

 UNUserNotificationCenter.current().requestAuthorization(options: authOptions, completionHandler: { (granted, error) in
        if granted {
            // Register after we get the permissions.
            UNUserNotificationCenter.current().delegate = self
        }
    })

10-05 20:03