diff --git a/RNNotifications/RNBridgeModule.m b/RNNotifications/RNBridgeModule.m index 918843442d910ef052aa3d397323b3a82b918524..e8e595c8864b4166037c4e41410a8724171f7103 100644 --- a/RNNotifications/RNBridgeModule.m +++ b/RNNotifications/RNBridgeModule.m @@ -1,11 +1,12 @@ #import "RNBridgeModule.h" #import "RNCommandsHandler.h" #import "RCTConvert+Notifications.h" -#import "RNNotificationsBridgeQueue.h" #import "RNEventEmitter.h" #import "RNNotifications.h" +#import "RNNotificationsStore.h" @implementation RNBridgeModule { + RNNotificationsStore* _store; RNCommandsHandler* _commandsHandler; } @@ -15,6 +16,7 @@ RCT_EXPORT_MODULE(); - (instancetype)init { self = [super init]; + _store = [RNNotificationsStore new]; _commandsHandler = [[RNCommandsHandler alloc] init]; return self; } @@ -29,7 +31,7 @@ RCT_EXPORT_MODULE(); - (void)setBridge:(RCTBridge *)bridge { _bridge = bridge; - [RNNotificationsBridgeQueue sharedInstance].openedRemoteNotification = [_bridge.launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]; + _store.initialNotification = [_bridge.launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]; } #pragma mark - JS interface diff --git a/RNNotifications/RNCommandsHandler.h b/RNNotifications/RNCommandsHandler.h index c16822e82daea6ae4b957cd91d8e0eb58acbd201..bb82398e524ac326847d28e14d13431e89c5a9dd 100644 --- a/RNNotifications/RNCommandsHandler.h +++ b/RNNotifications/RNCommandsHandler.h @@ -1,8 +1,11 @@ #import #import +#import "RNNotificationsStore.h" @interface RNCommandsHandler : NSObject +- (instancetype)initWithStore:(RNNotificationsStore *)store; + - (void)requestPermissionsWithCategories:(NSArray *)json; - (void)getInitialNotification:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject; diff --git a/RNNotifications/RNCommandsHandler.m b/RNNotifications/RNCommandsHandler.m index 848cf6132ff389723e5286e0b172a3237fb9393d..1161d3e935e3c3d41ebbf40e8b0281d5c502753f 100644 --- a/RNNotifications/RNCommandsHandler.m +++ b/RNNotifications/RNCommandsHandler.m @@ -1,26 +1,20 @@ #import "RNCommandsHandler.h" #import "RNNotifications.h" -#import "RNNotificationsBridgeQueue.h" #import "RCTConvert+Notifications.h" #import "RNPushKit.h" @implementation RNCommandsHandler { RNPushKit* _pushKit; + RNNotificationsStore* _store; } -- (instancetype)init { +- (instancetype)initWithStore:(RNNotificationsStore *)store { self = [super init]; - [[NSNotificationCenter defaultCenter] addObserver:self - selector:@selector(onJavaScriptLoaded) - name:RCTJavaScriptDidLoadNotification - object:nil]; + _store = store; + return self; } -- (void)onJavaScriptLoaded { -// [RNNotificationsBridgeQueue sharedInstance].jsIsReady = YES; -} - - (void)requestPermissionsWithCategories:(NSArray *)json { NSMutableSet* categories = nil; @@ -39,7 +33,9 @@ if (granted) { [UNUserNotificationCenter.currentNotificationCenter getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) { if (settings.authorizationStatus == UNAuthorizationStatusAuthorized) { - [[UIApplication sharedApplication] registerForRemoteNotifications]; + dispatch_async(dispatch_get_main_queue(), ^{ + [[UIApplication sharedApplication] registerForRemoteNotifications]; + }); } }]; } else { @@ -50,11 +46,11 @@ } - (void)getInitialNotification:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject { - resolve([RNNotificationsBridgeQueue sharedInstance].openedRemoteNotification); + resolve(_store.initialNotification); } - (void)completionHandler:(NSString *)completionKey { - [[RNNotificationsBridgeQueue sharedInstance] completeAction:completionKey]; + [_store completeAction:completionKey]; } - (void)abandonPermissions { diff --git a/RNNotifications/RNEventEmitter.h b/RNNotifications/RNEventEmitter.h index e7405a22a1c8777508bfbfe44939759249700235..9b13075a06dc9f060c581c43ef4dd1989d8cd05a 100644 --- a/RNNotifications/RNEventEmitter.h +++ b/RNNotifications/RNEventEmitter.h @@ -1,12 +1,13 @@ #import "RCTEventEmitter.h" -static NSString* const Registered = @"remoteNotificationsRegistered"; -static NSString* const RegistrationFailed = @"remoteNotificationsRegistrationFailed"; -static NSString* const PushKitRegistered = @"pushKitRegistered"; -static NSString* const NotificationReceivedForeground = @"notificationReceivedForeground"; -static NSString* const NotificationReceivedBackground = @"notificationReceivedBackground"; -static NSString* const NotificationOpened = @"notificationOpened"; -static NSString* const NotificationActionReceived = @"notificationActionReceived"; +static NSString* const RNRegistered = @"RNNotificationsRegistered"; +static NSString* const RNRegistrationFailed = @"RNNotificationsRegistrationFailed"; +static NSString* const RNPushKitRegistered = @"RNPushKitRegistered"; +static NSString* const RNNotificationReceivedForeground = @"RNNotificationReceivedForeground"; +static NSString* const RNNotificationReceivedBackground = @"RNNotificationReceivedBackground"; +static NSString* const RNNotificationOpened = @"RNNotificationOpened"; +static NSString* const RNActionTriggered = @"RNNotificationActionTriggered"; + @interface RNEventEmitter : RCTEventEmitter diff --git a/RNNotifications/RNEventEmitter.m b/RNNotifications/RNEventEmitter.m index 7086c62618678842898190e3db0df784436a43e9..8045bae525e0ff8513da2c70babdebb9c3e1a5e0 100644 --- a/RNNotifications/RNEventEmitter.m +++ b/RNNotifications/RNEventEmitter.m @@ -5,13 +5,13 @@ RCT_EXPORT_MODULE(); -(NSArray *)supportedEvents { - return @[Registered, - RegistrationFailed, - PushKitRegistered, - NotificationReceivedForeground, - NotificationReceivedBackground, - NotificationOpened, - NotificationActionReceived]; + return @[RNRegistered, + RNRegistrationFailed, + RNPushKitRegistered, + RNNotificationReceivedForeground, + RNNotificationReceivedBackground, + RNNotificationOpened, + RNActionTriggered]; } # pragma mark public diff --git a/RNNotifications/RNNotificationEventHandler.h b/RNNotifications/RNNotificationEventHandler.h index f43d52720eea67fb74fa79c3f068a1ba997717c5..bf56c2184e3f00818387bc2105d36e31306c8a5d 100644 --- a/RNNotifications/RNNotificationEventHandler.h +++ b/RNNotifications/RNNotificationEventHandler.h @@ -1,8 +1,11 @@ #import @import UserNotifications; +#import "RNNotificationsStore.h" @interface RNNotificationEventHandler : NSObject +- (instancetype)initWithStore:(RNNotificationsStore *)store; + - (void)didReceiveForegroundPayload:(NSDictionary *)payload; - (void)didOpenNotificationPayload:(NSDictionary *)payload; - (void)handleActionWithIdentifier:(NSString *)identifier forPayload:(NSDictionary *)payload withResponse:(NSString *)response completionHandler:(void (^)())completionHandler; diff --git a/RNNotifications/RNNotificationEventHandler.m b/RNNotifications/RNNotificationEventHandler.m index 077121718580b2fbedf902d24b2f4aa9b74c91db..58cdebc7178c61cdc3b18269a5bf784c1b5ca15c 100644 --- a/RNNotifications/RNNotificationEventHandler.m +++ b/RNNotifications/RNNotificationEventHandler.m @@ -1,19 +1,23 @@ #import "RNNotificationEventHandler.h" -#import "RNNotificationsBridgeQueue.h" #import "RNEventEmitter.h" -@implementation RNNotificationEventHandler +@implementation RNNotificationEventHandler { + RNNotificationsStore* _store; +} + +- (instancetype)initWithStore:(RNNotificationsStore *)store { + self = [super init]; + _store = store; + + return self; +} - (void)didReceiveForegroundPayload:(NSDictionary *)payload { -// if ([RNNotificationsBridgeQueue sharedInstance].jsIsReady == YES) { - [RNEventEmitter sendEvent:NotificationReceivedForeground body:payload]; -// } + [RNEventEmitter sendEvent:RNNotificationReceivedForeground body:payload]; } - (void)didOpenNotificationPayload:(NSDictionary *)payload { -// if ([RNNotificationsBridgeQueue sharedInstance].jsIsReady == YES) { - [RNEventEmitter sendEvent:NotificationOpened body:payload]; -// } + [RNEventEmitter sendEvent:RNNotificationOpened body:payload]; } - (void)handleActionWithIdentifier:(NSString *)identifier forPayload:(NSDictionary *)payload withResponse:(NSString *)response completionHandler:(void (^)())completionHandler { @@ -33,12 +37,8 @@ info[@"notification"] = userInfo; } - // Emit event to the queue (in order to store the completion handler). if JS thread is ready, post it also to the notification center (to the bridge). - [[RNNotificationsBridgeQueue sharedInstance] postAction:info withCompletionKey:completionKey andCompletionHandler:completionHandler]; - - // if ([RNNotificationsBridgeQueue sharedInstance].jsIsReady == YES) { - [RNEventEmitter sendEvent:NotificationActionReceived body:userInfo]; - // } + [_store setCompletionHandler:completionHandler withCompletionKey:identifier]; + [RNEventEmitter sendEvent:RNActionTriggered body:userInfo]; } @end diff --git a/RNNotifications/RNNotifications.m b/RNNotifications/RNNotifications.m index f4c64ff1a16207d065ca8883fa08c8c853d4f528..20772d205e0f5b438bd656d2eccb23976a2bdddd 100644 --- a/RNNotifications/RNNotifications.m +++ b/RNNotifications/RNNotifications.m @@ -6,17 +6,16 @@ #import "RNNotifications.h" #import #import -#import "RNNotificationsBridgeQueue.h" #import #import "RNEventEmitter.h" -#import "RNNotificationEventHandler.h" +#import "RNNotificationCenterListener.h" #import "RNUtils.h" NSString* const RNNotificationCreateAction = @"CREATE"; NSString* const RNNotificationClearAction = @"CLEAR"; @implementation RNNotifications { - RNNotificationEventHandler* _notificationEventHandler; + RNNotificationCenterListener* _notificationCenterListener; } + (instancetype)sharedInstance { @@ -30,16 +29,17 @@ NSString* const RNNotificationClearAction = @"CLEAR"; } - (void)initialize { - _notificationEventHandler = [RNNotificationEventHandler new]; + RNNotificationEventHandler* notificationEventHandler = [RNNotificationEventHandler new]; + _notificationCenterListener = [[RNNotificationCenterListener alloc] initWithNotificationEventHandler:notificationEventHandler]; } - (void)didRegisterForRemoteNotificationsWithDeviceToken:(id)deviceToken { NSString *tokenRepresentation = [deviceToken isKindOfClass:[NSString class]] ? deviceToken : [RNUtils deviceTokenToString:deviceToken]; - [RNEventEmitter sendEvent:Registered body:@{@"deviceToken": tokenRepresentation}]; + [RNEventEmitter sendEvent:RNRegistered body:@{@"deviceToken": tokenRepresentation}]; } - (void)didFailToRegisterForRemoteNotificationsWithError:(NSError *)error { - [RNEventEmitter sendEvent:RegistrationFailed body:@{@"code": [NSNumber numberWithInteger:error.code], @"domain": error.domain, @"localizedDescription": error.localizedDescription}]; + [RNEventEmitter sendEvent:RNRegistrationFailed body:@{@"code": [NSNumber numberWithInteger:error.code], @"domain": error.domain, @"localizedDescription": error.localizedDescription}]; } - (void)setBadgeForNotification:(NSDictionary *)notification { diff --git a/RNNotifications/RNNotifications.xcodeproj/project.pbxproj b/RNNotifications/RNNotifications.xcodeproj/project.pbxproj index b5a6c3971378fead22602bd6acd5772908b3bd15..d6cc928efa40751f5417db009b2d77435cb20d99 100644 --- a/RNNotifications/RNNotifications.xcodeproj/project.pbxproj +++ b/RNNotifications/RNNotifications.xcodeproj/project.pbxproj @@ -13,7 +13,6 @@ 50351F9822CD8604000713B3 /* RNCommandsHandler.m in Sources */ = {isa = PBXBuildFile; fileRef = 50351F9722CD8604000713B3 /* RNCommandsHandler.m */; }; 507DCCF522CE3EBD005D4E0B /* RNNotifications.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = D8A2F7561CB57F28002CC8F5 /* RNNotifications.h */; }; 507DCCF722CE3EF7005D4E0B /* RNBridgeModule.h in Headers */ = {isa = PBXBuildFile; fileRef = 50351F9022CD7DF4000713B3 /* RNBridgeModule.h */; }; - 507DCCF822CE3F04005D4E0B /* RNNotificationsBridgeQueue.h in Headers */ = {isa = PBXBuildFile; fileRef = D85B37461CC05A1200DE9EB6 /* RNNotificationsBridgeQueue.h */; }; 507DCCF922CE3F04005D4E0B /* RNNotifications.h in Headers */ = {isa = PBXBuildFile; fileRef = D8A2F7561CB57F28002CC8F5 /* RNNotifications.h */; settings = {ATTRIBUTES = (Public, ); }; }; 507DCCFA22CE3F04005D4E0B /* RNEventEmitter.h in Headers */ = {isa = PBXBuildFile; fileRef = 50351F8D22CD782F000713B3 /* RNEventEmitter.h */; }; 507DCCFB22CE3F04005D4E0B /* RNCommandsHandler.h in Headers */ = {isa = PBXBuildFile; fileRef = 50351F9622CD8604000713B3 /* RNCommandsHandler.h */; }; @@ -34,7 +33,6 @@ 50AD1FCB22D13ADB00E12362 /* RNPushKitEventHandler.m in Sources */ = {isa = PBXBuildFile; fileRef = 50AD1FC922D13ADB00E12362 /* RNPushKitEventHandler.m */; }; 50E49F0722D1E4E0007160C1 /* RNNotificationsStore.h in Headers */ = {isa = PBXBuildFile; fileRef = 50E49F0522D1E4E0007160C1 /* RNNotificationsStore.h */; }; 50E49F0822D1E4E0007160C1 /* RNNotificationsStore.m in Sources */ = {isa = PBXBuildFile; fileRef = 50E49F0622D1E4E0007160C1 /* RNNotificationsStore.m */; }; - D85B37451CC05A0900DE9EB6 /* RNNotificationsBridgeQueue.m in Sources */ = {isa = PBXBuildFile; fileRef = D85B37441CC05A0900DE9EB6 /* RNNotificationsBridgeQueue.m */; }; D8A2F7551CB57F1A002CC8F5 /* RNNotifications.m in Sources */ = {isa = PBXBuildFile; fileRef = D8A2F7541CB57F1A002CC8F5 /* RNNotifications.m */; }; /* End PBXBuildFile section */ @@ -90,8 +88,6 @@ 50AD1FC922D13ADB00E12362 /* RNPushKitEventHandler.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RNPushKitEventHandler.m; sourceTree = ""; }; 50E49F0522D1E4E0007160C1 /* RNNotificationsStore.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RNNotificationsStore.h; sourceTree = ""; }; 50E49F0622D1E4E0007160C1 /* RNNotificationsStore.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RNNotificationsStore.m; sourceTree = ""; }; - D85B37441CC05A0900DE9EB6 /* RNNotificationsBridgeQueue.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RNNotificationsBridgeQueue.m; sourceTree = ""; }; - D85B37461CC05A1200DE9EB6 /* RNNotificationsBridgeQueue.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RNNotificationsBridgeQueue.h; sourceTree = ""; }; D8A2F7541CB57F1A002CC8F5 /* RNNotifications.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RNNotifications.m; sourceTree = ""; }; D8A2F7561CB57F28002CC8F5 /* RNNotifications.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RNNotifications.h; sourceTree = ""; }; /* End PBXFileReference section */ @@ -188,8 +184,6 @@ D8A2F7541CB57F1A002CC8F5 /* RNNotifications.m */, 50E49F0522D1E4E0007160C1 /* RNNotificationsStore.h */, 50E49F0622D1E4E0007160C1 /* RNNotificationsStore.m */, - D85B37461CC05A1200DE9EB6 /* RNNotificationsBridgeQueue.h */, - D85B37441CC05A0900DE9EB6 /* RNNotificationsBridgeQueue.m */, 50351F8D22CD782F000713B3 /* RNEventEmitter.h */, 50351F8E22CD782F000713B3 /* RNEventEmitter.m */, 50351F9322CD7FF1000713B3 /* RCTConvert+Notifications.h */, @@ -215,7 +209,6 @@ 508CE81922D130B900357815 /* RNPushKitEventListener.h in Headers */, 50E49F0722D1E4E0007160C1 /* RNNotificationsStore.h in Headers */, 508CE81622D12FF600357815 /* RNNotificationCenterListener.h in Headers */, - 507DCCF822CE3F04005D4E0B /* RNNotificationsBridgeQueue.h in Headers */, 508CE82222D1372E00357815 /* RNUtils.h in Headers */, 507DCCFA22CE3F04005D4E0B /* RNEventEmitter.h in Headers */, 507DCCFB22CE3F04005D4E0B /* RNCommandsHandler.h in Headers */, @@ -328,7 +321,6 @@ 508CE81A22D130B900357815 /* RNPushKitEventListener.m in Sources */, 50351F9222CD7DF4000713B3 /* RNBridgeModule.m in Sources */, 508CE81E22D1337200357815 /* RNPushKit.m in Sources */, - D85B37451CC05A0900DE9EB6 /* RNNotificationsBridgeQueue.m in Sources */, 508CE7D622D12CCA00357815 /* RNNotificationEventHandler.m in Sources */, 50E49F0822D1E4E0007160C1 /* RNNotificationsStore.m in Sources */, 508CE82322D1372E00357815 /* RNUtils.m in Sources */, diff --git a/RNNotifications/RNNotificationsBridgeQueue.h b/RNNotifications/RNNotificationsBridgeQueue.h deleted file mode 100644 index b6294a9641c0cd97fcb1214ae94ff831b53e28d6..0000000000000000000000000000000000000000 --- a/RNNotifications/RNNotificationsBridgeQueue.h +++ /dev/null @@ -1,17 +0,0 @@ -#import - -@interface RNNotificationsBridgeQueue : NSObject - -@property NSDictionary* openedRemoteNotification; - -+ (nonnull instancetype)sharedInstance; - -- (void)postAction:(NSDictionary *)action withCompletionKey:(NSString *)completionKey andCompletionHandler:(void (^)())completionHandler; -- (void)postNotification:(NSDictionary *)notification; - -- (void)consumeActionsQueue:(void (^)(NSDictionary *))block; -- (void)consumeNotificationsQueue:(void (^)(NSDictionary *))block; - -- (void)completeAction:(NSString *)completionKey; - -@end diff --git a/RNNotifications/RNNotificationsBridgeQueue.m b/RNNotifications/RNNotificationsBridgeQueue.m deleted file mode 100644 index 747a2995bdc741f20c2b677ace742401badf4a14..0000000000000000000000000000000000000000 --- a/RNNotifications/RNNotificationsBridgeQueue.m +++ /dev/null @@ -1,94 +0,0 @@ -#import "RNNotificationsBridgeQueue.h" - -@implementation RNNotificationsBridgeQueue - -NSMutableArray* actionsQueue; -NSMutableArray* notificationsQueue; -NSMutableDictionary* actionCompletionHandlers; - -+ (nonnull instancetype)sharedInstance { - static RNNotificationsBridgeQueue* sharedInstance = nil; - static dispatch_once_t onceToken; - dispatch_once(&onceToken, ^{ - sharedInstance = [self new]; - }); - - return sharedInstance; -} - -- (instancetype)init -{ - actionsQueue = [NSMutableArray new]; - notificationsQueue = [NSMutableArray new]; - actionCompletionHandlers = [NSMutableDictionary new]; - - return self; -} - -- (void)postNotification:(NSDictionary *)notification -{ - if (!notificationsQueue) return; - [notificationsQueue insertObject:notification atIndex:0]; -} - -- (NSDictionary *)dequeueSingleNotification -{ - if (!notificationsQueue || notificationsQueue.count == 0) return nil; - - NSDictionary* notification = [notificationsQueue lastObject]; - [notificationsQueue removeLastObject]; - - return notification; -} - -- (void)consumeNotificationsQueue:(void (^)(NSDictionary *))block -{ - NSDictionary* notification; - - while ((notification = [self dequeueSingleNotification]) != nil) { - block(notification); - } - - notificationsQueue = nil; -} - -- (void)postAction:(NSDictionary *)action withCompletionKey:(NSString *)completionKey andCompletionHandler:(void (^)())completionHandler -{ - // store completion handler - actionCompletionHandlers[completionKey] = completionHandler; - - if (!actionsQueue) return; - [actionsQueue insertObject:action atIndex:0]; -} - -- (NSDictionary *)dequeueSingleAction -{ - if (!actionsQueue || actionsQueue.count == 0) return nil; - - NSDictionary* action = [actionsQueue lastObject]; - [actionsQueue removeLastObject]; - - return action; -} - -- (void)consumeActionsQueue:(void (^)(NSDictionary *))block -{ - NSDictionary* lastActionInfo; - - while ((lastActionInfo = [self dequeueSingleAction]) != nil) { - block(lastActionInfo); - } - - actionsQueue = nil; -} - -- (void)completeAction:(NSString *)completionKey -{ - void (^completionHandler)() = (void (^)())[actionCompletionHandlers valueForKey:completionKey]; - if (completionHandler) { - completionHandler(); - [actionCompletionHandlers removeObjectForKey:completionKey]; - } -} - -@end diff --git a/RNNotifications/RNNotificationsStore.h b/RNNotifications/RNNotificationsStore.h index 9e4c19178b65cfa0128c0370b115dc835e55eb9d..9babe03889067eac72177fb81fd9d87e84db577f 100644 --- a/RNNotifications/RNNotificationsStore.h +++ b/RNNotifications/RNNotificationsStore.h @@ -2,4 +2,9 @@ @interface RNNotificationsStore : NSObject +@property NSDictionary* initialNotification; + +- (void)completeAction:(NSString *)completionKey; +- (void)setCompletionHandler:(void (^)())completionHandler withCompletionKey:(NSString *)completionKey; + @end diff --git a/RNNotifications/RNNotificationsStore.m b/RNNotifications/RNNotificationsStore.m index 164f0715ed6f5dbd2da50e05d9a22f3fffa914a1..e7f879279b23c005b12b7a9aa97be32f30c383ea 100644 --- a/RNNotifications/RNNotificationsStore.m +++ b/RNNotifications/RNNotificationsStore.m @@ -1,13 +1,24 @@ #import "RNNotificationsStore.h" @implementation RNNotificationsStore { - NSMutableDictionary* actionCompletionHandlers; + NSMutableDictionary* _actionCompletionHandlers; } - (instancetype)init { - actionCompletionHandlers = [NSMutableDictionary new]; - + self = [super init]; + _actionCompletionHandlers = [NSMutableDictionary new]; return self; } +- (void)setCompletionHandler:(void (^)())completionHandler withCompletionKey:(NSString *)completionKey { + _actionCompletionHandlers[completionKey] = completionHandler; +} + +- (void)completeAction:(NSString *)completionKey { + void (^completionHandler)() = (void (^)())[_actionCompletionHandlers valueForKey:completionKey]; + if (completionHandler) { + completionHandler(); + [_actionCompletionHandlers removeObjectForKey:completionKey]; + } +} @end diff --git a/RNNotifications/RNPushKitEventHandler.m b/RNNotifications/RNPushKitEventHandler.m index e44e6d079aed95bed14b6c4aa98998e66569ce0d..ed27d83adf5af61079f2a1663107c11c25b4d884 100644 --- a/RNNotifications/RNPushKitEventHandler.m +++ b/RNNotifications/RNPushKitEventHandler.m @@ -4,7 +4,7 @@ @implementation RNPushKitEventHandler - (void)registeredWithToken:(NSString *)token { - [RNEventEmitter sendEvent:PushKitRegistered body:@{@"pushKitToken": token}]; + [RNEventEmitter sendEvent:RNPushKitRegistered body:@{@"pushKitToken": token}]; } @end diff --git a/e2e/BottomTabs.test.js b/e2e/BottomTabs.test.js deleted file mode 100644 index 48ad6c39505f398b7f256b2c2b7aa8a505ad4974..0000000000000000000000000000000000000000 --- a/e2e/BottomTabs.test.js +++ /dev/null @@ -1,15 +0,0 @@ -const Utils = require('./Utils'); -const { elementByLabel, elementById, sleep } = Utils; -describe('BottomTabs', () => { - beforeEach(async () => { - await device.relaunchApp(); - // await elementById(TestIDs.BOTTOM_TABS_BTN).tap(); - // await expect(elementByLabel('First Tab')).toBeVisible(); - }); - - it('switch to tab by index', async () => { - // await elementById(TestIDs.SWITCH_TAB_BY_INDEX_BTN).tap(); - await expect(elementByLabel('First Tab')).toBeNotVisible(); - await expect(elementByLabel('Second Tab')).toBeVisible(); - }); -}); diff --git a/e2e/Notifications.test.js b/e2e/Notifications.test.js new file mode 100644 index 0000000000000000000000000000000000000000..71b15ecb5e442353689515f4efe2b02bb29efd3f --- /dev/null +++ b/e2e/Notifications.test.js @@ -0,0 +1,39 @@ +const Utils = require('./Utils'); +const { elementByLabel, elementById } = Utils; + +describe('Notifications', () => { + beforeEach(async () => { + await device.relaunchApp({permissions: {notifications: 'YES'}}); + }); + + it('Receive foreground notification', async () => { + await device.sendUserNotification(getNotification('explicit/external/link/test_parameter')); + // await device.launchApp({newInstance: true, userNotification: getNotification('unknown/link', 'test/category', {parameter: 'test_body_param'})}); + // await elementById(TestIDs.SWITCH_TAB_BY_INDEX_BTN).tap(); + // await expect(elementByLabel('First Tab')).toBeNotVisible(); + // await expect(elementByLabel('Second Tab')).toBeVisible(); + }); +}); + +function getNotification(link, category = 'com.example.category', params = {}) { + return { + trigger: { + type: 'push' + }, + title: 'From push', + subtitle: 'Subtitle', + body: 'Body', + badge: 1, + payload: { + appId: '14517e1a-3ff0-af98-408e-2bd6953c36a2', + aps: { + alert: 'this is alert', + sound: 'chime.aiff' + }, + category, + link, ...params + }, + 'content-available': 0, + 'action-identifier': 'default' + }; +} \ No newline at end of file diff --git a/scripts/Utils.js b/e2e/Utils.js similarity index 100% rename from scripts/Utils.js rename to e2e/Utils.js diff --git a/example/index.ios.js b/example/index.ios.js index 3370901c8493107a61c5121a52f997516d55f66d..e797c3c83306ed3e5bb3c7fb5e4cd83fbc1e5a49 100644 --- a/example/index.ios.js +++ b/example/index.ios.js @@ -1,13 +1,9 @@ -/** - * Sample React Native App - * https://github.com/facebook/react-native - */ - import { AppRegistry, StyleSheet, View, - Text + Text, + Button } from 'react-native'; import React, {Component} from 'react'; @@ -37,18 +33,15 @@ let replyAction = new NotificationAction({ completed(); }); -let cat = new NotificationCategory({ - identifier: 'SOME_CATEGORY', - actions: [upvoteAction, replyAction], - context: 'default' -}); - class NotificationsExampleApp extends Component { constructor() { super(); + this.state = { + notifications: [] + }; + NotificationsIOS.addEventListener('remoteNotificationsRegistered', this.onPushRegistered.bind(this)); - NotificationsIOS.requestPermissions([cat]); NotificationsIOS.consumeBackgroundQueue(); @@ -56,7 +49,6 @@ class NotificationsExampleApp extends Component { NotificationsIOS.registerPushKit(); NotificationsIOS.addEventListener('notificationReceivedForeground', this.onNotificationReceivedForeground.bind(this)); - NotificationsIOS.addEventListener('notificationReceivedBackground', this.onNotificationReceivedBackground.bind(this)); NotificationsIOS.addEventListener('notificationOpened', this.onNotificationOpened.bind(this)); } @@ -69,54 +61,48 @@ class NotificationsExampleApp extends Component { } onNotificationReceivedForeground(notification) { + alert(JSON.stringify(notification)); console.log('Notification Received Foreground: ' + JSON.stringify(notification)); - } - - onNotificationReceivedBackground(notification) { - NotificationsIOS.log('Notification Received Background: ' + JSON.stringify(notification)); - - let localNotification = NotificationsIOS.localNotification({ - alertBody: 'Received background notificiation!', - alertTitle: 'Local Notification Title', - alertAction: 'Click here to open', - soundName: 'chime.aiff', - category: 'SOME_CATEGORY', - userInfo: notification.getData() + this.setState({ + notifications: [...this.state.notifications, notification] }); - - // if you want to fire the local notification 10 seconds later, - // add the following line to the notification payload: - // fireDate: new Date(Date.now() + (10 * 1000)).toISOString() - - // NotificationsIOS.backgroundTimeRemaining(time => NotificationsIOS.log('remaining background time: ' + time)); - - // NotificationsIOS.cancelLocalNotification(localNotification); } onNotificationOpened(notification) { console.log('Notification Opened: ' + JSON.stringify(notification)); } + renderNotification(notification) { + return {`${''} | ${JSON.stringify(notification)}`}; + } + render() { + const notifications = this.state.notifications.map((notification, idx) => + ( + + {this.renderNotification(notification)} + + )); + return ( - - Welcome to React Native Notifications Demo App! - - - To get started, edit index.ios.js - - - Press Cmd+R to reload,{'\n'} - Cmd+D or shake for dev menu - +