[ios][messaging] fix #1286 - 'getToken resolves to null if called too early'

This commit is contained in:
Salakar 2018-07-20 16:28:07 +01:00
parent 321e4a2909
commit 66aa5ccb67
1 changed files with 16 additions and 1 deletions

View File

@ -108,7 +108,22 @@ didReceiveMessage:(nonnull FIRMessagingRemoteMessage *)remoteMessage {
// ** Start React Module methods **
RCT_EXPORT_METHOD(getToken:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
resolve([[FIRInstanceID instanceID] token]);
if (initialToken) {
resolve(initialToken);
} else if ([[FIRInstanceID instanceID] token]) {
resolve([[FIRInstanceID instanceID] token]);
} else {
NSString * senderId = [[FIRApp defaultApp] options].GCMSenderID;
[[FIRMessaging messaging] retrieveFCMTokenForSenderID:senderId completion:^(NSString * _Nullable FCMToken, NSError * _Nullable error) {
if (error) {
reject(@"messaging/fcm-token-error", @"Failed to retrieve FCM token.", error);
} else if (FCMToken) {
resolve(FCMToken);
} else {
resolve([NSNull null]);
}
}];
}
}
RCT_EXPORT_METHOD(requestPermission:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {