diff --git a/android/src/main/java/io/invertase/firebase/auth/RNFirebaseAuth.java b/android/src/main/java/io/invertase/firebase/auth/RNFirebaseAuth.java index 9ff09449..e6863c58 100644 --- a/android/src/main/java/io/invertase/firebase/auth/RNFirebaseAuth.java +++ b/android/src/main/java/io/invertase/firebase/auth/RNFirebaseAuth.java @@ -237,6 +237,11 @@ public class RNFirebaseAuth extends ReactContextBaseJavaModule { }); } + /** + * delete + * + * @param promise + */ @ReactMethod public void delete(final Promise promise) { FirebaseUser user = mAuth.getCurrentUser(); diff --git a/android/src/main/java/io/invertase/firebase/messaging/RNFirebaseMessaging.java b/android/src/main/java/io/invertase/firebase/messaging/RNFirebaseMessaging.java index 9122539e..dcce3e9d 100644 --- a/android/src/main/java/io/invertase/firebase/messaging/RNFirebaseMessaging.java +++ b/android/src/main/java/io/invertase/firebase/messaging/RNFirebaseMessaging.java @@ -10,6 +10,7 @@ import android.util.Log; import com.facebook.react.bridge.Arguments; import com.facebook.react.bridge.Callback; +import com.facebook.react.bridge.Promise; import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.bridge.ReactContext; import com.facebook.react.bridge.ReactContextBaseJavaModule; @@ -140,16 +141,13 @@ public class RNFirebaseMessaging extends ReactContextBaseJavaModule { } @ReactMethod - public void getToken(final Callback callback) { - + public void getToken(final Promise promise) { try { String token = FirebaseInstanceId.getInstance().getToken(); Log.d(TAG, "Firebase token: " + token); - callback.invoke(null, token); + promise.resolve(token); } catch (Exception e) { - WritableMap error = Arguments.createMap(); - error.putString("message", e.getMessage()); - callback.invoke(error); + promise.reject("messaging/unknown", e.getMessage(), e); } } diff --git a/ios/RNFirebase/RNFirebaseMessaging.m b/ios/RNFirebase/RNFirebaseMessaging.m index 899650bb..bdc56718 100644 --- a/ios/RNFirebase/RNFirebaseMessaging.m +++ b/ios/RNFirebase/RNFirebaseMessaging.m @@ -12,18 +12,18 @@ + (UILocalNotification *)UILocalNotification:(id)json { - NSDictionary *details = [self NSDictionary:json]; - UILocalNotification *notification = [UILocalNotification new]; - notification.fireDate = [RCTConvert NSDate:details[@"fireDate"]] ?: [NSDate date]; - notification.alertBody = [RCTConvert NSString:details[@"alertBody"]]; - notification.alertAction = [RCTConvert NSString:details[@"alertAction"]]; - notification.soundName = [RCTConvert NSString:details[@"soundName"]] ?: UILocalNotificationDefaultSoundName; - notification.userInfo = [RCTConvert NSDictionary:details[@"userInfo"]]; - notification.category = [RCTConvert NSString:details[@"category"]]; - if (details[@"applicationIconBadgeNumber"]) { - notification.applicationIconBadgeNumber = [RCTConvert NSInteger:details[@"applicationIconBadgeNumber"]]; - } - return notification; + NSDictionary *details = [self NSDictionary:json]; + UILocalNotification *notification = [UILocalNotification new]; + notification.fireDate = [RCTConvert NSDate:details[@"fireDate"]] ?: [NSDate date]; + notification.alertBody = [RCTConvert NSString:details[@"alertBody"]]; + notification.alertAction = [RCTConvert NSString:details[@"alertAction"]]; + notification.soundName = [RCTConvert NSString:details[@"soundName"]] ?: UILocalNotificationDefaultSoundName; + notification.userInfo = [RCTConvert NSDictionary:details[@"userInfo"]]; + notification.category = [RCTConvert NSString:details[@"category"]]; + if (details[@"applicationIconBadgeNumber"]) { + notification.applicationIconBadgeNumber = [RCTConvert NSInteger:details[@"applicationIconBadgeNumber"]]; + } + return notification; } @end @@ -33,209 +33,204 @@ // https://github.com/facebook/react-native/blob/master/Libraries/PushNotificationIOS/RCTPushNotificationManager.m static NSDictionary *RCTFormatLocalNotification(UILocalNotification *notification) { - NSMutableDictionary *formattedLocalNotification = [NSMutableDictionary dictionary]; - if (notification.fireDate) { - NSDateFormatter *formatter = [NSDateFormatter new]; - [formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"]; - NSString *fireDateString = [formatter stringFromDate:notification.fireDate]; - formattedLocalNotification[@"fireDate"] = fireDateString; - } - formattedLocalNotification[@"alertAction"] = RCTNullIfNil(notification.alertAction); - formattedLocalNotification[@"alertBody"] = RCTNullIfNil(notification.alertBody); - formattedLocalNotification[@"applicationIconBadgeNumber"] = @(notification.applicationIconBadgeNumber); - formattedLocalNotification[@"category"] = RCTNullIfNil(notification.category); - formattedLocalNotification[@"soundName"] = RCTNullIfNil(notification.soundName); - formattedLocalNotification[@"userInfo"] = RCTNullIfNil(RCTJSONClean(notification.userInfo)); - formattedLocalNotification[@"remote"] = @NO; - return formattedLocalNotification; + NSMutableDictionary *formattedLocalNotification = [NSMutableDictionary dictionary]; + if (notification.fireDate) { + NSDateFormatter *formatter = [NSDateFormatter new]; + [formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"]; + NSString *fireDateString = [formatter stringFromDate:notification.fireDate]; + formattedLocalNotification[@"fireDate"] = fireDateString; + } + formattedLocalNotification[@"alertAction"] = RCTNullIfNil(notification.alertAction); + formattedLocalNotification[@"alertBody"] = RCTNullIfNil(notification.alertBody); + formattedLocalNotification[@"applicationIconBadgeNumber"] = @(notification.applicationIconBadgeNumber); + formattedLocalNotification[@"category"] = RCTNullIfNil(notification.category); + formattedLocalNotification[@"soundName"] = RCTNullIfNil(notification.soundName); + formattedLocalNotification[@"userInfo"] = RCTNullIfNil(RCTJSONClean(notification.userInfo)); + formattedLocalNotification[@"remote"] = @NO; + return formattedLocalNotification; } - (void) dealloc { - [[NSNotificationCenter defaultCenter] removeObserver: self]; + [[NSNotificationCenter defaultCenter] removeObserver: self]; } + (void) setup:(UIApplication *) application { - [[NSNotificationCenter defaultCenter] addObserver:self - selector:@selector(connectToFirebase) - name: UIApplicationDidEnterBackgroundNotification - object: nil]; - - [[NSNotificationCenter defaultCenter] addObserver:self - selector:@selector(disconnectFromFirebase) - name: UIApplicationDidBecomeActiveNotification - object: nil]; - - [[NSNotificationCenter defaultCenter] addObserver:self - selector:@selector(handleRemoteNotificationReceived:) - name:MESSAGING_MESSAGE_RECEIVED_REMOTE - object: nil]; - - [[NSNotificationCenter defaultCenter] addObserver:self - selector:@selector(handleLocalNotificationReceived:) - name:MESSAGING_MESSAGE_RECEIVED_LOCAL - object: nil]; - - [[NSNotificationCenter defaultCenter] addObserver:self - selector:@selector(handleTokenRefresh) - name:kFIRInstanceIDTokenRefreshNotification - object: nil]; - - if (SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(@"9.0")) { - UIUserNotificationType allNotificationTypes = - (UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge); - UIUserNotificationSettings *settings = - [UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:nil]; - [[UIApplication sharedApplication] registerUserNotificationSettings:settings]; -} else { - // iOS 10 or later - #if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0 - UNAuthorizationOptions authOptions = - UNAuthorizationOptionAlert - | UNAuthorizationOptionSound - | UNAuthorizationOptionBadge; - [[UNUserNotificationCenter currentNotificationCenter] - requestAuthorizationWithOptions:authOptions - completionHandler:^(BOOL granted, NSError * _Nullable error) { - } - ]; - - // For iOS 10 display notification (sent via APNS) - [[UNUserNotificationCenter currentNotificationCenter] setDelegate:self]; - // For iOS 10 data message (sent via FCM) - [[FIRMessaging messaging] setRemoteMessageDelegate:self]; - #endif -} - -[[UIApplication sharedApplication] registerForRemoteNotifications]; + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(connectToFirebase) + name: UIApplicationDidEnterBackgroundNotification + object: nil]; + + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(disconnectFromFirebase) + name: UIApplicationDidBecomeActiveNotification + object: nil]; + + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(handleRemoteNotificationReceived:) + name:MESSAGING_MESSAGE_RECEIVED_REMOTE + object: nil]; + + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(handleLocalNotificationReceived:) + name:MESSAGING_MESSAGE_RECEIVED_LOCAL + object: nil]; + + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(handleTokenRefresh) + name:kFIRInstanceIDTokenRefreshNotification + object: nil]; + + if (SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(@"9.0")) { + UIUserNotificationType allNotificationTypes = + (UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge); + UIUserNotificationSettings *settings = + [UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:nil]; + [[UIApplication sharedApplication] registerUserNotificationSettings:settings]; + } else { + // iOS 10 or later +#if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0 + UNAuthorizationOptions authOptions = + UNAuthorizationOptionAlert + | UNAuthorizationOptionSound + | UNAuthorizationOptionBadge; + [[UNUserNotificationCenter currentNotificationCenter] + requestAuthorizationWithOptions:authOptions + completionHandler:^(BOOL granted, NSError * _Nullable error) { + } + ]; + + // For iOS 10 display notification (sent via APNS) + [[UNUserNotificationCenter currentNotificationCenter] setDelegate:self]; + // For iOS 10 data message (sent via FCM) + [[FIRMessaging messaging] setRemoteMessageDelegate:self]; +#endif + } + + [[UIApplication sharedApplication] registerForRemoteNotifications]; } #pragma mark callbacks - (void) connectToFirebase { - [[FIRMessaging messaging] connectWithCompletion:^(NSError *error) { - NSDictionary *evt; - NSString *evtName; - if (error != nil) { - NSLog(@"Error connecting: %@", [error debugDescription]); - evtName = MESSAGING_SUBSYSTEM_ERROR; - evt = @{ - @"eventName": MESSAGING_SUBSYSTEM_ERROR, - @"msg": [error debugDescription] - }; - } else { - NSLog(@"Connected to Firebase messaging"); - evtName = MESSAGING_SUBSYSTEM_EVENT; - evt = @{ - @"result": @"connected" - }; - [self - sendJSEvent:evtName - props: evt]; - - } - }]; + [[FIRMessaging messaging] connectWithCompletion:^(NSError *error) { + NSDictionary *evt; + NSString *evtName; + if (error != nil) { + NSLog(@"Error connecting: %@", [error debugDescription]); + evtName = MESSAGING_SUBSYSTEM_ERROR; + evt = @{ + @"eventName": MESSAGING_SUBSYSTEM_ERROR, + @"msg": [error debugDescription] + }; + } else { + NSLog(@"Connected to Firebase messaging"); + evtName = MESSAGING_SUBSYSTEM_EVENT; + evt = @{ + @"result": @"connected" + }; + [self + sendJSEvent:evtName + props: evt]; + + } + }]; } - (void) disconnectFromFirebase { - [[FIRMessaging messaging] disconnect]; - NSLog(@"Disconnect from Firebase"); - [self + [[FIRMessaging messaging] disconnect]; + NSLog(@"Disconnect from Firebase"); + [self sendJSEvent:MESSAGING_SUBSYSTEM_EVENT props: @{ - @"status": @"disconnected" - }]; + @"status": @"disconnected" + }]; } - (void) handleRemoteNotificationReceived:(NSNotification *) n { - NSMutableDictionary *props = [[NSMutableDictionary alloc] initWithDictionary: n.userInfo]; - [self sendJSEvent:MESSAGING_MESSAGE_RECEIVED_REMOTE props: props]; + NSMutableDictionary *props = [[NSMutableDictionary alloc] initWithDictionary: n.userInfo]; + [self sendJSEvent:MESSAGING_MESSAGE_RECEIVED_REMOTE props: props]; } - (void) handleLocalNotificationReceived:(NSNotification *) n { - NSMutableDictionary *props = [[NSMutableDictionary alloc] initWithDictionary: n.userInfo]; - [self sendJSEvent:MESSAGING_MESSAGE_RECEIVED_LOCAL props: props]; + NSMutableDictionary *props = [[NSMutableDictionary alloc] initWithDictionary: n.userInfo]; + [self sendJSEvent:MESSAGING_MESSAGE_RECEIVED_LOCAL props: props]; } - (void) handleTokenRefresh { - NSDictionary *props = @{ - @"status": @"token_refreshed", - @"token": [[FIRInstanceID instanceID] token] - }; - [self sendJSEvent:MESSAGING_TOKEN_REFRESH props: props]; + NSDictionary *props = @{ + @"status": @"token_refreshed", + @"token": [[FIRInstanceID instanceID] token] + }; + [self sendJSEvent:MESSAGING_TOKEN_REFRESH props: props]; } RCT_EXPORT_MODULE(RNFirebaseMessaging); -RCT_EXPORT_METHOD(getToken:(RCTResponseSenderBlock)callback) -{ - NSString *token = [[FIRInstanceID instanceID] token]; - callback(@[[NSNull null], @{ - @"status": @"success", - @"token": token - }]); +RCT_EXPORT_METHOD(getToken:(RCTPromiseResolveBlock) resolve rejecter:(RCTPromiseRejectBlock) reject) { + NSString *token = [[FIRInstanceID instanceID] token]; + resolve(token); } - RCT_EXPORT_METHOD(sendLocal:(UILocalNotification *)notification - callback:(RCTResponseSenderBlock) callback) + callback:(RCTResponseSenderBlock) callback) { - NSLog(@"sendLocal called with notification: %@", notification); - [RCTSharedApplication() presentLocalNotificationNow:notification]; + NSLog(@"sendLocal called with notification: %@", notification); + [RCTSharedApplication() presentLocalNotificationNow:notification]; } RCT_EXPORT_METHOD(scheduleLocal:(UILocalNotification *)notification - callback:(RCTResponseSenderBlock) callback) + callback:(RCTResponseSenderBlock) callback) { - [RCTSharedApplication() scheduleLocalNotification:notification]; + [RCTSharedApplication() scheduleLocalNotification:notification]; } RCT_EXPORT_METHOD(cancelAllLocalNotifications) { - [RCTSharedApplication() cancelAllLocalNotifications]; + [RCTSharedApplication() cancelAllLocalNotifications]; } RCT_EXPORT_METHOD(cancelLocalNotifications:(NSDictionary *)userInfo) { - for (UILocalNotification *notification in [UIApplication sharedApplication].scheduledLocalNotifications) { - __block BOOL matchesAll = YES; - NSDictionary *notificationInfo = notification.userInfo; - // Note: we do this with a loop instead of just `isEqualToDictionary:` - // because we only require that all specified userInfo values match the - // notificationInfo values - notificationInfo may contain additional values - // which we don't care about. - [userInfo enumerateKeysAndObjectsUsingBlock:^(NSString *key, id obj, BOOL *stop) { - if (![notificationInfo[key] isEqual:obj]) { - matchesAll = NO; - *stop = YES; - } - }]; - if (matchesAll) { - [[UIApplication sharedApplication] cancelLocalNotification:notification]; + for (UILocalNotification *notification in [UIApplication sharedApplication].scheduledLocalNotifications) { + __block BOOL matchesAll = YES; + NSDictionary *notificationInfo = notification.userInfo; + // Note: we do this with a loop instead of just `isEqualToDictionary:` + // because we only require that all specified userInfo values match the + // notificationInfo values - notificationInfo may contain additional values + // which we don't care about. + [userInfo enumerateKeysAndObjectsUsingBlock:^(NSString *key, id obj, BOOL *stop) { + if (![notificationInfo[key] isEqual:obj]) { + matchesAll = NO; + *stop = YES; + } + }]; + if (matchesAll) { + [[UIApplication sharedApplication] cancelLocalNotification:notification]; + } } - } } RCT_EXPORT_METHOD(sendRemote:(UILocalNotification *)notification - callback:(RCTResponseSenderBlock) callback) + callback:(RCTResponseSenderBlock) callback) { - + } RCT_EXPORT_METHOD(send:(NSString *) senderId - messageId:(NSString *) messageId - messageType:(NSString *) messageType - msg: (NSString *) msg - callback:(RCTResponseSenderBlock)callback) + messageId:(NSString *) messageId + messageType:(NSString *) messageType + msg: (NSString *) msg + callback:(RCTResponseSenderBlock)callback) { - + } RCT_EXPORT_METHOD(listenForTokenRefresh:(RCTResponseSenderBlock)callback) @@ -245,41 +240,41 @@ RCT_EXPORT_METHOD(unlistenForTokenRefresh:(RCTResponseSenderBlock)callback) {} RCT_EXPORT_METHOD(subscribeToTopic:(NSString *) topic - callback:(RCTResponseSenderBlock)callback) + callback:(RCTResponseSenderBlock)callback) { - [[FIRMessaging messaging] subscribeToTopic:topic]; - callback(@[[NSNull null], @{ - @"result": @"success", - @"topic": topic - }]); + [[FIRMessaging messaging] subscribeToTopic:topic]; + callback(@[[NSNull null], @{ + @"result": @"success", + @"topic": topic + }]); } RCT_EXPORT_METHOD(unsubscribeFromTopic:(NSString *) topic - callback: (RCTResponseSenderBlock)callback) + callback: (RCTResponseSenderBlock)callback) { - [[FIRMessaging messaging] unsubscribeFromTopic:topic]; - callback(@[[NSNull null], @{ - @"result": @"success", - @"topic": topic - }]); + [[FIRMessaging messaging] unsubscribeFromTopic:topic]; + callback(@[[NSNull null], @{ + @"result": @"success", + @"topic": topic + }]); } RCT_EXPORT_METHOD(setBadge:(NSInteger) number - callback:(RCTResponseSenderBlock) callback) + callback:(RCTResponseSenderBlock) callback) { - RCTSharedApplication().applicationIconBadgeNumber = number; - callback(@[[NSNull null], @{ - @"result": @"success", - @"number": @(number) - }]); + RCTSharedApplication().applicationIconBadgeNumber = number; + callback(@[[NSNull null], @{ + @"result": @"success", + @"number": @(number) + }]); } RCT_EXPORT_METHOD(getBadge:(RCTResponseSenderBlock) callback) { - callback(@[[NSNull null], @{ - @"result": @"success", - @"number": @(RCTSharedApplication().applicationIconBadgeNumber) - }]); + callback(@[[NSNull null], @{ + @"result": @"success", + @"number": @(RCTSharedApplication().applicationIconBadgeNumber) + }]); } RCT_EXPORT_METHOD(listenForReceiveNotification:(RCTResponseSenderBlock)callback) @@ -297,11 +292,11 @@ RCT_EXPORT_METHOD(unlistenForReceiveUpstreamSend:(RCTResponseSenderBlock)callbac // Not sure how to get away from this... yet - (NSArray *)supportedEvents { return @[ - MESSAGING_SUBSYSTEM_EVENT, - MESSAGING_SUBSYSTEM_ERROR, - MESSAGING_TOKEN_REFRESH, - MESSAGING_MESSAGE_RECEIVED_LOCAL, - MESSAGING_MESSAGE_RECEIVED_REMOTE]; + MESSAGING_SUBSYSTEM_EVENT, + MESSAGING_SUBSYSTEM_ERROR, + MESSAGING_TOKEN_REFRESH, + MESSAGING_MESSAGE_RECEIVED_LOCAL, + MESSAGING_MESSAGE_RECEIVED_REMOTE]; } - (void) sendJSEvent:(NSString *)title @@ -310,9 +305,9 @@ RCT_EXPORT_METHOD(unlistenForReceiveUpstreamSend:(RCTResponseSenderBlock)callbac @try { [self sendEventWithName:title body:@{ - @"eventName": title, - @"body": props - }]; + @"eventName": title, + @"body": props + }]; } @catch (NSException *err) { NSLog(@"An error occurred in sendJSEvent: %@", [err debugDescription]); diff --git a/lib/modules/messaging/index.js b/lib/modules/messaging/index.js index ca1ad57e..6c598baa 100644 --- a/lib/modules/messaging/index.js +++ b/lib/modules/messaging/index.js @@ -44,8 +44,7 @@ export default class Messaging extends Base { } getToken() { - this.log.info('getToken for cloudMessaging'); - return promisify('getToken', FirebaseMessaging)(); + return FirebaseMessaging.getToken(); } send(remoteMessage: RemoteMessage) { diff --git a/package.json b/package.json index 18042b4e..ead99865 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,6 @@ "description": "A react native firebase library supporting both android and ios native firebase SDK's", "main": "index", "scripts": { - "start": "node node_modules/react-native/local-cli/cli.js start", "build": "./node_modules/.bin/babel --source-maps=true --out-dir=dist .", "dev": "npm run compile -- --watch", "lint": "eslint ./src",