Firebase刷新令牌

时间:2016-07-18 19:19:57

标签: ios objective-c firebase firebase-cloud-messaging

使用方法

[FIRInstanceID tokenWithAuthorizedEntity:scope:options:handler]

我不太确定参数的要求是什么?什么是授权实体和行动?我还要将苹果的APNS令牌传递给该方法吗?

3 个答案:

答案 0 :(得分:6)

  1. AUTHORIZED_ENTITY - 基本上它要求google项目ID。它是数字的,如果您之前已经在项目中集成了GCM,那么它将是GCM_SENDER_ID(类似于“568520103762”)。检查您的Google-info.plist以找到它。
  2. SCOPE - kFIRInstanceIDScopeFirebaseMessaging
  3. 选项 - @ {@“apns_token”:deviceToken}(您将在didRegisterForRemoteNotifications方法中获取DeviceToken)
  4. HANDLER - 如果您已收到令牌或在此处发现错误,请抓住令牌。如果token为nil,则在“tokenRefreshNotification”方法中等待令牌,如果[FIRInstanceID tokenWithAuthorizedEntity:scope:options:handler]中的令牌为nil,将自动调用该方法
  5. 示例:

     if (![[FIRInstanceID instanceID] token]) {
        [[FIRInstanceID instanceID] tokenWithAuthorizedEntity:_gcmSenderId scope:kFIRInstanceIDScopeFirebaseMessaging options:_registrationOptions handler:^(NSString * _Nullable token, NSError * _Nullable error) {
    
            // Fetch the token or error
        }];
    
    }
    

答案 1 :(得分:1)

你可以这样做。

[[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);        
}];

答案 2 :(得分:0)

Swift的版本(基于@HeadOnn的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
    }
}