簡體   English   中英

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

[英]Not receiving remote push notifications on iOS

我目前正在將我的應用程序從Objective-C重寫為Swift,並正在處理通知。 出於某種原因,該應用程序的Swift版本未收到Objective-C版本的任何遠程推送通知。

這是我用來在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()

我認為該應用程序已成功注冊,因為didRegisterForRemoteNotificationsWithDeviceToken方法被調用了。 但是,當我嘗試使用從該方法獲得的令牌發送測試通知時,沒有在設備上收到實際的通知。

這些方法也沒有被調用:

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

}

我究竟做錯了什么?

你說你沒有收到讓我們首先確保那些你沒有被調用之前提到的方法都是從這里正在添加extension AppDelegate: UNUserNotificationCenterDelegate (順便說一下,當你實現didReceive方法確保您調用completionHandler()在末尾)

執行此操作時:

application.registerForRemoteNotifications()

確保從主線程運行它,因為如果未從后台指定它,則可以調用它,否則可能會失敗。 你可以這樣做

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

我假設您以這種方式或類似的方式獲取設備令牌

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

最后實現此方法,以查看注冊設備時是否有任何錯誤

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

哦,順便說一句(以防萬一您錯過了它),請確保已在項目目標中啟用了推送通知。

確保在項目設置功能中啟用了推送通知。 如何啟用:轉到目標:->選擇項目->轉到功能->轉到推送通知。 在那里啟用它。

另一件事是證書。 在開發過程中,您不應該生產證書。

暫無
暫無

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

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