Commit 25c9c400 authored by Lidan Hifi's avatar Lidan Hifi

fix bug- trigger notification opened event on app launch

parent 97ed9e39
...@@ -142,6 +142,9 @@ RCT_EXPORT_MODULE() ...@@ -142,6 +142,9 @@ RCT_EXPORT_MODULE()
selector:@selector(handleNotificationActionTriggered:) selector:@selector(handleNotificationActionTriggered:)
name:RNNotificationActionTriggered name:RNNotificationActionTriggered
object:nil]; object:nil];
[RNNotificationsBridgeQueue sharedInstance].openedRemoteNotification = [_bridge.launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
[RNNotificationsBridgeQueue sharedInstance].openedLocalNotification = [_bridge.launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
} }
/* /*
...@@ -165,6 +168,9 @@ RCT_EXPORT_MODULE() ...@@ -165,6 +168,9 @@ RCT_EXPORT_MODULE()
{ {
UIApplicationState state = [UIApplication sharedApplication].applicationState; UIApplicationState state = [UIApplication sharedApplication].applicationState;
if ([RNNotificationsBridgeQueue sharedInstance].jsIsReady == YES) {
// JS thread is ready, push the notification to the bridge
if (state == UIApplicationStateActive) { if (state == UIApplicationStateActive) {
// Notification received foreground // Notification received foreground
[self didReceiveNotificationOnForegroundState:notification]; [self didReceiveNotificationOnForegroundState:notification];
...@@ -175,6 +181,10 @@ RCT_EXPORT_MODULE() ...@@ -175,6 +181,10 @@ RCT_EXPORT_MODULE()
// Notification received background // Notification received background
[self didReceiveNotificationOnBackgroundState:notification]; [self didReceiveNotificationOnBackgroundState:notification];
} }
} else {
// JS thread is not ready - store it in the native notifications queue
[[RNNotificationsBridgeQueue sharedInstance] postNotification:notification];
}
} }
+ (void)didReceiveLocalNotification:(UILocalNotification *)notification + (void)didReceiveLocalNotification:(UILocalNotification *)notification
...@@ -231,14 +241,9 @@ RCT_EXPORT_MODULE() ...@@ -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 [[NSNotificationCenter defaultCenter] postNotificationName:RNNotificationReceivedBackground
object:self object:self
userInfo:notification]; userInfo:notification];
} else {
[[RNNotificationsBridgeQueue sharedInstance] postNotification:notification];
}
} }
+ (void)didNotificationOpen:(NSDictionary *)notification + (void)didNotificationOpen:(NSDictionary *)notification
...@@ -450,19 +455,32 @@ RCT_EXPORT_METHOD(backgroundTimeRemaining:(RCTResponseSenderBlock)callback) ...@@ -450,19 +455,32 @@ RCT_EXPORT_METHOD(backgroundTimeRemaining:(RCTResponseSenderBlock)callback)
RCT_EXPORT_METHOD(consumeBackgroundQueue) RCT_EXPORT_METHOD(consumeBackgroundQueue)
{ {
// Mark JS Thread as ready
[RNNotificationsBridgeQueue sharedInstance].jsIsReady = YES;
// Push actions to JS
[[RNNotificationsBridgeQueue sharedInstance] consumeActionsQueue:^(NSDictionary* action) { [[RNNotificationsBridgeQueue sharedInstance] consumeActionsQueue:^(NSDictionary* action) {
[[NSNotificationCenter defaultCenter] postNotificationName:RNNotificationActionTriggered [[NSNotificationCenter defaultCenter] postNotificationName:RNNotificationActionTriggered
object:self object:self
userInfo:action]; userInfo:action];
}]; }];
// Push background notifications to JS
[[RNNotificationsBridgeQueue sharedInstance] consumeNotificationsQueue:^(NSDictionary* notification) { [[RNNotificationsBridgeQueue sharedInstance] consumeNotificationsQueue:^(NSDictionary* notification) {
[[NSNotificationCenter defaultCenter] postNotificationName:RNNotificationReceivedBackground [RNNotifications didReceiveRemoteNotification:notification];
object:self
userInfo: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) RCT_EXPORT_METHOD(localNotification:(NSDictionary *)notification)
......
...@@ -3,8 +3,11 @@ ...@@ -3,8 +3,11 @@
@interface RNNotificationsBridgeQueue : NSObject @interface RNNotificationsBridgeQueue : NSObject
@property BOOL jsIsReady; @property BOOL jsIsReady;
@property NSDictionary* openedRemoteNotification;
@property NSDictionary* openedLocalNotification;
+ (nonnull instancetype)sharedInstance; + (nonnull instancetype)sharedInstance;
- (void)postAction:(NSDictionary *)action withCompletionKey:(NSString *)completionKey andCompletionHandler:(void (^)())completionHandler; - (void)postAction:(NSDictionary *)action withCompletionKey:(NSString *)completionKey andCompletionHandler:(void (^)())completionHandler;
- (void)postNotification:(NSDictionary *)notification; - (void)postNotification:(NSDictionary *)notification;
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
@implementation RNNotificationsBridgeQueue @implementation RNNotificationsBridgeQueue
NSMutableArray<NSDictionary *>* actionsQueue; NSMutableArray<NSDictionary *>* actionsQueue;
NSMutableArray<NSDictionary *>* backgroundNotificationsQueue; NSMutableArray<NSDictionary *>* notificationsQueue;
NSMutableDictionary* actionCompletionHandlers; NSMutableDictionary* actionCompletionHandlers;
+ (nonnull instancetype)sharedInstance { + (nonnull instancetype)sharedInstance {
...@@ -19,7 +19,7 @@ NSMutableDictionary* actionCompletionHandlers; ...@@ -19,7 +19,7 @@ NSMutableDictionary* actionCompletionHandlers;
- (instancetype)init - (instancetype)init
{ {
actionsQueue = [NSMutableArray new]; actionsQueue = [NSMutableArray new];
backgroundNotificationsQueue = [NSMutableArray new]; notificationsQueue = [NSMutableArray new];
actionCompletionHandlers = [NSMutableDictionary new]; actionCompletionHandlers = [NSMutableDictionary new];
self.jsIsReady = NO; self.jsIsReady = NO;
...@@ -28,16 +28,16 @@ NSMutableDictionary* actionCompletionHandlers; ...@@ -28,16 +28,16 @@ NSMutableDictionary* actionCompletionHandlers;
- (void)postNotification:(NSDictionary *)notification - (void)postNotification:(NSDictionary *)notification
{ {
if (!backgroundNotificationsQueue) return; if (!notificationsQueue) return;
[backgroundNotificationsQueue insertObject:notification atIndex:0]; [notificationsQueue insertObject:notification atIndex:0];
} }
- (NSDictionary *)dequeueSingleNotification - (NSDictionary *)dequeueSingleNotification
{ {
if (!backgroundNotificationsQueue || backgroundNotificationsQueue.count == 0) return nil; if (!notificationsQueue || notificationsQueue.count == 0) return nil;
NSDictionary* notification = [backgroundNotificationsQueue lastObject]; NSDictionary* notification = [notificationsQueue lastObject];
[backgroundNotificationsQueue removeLastObject]; [notificationsQueue removeLastObject];
return notification; return notification;
} }
...@@ -50,7 +50,7 @@ NSMutableDictionary* actionCompletionHandlers; ...@@ -50,7 +50,7 @@ NSMutableDictionary* actionCompletionHandlers;
block(notification); block(notification);
} }
backgroundNotificationsQueue = nil; notificationsQueue = nil;
} }
- (void)postAction:(NSDictionary *)action withCompletionKey:(NSString *)completionKey andCompletionHandler:(void (^)())completionHandler - (void)postAction:(NSDictionary *)action withCompletionKey:(NSString *)completionKey andCompletionHandler:(void (^)())completionHandler
......
...@@ -691,6 +691,7 @@ ...@@ -691,6 +691,7 @@
OTHER_LDFLAGS = "-ObjC"; OTHER_LDFLAGS = "-ObjC";
PRODUCT_BUNDLE_IDENTIFIER = "com.wix.smart-notifications-test-app"; PRODUCT_BUNDLE_IDENTIFIER = "com.wix.smart-notifications-test-app";
PRODUCT_NAME = NotificationsExampleApp; PRODUCT_NAME = NotificationsExampleApp;
PROVISIONING_PROFILE = "";
}; };
name = Debug; name = Debug;
}; };
...@@ -710,6 +711,7 @@ ...@@ -710,6 +711,7 @@
OTHER_LDFLAGS = "-ObjC"; OTHER_LDFLAGS = "-ObjC";
PRODUCT_BUNDLE_IDENTIFIER = "com.wix.smart-notifications-test-app"; PRODUCT_BUNDLE_IDENTIFIER = "com.wix.smart-notifications-test-app";
PRODUCT_NAME = NotificationsExampleApp; PRODUCT_NAME = NotificationsExampleApp;
PROVISIONING_PROFILE = "";
}; };
name = Release; name = Release;
}; };
...@@ -730,6 +732,7 @@ ...@@ -730,6 +732,7 @@
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
CODE_SIGN_IDENTITY = "iPhone Developer";
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
COPY_PHASE_STRIP = NO; COPY_PHASE_STRIP = NO;
ENABLE_STRICT_OBJC_MSGSEND = YES; ENABLE_STRICT_OBJC_MSGSEND = YES;
...@@ -777,6 +780,7 @@ ...@@ -777,6 +780,7 @@
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
CODE_SIGN_IDENTITY = "iPhone Developer";
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
COPY_PHASE_STRIP = YES; COPY_PHASE_STRIP = YES;
ENABLE_NS_ASSERTIONS = NO; ENABLE_NS_ASSERTIONS = NO;
......
...@@ -33,7 +33,7 @@ ...@@ -33,7 +33,7 @@
* on the same Wi-Fi network. * 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 * OPTION 2
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment