Correct way to retrieve token for FCM - iOS 10 Swift 3

前端 未结 15 2015
日久生厌
日久生厌 2021-01-31 02:42

i had implement Firebase with FirebaseAuth/FCM etc and did sent notification successfully through Firebase Console.

However i would need to push the notification from m

相关标签:
15条回答
  • 2021-01-31 03:03

    First of all, you should import all required libs

    import Firebase
    import UserNotifications
    

    later in AppDelegate.swift call next function

    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        Messaging.messaging().apnsToken = deviceToken
    
        InstanceID.instanceID().instanceID { (result, error) in
            if let error = error {
                print("Error fetching remote instance ID: \(error)")
            } else if let result = result {
                print("Remote instance ID token: \(result.token)")
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-31 03:03

    confirm to MessagingDelegate protocol.

    then you can add below delegate method and get the Firebase Token. (documentation https://firebase.google.com/docs/cloud-messaging/ios/client )

    func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
            // send to remote server
            InstanceID.instanceID().instanceID { result, error in
                if let error = error {
                    print("Error fetching remote instance ID: \(error)")
                } else if let result = result {
                    print("Remote instance ID token: \(result.token)")
                }
            }
        }
    
    0 讨论(0)
  • 2021-01-31 03:04

    This question is old but still if someone want to use in Objective C.

    Latest Firebase: 6.27.0

    In iOS Objective C we can use like this

    [[FIRInstanceID instanceID] instanceIDWithHandler:^(FIRInstanceIDResult * _Nullable result,
                                                                NSError * _Nullable error) {
                if (error != nil) {
                    NSLog(@"Error : %@", error);
                } else {
                    token = result.token;
                }
                NSLog(@"Token %@", result.token);
               
            }];
    

    and to get Instance Id:

     [[FIRInstanceID instanceID] getIDWithHandler:^(NSString *identity, NSError *error) {
             if (error != nil) {
               NSLog(@"Error : %@", error);
             } else {
               NSLog(@"instance ID: %@", identity);
             }
             NSLog(@"IID %@", identity);
           }];
    
    0 讨论(0)
  • 2021-01-31 03:06

    Swift 3 + Firebase 4.0.4 :

    static var FirebaseToken : String? {
        return InstanceID.instanceID().token()
    }
    
    0 讨论(0)
  • 2021-01-31 03:07

    Swift 4

    Courtesy of: https://stackoverflow.com/a/50945350/1014164

    InstanceID.instanceID().instanceID { (result, error) in
        if let error = error {
            print("Error fetching remote instange ID: \(error)")
        } else if let result = result {
            print("Remote instance ID token: \(result.token)")
        }
    }
    
    0 讨论(0)
  • 2021-01-31 03:07
    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {        
        Messaging.messaging().apnsToken = deviceToken
    
        let deviceTokenString = deviceToken.reduce("") { $0 + String(format: "%02X", $1) }
    
        print("APNs device token: \(deviceTokenString)"
    }
    
    0 讨论(0)
提交回复
热议问题