diff --git a/RNNotifications/RNNotifications.m b/RNNotifications/RNNotifications.m index 072b26dc45acb6d78252a71b893cd3afaa821e02..da258486f2b40877f4f1a00679fc85ec64d788b5 100644 --- a/RNNotifications/RNNotifications.m +++ b/RNNotifications/RNNotifications.m @@ -142,6 +142,9 @@ RCT_EXPORT_MODULE() selector:@selector(handleNotificationActionTriggered:) name:RNNotificationActionTriggered object:nil]; + + [RNNotificationsBridgeQueue sharedInstance].openedRemoteNotification = [_bridge.launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]; + [RNNotificationsBridgeQueue sharedInstance].openedLocalNotification = [_bridge.launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey]; } /* @@ -165,15 +168,22 @@ RCT_EXPORT_MODULE() { UIApplicationState state = [UIApplication sharedApplication].applicationState; - if (state == UIApplicationStateActive) { - // Notification received foreground - [self didReceiveNotificationOnForegroundState:notification]; - } else if (state == UIApplicationStateInactive) { - // Notification opened - [self didNotificationOpen:notification]; + if ([RNNotificationsBridgeQueue sharedInstance].jsIsReady == YES) { + // JS thread is ready, push the notification to the bridge + + if (state == UIApplicationStateActive) { + // Notification received foreground + [self didReceiveNotificationOnForegroundState:notification]; + } else if (state == UIApplicationStateInactive) { + // Notification opened + [self didNotificationOpen:notification]; + } else { + // Notification received background + [self didReceiveNotificationOnBackgroundState:notification]; + } } else { - // Notification received background - [self didReceiveNotificationOnBackgroundState:notification]; + // JS thread is not ready - store it in the native notifications queue + [[RNNotificationsBridgeQueue sharedInstance] postNotification:notification]; } } @@ -231,14 +241,9 @@ RCT_EXPORT_MODULE() } } - // if Js thread is ready- post notification to bridge. otherwise- post it to the bridge queue - if ([RNNotificationsBridgeQueue sharedInstance].jsIsReady == YES) { - [[NSNotificationCenter defaultCenter] postNotificationName:RNNotificationReceivedBackground - object:self - userInfo:notification]; - } else { - [[RNNotificationsBridgeQueue sharedInstance] postNotification:notification]; - } + [[NSNotificationCenter defaultCenter] postNotificationName:RNNotificationReceivedBackground + object:self + userInfo:notification]; } + (void)didNotificationOpen:(NSDictionary *)notification @@ -450,19 +455,32 @@ RCT_EXPORT_METHOD(backgroundTimeRemaining:(RCTResponseSenderBlock)callback) RCT_EXPORT_METHOD(consumeBackgroundQueue) { + // Mark JS Thread as ready + [RNNotificationsBridgeQueue sharedInstance].jsIsReady = YES; + + // Push actions to JS [[RNNotificationsBridgeQueue sharedInstance] consumeActionsQueue:^(NSDictionary* action) { [[NSNotificationCenter defaultCenter] postNotificationName:RNNotificationActionTriggered object:self userInfo:action]; }]; + // Push background notifications to JS [[RNNotificationsBridgeQueue sharedInstance] consumeNotificationsQueue:^(NSDictionary* notification) { - [[NSNotificationCenter defaultCenter] postNotificationName:RNNotificationReceivedBackground - object:self - userInfo:notification]; + [RNNotifications didReceiveRemoteNotification:notification]; }]; - [RNNotificationsBridgeQueue sharedInstance].jsIsReady = YES; + // Push opened local notifications + NSDictionary* openedLocalNotification = [RNNotificationsBridgeQueue sharedInstance].openedLocalNotification; + if (openedLocalNotification) { + [RNNotifications didNotificationOpen:openedLocalNotification]; + } + + // Push opened remote notifications + NSDictionary* openedRemoteNotification = [RNNotificationsBridgeQueue sharedInstance].openedRemoteNotification; + if (openedRemoteNotification) { + [RNNotifications didNotificationOpen:openedRemoteNotification]; + } } RCT_EXPORT_METHOD(localNotification:(NSDictionary *)notification) diff --git a/RNNotifications/RNNotificationsBridgeQueue.h b/RNNotifications/RNNotificationsBridgeQueue.h index 0d3409d713443e3f42be4835ddd895637b0f9bd4..61ff05ba57e5baf5ea3570bfd44ea6924d892f17 100644 --- a/RNNotifications/RNNotificationsBridgeQueue.h +++ b/RNNotifications/RNNotificationsBridgeQueue.h @@ -3,8 +3,11 @@ @interface RNNotificationsBridgeQueue : NSObject @property BOOL jsIsReady; +@property NSDictionary* openedRemoteNotification; +@property NSDictionary* openedLocalNotification; + (nonnull instancetype)sharedInstance; + - (void)postAction:(NSDictionary *)action withCompletionKey:(NSString *)completionKey andCompletionHandler:(void (^)())completionHandler; - (void)postNotification:(NSDictionary *)notification; diff --git a/RNNotifications/RNNotificationsBridgeQueue.m b/RNNotifications/RNNotificationsBridgeQueue.m index 90960f22806e43c13eea45733cb604a77f2ca6af..9555f3fb457a02bd88215dc5671e398c92ff3eb9 100644 --- a/RNNotifications/RNNotificationsBridgeQueue.m +++ b/RNNotifications/RNNotificationsBridgeQueue.m @@ -3,7 +3,7 @@ @implementation RNNotificationsBridgeQueue NSMutableArray* actionsQueue; -NSMutableArray* backgroundNotificationsQueue; +NSMutableArray* notificationsQueue; NSMutableDictionary* actionCompletionHandlers; + (nonnull instancetype)sharedInstance { @@ -19,7 +19,7 @@ NSMutableDictionary* actionCompletionHandlers; - (instancetype)init { actionsQueue = [NSMutableArray new]; - backgroundNotificationsQueue = [NSMutableArray new]; + notificationsQueue = [NSMutableArray new]; actionCompletionHandlers = [NSMutableDictionary new]; self.jsIsReady = NO; @@ -28,16 +28,16 @@ NSMutableDictionary* actionCompletionHandlers; - (void)postNotification:(NSDictionary *)notification { - if (!backgroundNotificationsQueue) return; - [backgroundNotificationsQueue insertObject:notification atIndex:0]; + if (!notificationsQueue) return; + [notificationsQueue insertObject:notification atIndex:0]; } - (NSDictionary *)dequeueSingleNotification { - if (!backgroundNotificationsQueue || backgroundNotificationsQueue.count == 0) return nil; + if (!notificationsQueue || notificationsQueue.count == 0) return nil; - NSDictionary* notification = [backgroundNotificationsQueue lastObject]; - [backgroundNotificationsQueue removeLastObject]; + NSDictionary* notification = [notificationsQueue lastObject]; + [notificationsQueue removeLastObject]; return notification; } @@ -50,7 +50,7 @@ NSMutableDictionary* actionCompletionHandlers; block(notification); } - backgroundNotificationsQueue = nil; + notificationsQueue = nil; } - (void)postAction:(NSDictionary *)action withCompletionKey:(NSString *)completionKey andCompletionHandler:(void (^)())completionHandler diff --git a/example/ios/NotificationsExampleApp.xcodeproj/project.pbxproj b/example/ios/NotificationsExampleApp.xcodeproj/project.pbxproj index 2480ea91cf7190eb8e9eeec4a6cf7d44ef9c3ec3..0817c65e7530b2d74177aa203e87b4d66e0cdc34 100644 --- a/example/ios/NotificationsExampleApp.xcodeproj/project.pbxproj +++ b/example/ios/NotificationsExampleApp.xcodeproj/project.pbxproj @@ -691,6 +691,7 @@ OTHER_LDFLAGS = "-ObjC"; PRODUCT_BUNDLE_IDENTIFIER = "com.wix.smart-notifications-test-app"; PRODUCT_NAME = NotificationsExampleApp; + PROVISIONING_PROFILE = ""; }; name = Debug; }; @@ -710,6 +711,7 @@ OTHER_LDFLAGS = "-ObjC"; PRODUCT_BUNDLE_IDENTIFIER = "com.wix.smart-notifications-test-app"; PRODUCT_NAME = NotificationsExampleApp; + PROVISIONING_PROFILE = ""; }; name = Release; }; @@ -730,6 +732,7 @@ CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + CODE_SIGN_IDENTITY = "iPhone Developer"; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; COPY_PHASE_STRIP = NO; ENABLE_STRICT_OBJC_MSGSEND = YES; @@ -777,6 +780,7 @@ CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + CODE_SIGN_IDENTITY = "iPhone Developer"; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; COPY_PHASE_STRIP = YES; ENABLE_NS_ASSERTIONS = NO; diff --git a/example/ios/NotificationsExampleApp/AppDelegate.m b/example/ios/NotificationsExampleApp/AppDelegate.m index 18a646a810b5f133e180e464dcc635e011530567..f759c7e156d019095dadfea85b0264b9721fd5bf 100644 --- a/example/ios/NotificationsExampleApp/AppDelegate.m +++ b/example/ios/NotificationsExampleApp/AppDelegate.m @@ -33,7 +33,7 @@ * on the same Wi-Fi network. */ - jsCodeLocation = [NSURL URLWithString:@"http://192.168.1.15:8081/index.ios.bundle?platform=ios&dev=true"]; + jsCodeLocation = [NSURL URLWithString:@"http://172.31.9.91:8081/index.ios.bundle?platform=ios&dev=true"]; /** * OPTION 2