簡體   English   中英

UNUserNotificationCenter didReceive 沒有被調用

[英]UNUserNotificationCenter didReceive not being called

當我發送推送通知,並且應用程序在前台時會調用willPresent didReceive永遠不會被調用。 當應用程序在后台並收到推送通知時,會顯示警報,但應用程序從不調用didReceivewillPresent

在 Project > Capabilities 中,我檢查了 Background Modes Location updatesRemote notifications 位置更新用於不相關的代碼。

我還啟用了推送通知。 這工作正常,因為它們正在被接收。

我已經在我的AppDelegate實現了UNUserNotificationCenter通知內容,見下文:

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,斯威夫特 3。

我發現我需要在獲得許可后設置委托。

否則,當您第一次使用該應用程序時,您會在獲得權限之前注冊委托,並且不會觸發 didReceive 函數 - 這僅在您第一次運行應用程序時發生,在殺死並重新啟動之后,它被調用得很好。

見下文:

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

didReceive在您收到通知時不會執行,它會在用戶對收到的通知執行操作時執行

來自https://developer.apple.com/documentation/usernotifications/unusernotificationcenterdelegate 上的 Apple 文檔

調用以讓您的應用知道用戶為給定通知選擇了哪個操作。

這意味着當用戶對收到的通知執行操作(推送通知、向下滑動等)時將調用此方法

如果您點擊通知橫幅,將調用willPresent方法。

如果您希望在后台處理推送通知而不必點擊橫幅,那么您應該將 {"content-available":1} 附加到您的通知負載。 這種類型的推送通知稱為靜默推送。 不會像普通推送那樣創建通知橫幅/警報,您必須自己創建橫幅。

當您收到帶有“內容可用”的推送通知時,將在應用程序處於后台時調用didReceive委托方法。

注意您還必須在 plist 文件的UIBackgroundModes鍵中添加“遠程通知”。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM