繁体   English   中英

Firebase刷新令牌

[英]Firebase Refresh Token

使用方法

[FIRInstanceID tokenWithAuthorizedEntity:scope:options:handler]

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

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

例:

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

        // Fetch the token or error
    }];

}

你可以这样做。

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

Swift版本(基于@ HeadOnn的回答 ):

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
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM