16

我正在嘗試使用FCM進行通知。
<FIRInstanceID/WARNING>未能取得APNS令牌錯誤Domain=com.firebase.iid Code=1001 "(null)"發生,所以我無法獲取通知。 什麼問題?Firebase(FCM)無法獲取APNS令牌Error Domain = com.firebase.iid代碼= 1001

在控制檯,
無法抓取APNS令牌Error Domain=com.firebase.iid Code=1001 "(null)"

以下是我對Appdelegate

import UIKit 
import CoreData 
import Alamofire 
import Firebase 
import FirebaseInstanceID 
import FirebaseMessaging 

@UIApplicationMain 
class AppDelegate: UIResponder, UIApplicationDelegate { 

    var window: UIWindow? 

    var badgeCount : Int = 0; 

    enum BasicValidity : String { 
     case Success = "basicInfo" 
     case Fail = "OauthAuthentificationError" 
    } 

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 
     // Override point for customization after application launch. 


     let uns: UIUserNotificationSettings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil) 
     application.registerUserNotificationSettings(uns) 
     application.registerForRemoteNotifications() 

     FIRApp.configure() 

     NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.tokenRefreshNotification), name: kFIRInstanceIDTokenRefreshNotification, object: nil) 

     if let token = FIRInstanceID.instanceID().token() { 
      sendTokenToServer(token) 
      print("token is < \(token) >:") 
     } 

     return true 
    } 

    func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData){ 


     print("didRegisterForRemoteNotificationsWithDeviceToken()") 

     // if FirebaseAppDelegateProxyEnabled === NO: 
     FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: .Sandbox) 

     print("APNS: <\(deviceToken)>") 
    } 

    func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError){ 

     print("Registration for remote notification failed with error: \(error.localizedDescription)") 
    } 

    func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]){ 


     print("didReceiveRemoteNotification()") 

     //if FirebaseAppDelegateProxyEnabled === NO: 
     FIRMessaging.messaging().appDidReceiveMessage(userInfo) 

     // handler(.NoData) 

    } 

    // [START refresh_token] 
    func tokenRefreshNotification(notification: NSNotification) { 
     print("tokenRefreshNotification()") 
     if let token = FIRInstanceID.instanceID().token() { 
      print("InstanceID token: \(token)") 
      sendTokenToServer(token) 
      FIRMessaging.messaging().subscribeToTopic("/topics/global") 
      print("Subscribed to: /topics/global") 
     } 
     connectToFcm() 
    } 
    // [END refresh_token] 

    func sendTokenToServer(currentToken: String) { 
     print("sendTokenToServer() Token: \(currentToken)") 
     // Send token to server ONLY IF NECESSARY 
    } 

    // [START connect_to_fcm] 
    func connectToFcm() { 
     FIRMessaging.messaging().connectWithCompletion { (error) in 
      if error != nil { 
       print("Unable to connect with FCM. \(error!)") 
      } else { 
       print("Connected to FCM.") 
      } 
     } 
    } 
    // [END connect_to_fcm] 

    func applicationWillResignActive(application: UIApplication) { 
     // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 
     // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. 
    } 

    // [START disconnect_from_fcm] 
    func applicationDidEnterBackground(application: UIApplication) { 
     // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
     // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 

     FIRMessaging.messaging().disconnect() 
     print("Disconnected from FCM.") 
    } 

    func applicationWillEnterForeground(application: UIApplication) { 
     // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. 
    } 

    func applicationDidBecomeActive(application: UIApplication) { 
     // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 

     // UIApplication.sharedApplication().applicationIconBadgeNumber = 0 
     connectToFcm() 

    } 

    func applicationWillTerminate(application: UIApplication) { 
     // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 
     // Saves changes in the application's managed object context before the application terminates. 
     self.saveContext() 
    } 
  • 碼我可以從火力地堡康索爾得到通知,如果我用束髮的通知ID。但我不能得到如果我們的服務器發送通知到具有令牌的特定設備。
+0

@Frank van Puffelen你能幫我嗎? – kimpro

+0

鑑於當您從控制檯發送到應用程序(套件ID)時收到通知,這意味着您的應用程序具有有效的IID令牌,但我不確定那是您用來發送給的應用程序。如果您卸載應用程序並重新安裝,您可能會注意到打印了多個令牌,只有最後一個有效。我也會嘗試發送到您正在使用的主題,因爲這也會確認設備具有有效的令牌,但您可能會使用錯誤的令牌。 –

+0

@kimpro我有同樣的問題 - 你能找到如何解決它的方式?謝謝! – Dmitry

回答

4

對於我來說,我嘗試以下thigs,使其工作:

  • 重新啓用Capabilities->推送通知,鑰匙扣共享和背景modes->遠程通知
  • 重新安裝應用程序(此將生成新的刷新標記,對於後續運行它將保持不變,所以每次運行應用程序時都不會打印)。
  • 確保我在火力控制檯 - 上傳正確.p12文件>項目設置 - >雲消息
  • 在複覈蘋果開發者中心的provisioning profile(我不得不重新激活的iOS開發者的provisioning profile)

您可能仍然會收到警告,但如果您嘗試使用刷新令牌從Firebase控制檯發送通知,則該操作將起作用。

+0

where i可以找到刷新token.tokenRefresh通知不被稱爲 –

+0

@KrutarthPatel不確定你的問題,但如果你的tokenRefreshNotification沒有被調用,你可能需要卸載並重新安裝應用程序。如果您將調試點放在該函數中,那麼在配置Firebase之後,它會被調用。 –

0

聽起來像您用來提供遠程通知的服務器可能有您的FCM令牌的舊記錄。的谷歌FCM文檔狀態,所述FCM令牌可以旋轉(改變)時:

  • 該應用新的設備
  • 用戶卸載上還原/重新安裝該應用
  • 用戶清除應用程序數據。

他們引用它的好習慣使用委託回調方法更新此令牌的服務器的記錄:

func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) { 
    // Send the token to your server here 
} 

這種方法並沒有爲我工作,所以我不得不手動的更新每次應用程序啓動(延遲20-25秒之後,因爲輪換並不總是即時的)。你可以這樣做:

let token = Messaging.messaging().fcmToken // Send this to your server 

這些變化我還是得到警告控制檯日誌消息後,但推送通知每次都很好地工作。

相關問題