2

我在我的應用程序中包括谷歌firebase - 創建谷歌帳戶,創建谷歌應用程序,上傳APNS證書(.pem和在另一項服務工作),並從控制檯發送推送通知,我的應用程序不收到它。在火力地堡控制檯我看到設備的狀態完成,但概數爲「 - 」firebase iOS沒有收到推送通知

當然,我的最新條款型材和APNS證書

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 

    // Register for remote notifications 
    if #available(iOS 8.0, *) { 
     let settings: UIUserNotificationSettings = 
      UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil) 
     application.registerUserNotificationSettings(settings) 
     application.registerForRemoteNotifications() 
    } else { 
     // Fallback 
     let types: UIRemoteNotificationType = [.Alert, .Badge, .Sound] 
     application.registerForRemoteNotificationTypes(types) 
    } 


    FIRApp.configure() 
    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.tokenRefreshNotificaiton), 
                name: kFIRInstanceIDTokenRefreshNotification, object: nil) 
}  

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], 
       fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) { 

    print(userInfo) 
} 

func tokenRefreshNotificaiton(notification: NSNotification) { 
    if let refreshedToken = FIRInstanceID.instanceID().token() { 
     print("InstanceID token: \(refreshedToken)") 

     User.sharedUser.googleUID = refreshedToken 
    } 
} 


func application(application: UIApplication, handleActionWithIdentifier identifier: String?, forRemoteNotification userInfo: [NSObject : AnyObject], withResponseInfo responseInfo: [NSObject : AnyObject], completionHandler:() -> Void) { 

    print(userInfo) 
} 

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

    FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: FIRInstanceIDAPNSTokenType.Prod) 
} 

func application(application: UIApplication, handleActionWithIdentifier identifier: String?, forRemoteNotification userInfo: [NSObject : AnyObject], withResponseInfo responseInfo: [NSObject : AnyObject], completionHandler:() -> Void) { 

    print(userInfo) 
} 

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], 
       fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) { 


    print(userInfo) 
} 

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

    print("ошибка") 
    print(error) 
    print(error.description) 

} 
+0

發佈您的應用程序:didRegisterForRemoteNotificationsWithDeviceToken:和'application:didFailToRegisterForRemoteNotificationsWithError:'代碼。 – redent84

+0

編輯並添加代碼 –

+1

Frank,此代碼的用途是什麼?FIRInstanceID.instanceID()。setAPNSToken(deviceToken,type:FIRInstanceIDAPNSTokenType.Prod)'?它並未在Firebase示例中說明它是必需的。 – justColbs

回答

9

我看到的第一件事情是沒有的呼叫連接到FIRMessaging。嘗試添加到您的AppDelegate

func applicationDidBecomeActive(application: UIApplication) { 
    FIRMessaging.messaging().connectWithCompletion { error in 
     print(error) 
    } 
} 
+0

謝謝:)它的工作完美,我續簽我的證書並添加此代碼 –

5

使用以下代碼。

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool 
{ 
    if #available(iOS 10.0, *) 
    { 
     // For iOS 10 display notification (sent via APNS) 
     UNUserNotificationCenter.currentNotificationCenter().delegate = self 
     UNUserNotificationCenter.currentNotificationCenter().requestAuthorizationWithOptions([.Badge, .Sound, .Alert])   { (granted, error) in 
       if granted 
       { 
       //self.registerCategory() 
      } 
     } 
     // For iOS 10 data message (sent via FCM) 
     FIRMessaging.messaging().remoteMessageDelegate = self 
    } 
    else 
    { 
     let settings: UIUserNotificationSettings = UIUserNotificationSettings(forTypes: [.Alert,.Badge,.Sound], categories: nil) 
     application.registerUserNotificationSettings(settings) 
    }   
    application.registerForRemoteNotifications()   
    //Configuring Firebase 
    FIRApp.configure() 
    // Add observer for InstanceID token refresh callback. 
    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.tokenRefreshNotification), name: kFIRInstanceIDTokenRefreshNotification, object: nil)  
    return true 
} 

//Receive Remote Notification on Background 
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) 
{ 
    FIRMessaging.messaging().appDidReceiveMessage(userInfo) 
} 

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) 
{ 
    FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: FIRInstanceIDAPNSTokenType.Sandbox) 
    FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: FIRInstanceIDAPNSTokenType.Prod) 
} 

func tokenRefreshNotification(notification: NSNotification) 
{ 
    if let refreshedToken = FIRInstanceID.instanceID().token() 
    { 
      print("InstanceID token: \(refreshedToken)") 
    } 
    // Connect to FCM since connection may have failed when attempted before having a token. 
    connectToFcm() 
} 

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

func applicationDidBecomeActive(application: UIApplication) 
{    
    connectToFcm() 
}