Firebase Refresh Token

后端 未结 3 1855
孤独总比滥情好
孤独总比滥情好 2021-01-18 21:07

Using the method

[FIRInstanceID tokenWithAuthorizedEntity:scope:options:handler]

Im not quite sure what the parameters are calling for? What is

相关标签:
3条回答
  • 2021-01-18 21:56

    Version for Swift (based on @HeadOnn's answer):

    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data)
    {
        Messaging.messaging().setAPNSToken(deviceToken, type: .prod) // may be excess
    
        guard let plistPath = Bundle.main.path(forResource: "GoogleService-Info", ofType: "plist"),
            let options = FirebaseOptions(contentsOfFile: plistPath)
        else { return }
    
        InstanceID.instanceID().token(withAuthorizedEntity: options.gcmSenderID, 
                scope: InstanceIDScopeFirebaseMessaging,
                options: ["apns_token": deviceToken])
        { (token, error) in
            // handle token and error
        }
    }
    
    0 讨论(0)
  • 2021-01-18 21:57
    1. AUTHORIZED_ENTITY - Basically it asks for the google project id. It is numeric, and if you already had GCM integrated in your project before, it would be GCM_SENDER_ID (something like "568520103762"). Check your Google-info.plist to find it.
    2. SCOPE - kFIRInstanceIDScopeFirebaseMessaging
    3. OPTIONS - @{@"apns_token": deviceToken} (You will get DeviceToken in didRegisterForRemoteNotifications method)
    4. HANDLER - Catch token if you have received token or catch the error here. If token comes nil, then wait for token in "tokenRefreshNotification" method, which will be called automatically if the token is nil in [FIRInstanceID tokenWithAuthorizedEntity:scope:options:handler]

    Example:

     if (![[FIRInstanceID instanceID] token]) {
        [[FIRInstanceID instanceID] tokenWithAuthorizedEntity:_gcmSenderId scope:kFIRInstanceIDScopeFirebaseMessaging options:_registrationOptions handler:^(NSString * _Nullable token, NSError * _Nullable error) {
    
            // Fetch the token or error
        }];
    
    }
    
    0 讨论(0)
  • 2021-01-18 21:59

    You can do like this.

    [[FIRInstanceID instanceID] setAPNSToken:deviceToken type:FIRInstanceIDAPNSTokenTypeProd];
    
    [[FIRInstanceID instanceID] tokenWithAuthorizedEntity:gcmSenderID scope:kFIRInstanceIDTokenRefreshNotification options:nil handler:^(NSString * _Nullable token, NSError * _Nullable error) {
    
        NSLog(@"GCM Registration token = %@",token);
        NSLog(@"GCM Registration error = %@",error);        
    }];
    
    0 讨论(0)
提交回复
热议问题