diff --git a/src/ios/FirebasePlugin.m b/src/ios/FirebasePlugin.m index a049429ed..9d4634a14 100755 --- a/src/ios/FirebasePlugin.m +++ b/src/ios/FirebasePlugin.m @@ -204,15 +204,25 @@ - (void)getId:(CDVInvokedUrlCommand *)command { } - (void)getToken:(CDVInvokedUrlCommand *)command { - [[FIRMessaging messaging] tokenWithCompletion:^(NSString *token, NSError *error) { - @try { - [self handleStringResultWithPotentialError:error command:command result:token]; - }@catch (NSException *exception) { - [self handlePluginExceptionWithContext:exception :command]; - } + [self _getToken:^(NSString *token, NSError *error) { + [self handleStringResultWithPotentialError:error command:command result:token]; }]; } +-(void)_getToken:(void (^)(NSString *token, NSError *error))completeBlock { + @try { + [[FIRMessaging messaging] tokenWithCompletion:^(NSString *token, NSError *error) { + @try { + completeBlock(token, error); + }@catch (NSException *exception) { + [self handlePluginExceptionWithoutContext:exception]; + } + }]; + }@catch (NSException *exception) { + [self handlePluginExceptionWithoutContext:exception]; + } +} + - (void)getAPNSToken:(CDVInvokedUrlCommand *)command { CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:[self getAPNSToken]]; [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; @@ -430,22 +440,11 @@ - (void)onMessageReceived:(CDVInvokedUrlCommand *)command { - (void)onTokenRefresh:(CDVInvokedUrlCommand *)command { self.tokenRefreshCallbackId = command.callbackId; - @try { - [[FIRInstallations installations] authTokenForcingRefresh:true - completion:^(FIRInstallationsAuthTokenResult *result, NSError *error) { - @try { - if (error != nil) { - [self sendPluginErrorWithError:error command:command]; - }else{ - [self sendPluginStringResult:[result authToken] command:command callbackId:command.callbackId]; - } - }@catch (NSException *exception) { - [self handlePluginExceptionWithContext:exception :command]; - } - }]; - }@catch (NSException *exception) { - [self handlePluginExceptionWithContext:exception :command]; - } + [self _getToken:^(NSString *token, NSError *error) { + if(error == nil && token != nil){ + [self sendToken:token]; + } + }]; } - (void)onApnsTokenReceived:(CDVInvokedUrlCommand *)command { diff --git a/www/firebase.js b/www/firebase.js index 20edac5d9..a8fff661e 100644 --- a/www/firebase.js +++ b/www/firebase.js @@ -34,10 +34,6 @@ exports._onInstallationIdChangeCallback = function(installationId){ **************/ // Notifications -exports.getId = function (success, error) { - exec(success, error, "FirebasePlugin", "getId", []); -}; - exports.getToken = function (success, error) { exec(success, error, "FirebasePlugin", "getToken", []); }; @@ -443,6 +439,11 @@ exports.functionsHttpsCallable = function (name, args, success, error) { exec(success, error, "FirebasePlugin", "functionsHttpsCallable", [name, args]); }; +// Installations +exports.getId = function (success, error) { + exec(success, error, "FirebasePlugin", "getId", []); +}; + exports.getInstallationId = function (success, error) { exec(success, error, "FirebasePlugin", "getInstallationId", []); };