ios10, Swift 3 and Firebase Push Notifications (FCM)

后端 未结 3 1515
温柔的废话
温柔的废话 2020-12-17 03:20

I am struggling to display my push notifications that I am sending to my device from the FCM notification console. I can see the device is receiving the notification becaus

相关标签:
3条回答
  • 2020-12-17 04:06

    You need to move

     application.registerForRemoteNotifications()
    

    it should not be inside the else.

    // [START register_for_notifications]
        if #available(iOS 10.0, *) {
            let authOptions : UNAuthorizationOptions = [.Alert, .Badge, .Sound]
            UNUserNotificationCenter.currentNotificationCenter().requestAuthorizationWithOptions(
                authOptions,
                completionHandler: {_,_ in })
    
            // For iOS 10 display notification (sent via APNS)
            UNUserNotificationCenter.currentNotificationCenter().delegate = self
            // 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()
    

    See following commit in Firebase

    0 讨论(0)
  • 2020-12-17 04:08

    Inside method didRegisterForRemoteNotificationsWithDeviceToken, add the following code:

    func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
        let tokenChars = UnsafePointer<CChar>(deviceToken.bytes)
        var tokenString = ""
    
        for i in 0..<deviceToken.length {
            tokenString += String(format: "%02.2hhx", arguments: [tokenChars[i]])
        }
    
        FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: FIRInstanceIDAPNSTokenType.Unknown)
    
        print("tokenString: \(tokenString)")
    }
    

    And do not forget to enable Push Notifications inside Capabilities.

    0 讨论(0)
  • 2020-12-17 04:18

    For me it was the missing entitlements file, so in the Capabilities of your project, you have to click fix issue. Xcode will setup the file for you.

    0 讨论(0)
提交回复
热议问题