From df53f390fd1d258f80a1a7324f107257867e3f36 Mon Sep 17 00:00:00 2001 From: yogevbd Date: Mon, 8 Jul 2019 01:31:51 +0300 Subject: [PATCH] WIP --- RNNotifications/RNBridgeModule.m | 7 +--- RNNotifications/RNCommandsHandler.h | 2 - RNNotifications/RNCommandsHandler.m | 11 +---- RNNotifications/RNEventEmitter.h | 10 ++--- RNNotifications/RNEventEmitter.m | 42 ++++++++++++------- RNNotifications/RNNotificationEventHandler.h | 1 + RNNotifications/RNNotifications.h | 3 ++ RNNotifications/RNNotifications.m | 18 +++++++- .../RNNotifications.xcodeproj/project.pbxproj | 12 ------ RNNotifications/RNPushKitEventListener.m | 1 - example/index.ios.js | 5 +++ .../ios/NotificationsExampleApp/AppDelegate.h | 4 +- .../ios/NotificationsExampleApp/AppDelegate.m | 13 +++--- .../NotificationsExampleApp.entitlements | 8 ++++ 14 files changed, 76 insertions(+), 61 deletions(-) create mode 100644 example/ios/NotificationsExampleApp/NotificationsExampleApp.entitlements diff --git a/RNNotifications/RNBridgeModule.m b/RNNotifications/RNBridgeModule.m index e8e595c..9850b49 100644 --- a/RNNotifications/RNBridgeModule.m +++ b/RNNotifications/RNBridgeModule.m @@ -1,12 +1,10 @@ #import "RNBridgeModule.h" #import "RNCommandsHandler.h" #import "RCTConvert+Notifications.h" -#import "RNEventEmitter.h" #import "RNNotifications.h" -#import "RNNotificationsStore.h" +#import @implementation RNBridgeModule { - RNNotificationsStore* _store; RNCommandsHandler* _commandsHandler; } @@ -16,7 +14,6 @@ RCT_EXPORT_MODULE(); - (instancetype)init { self = [super init]; - _store = [RNNotificationsStore new]; _commandsHandler = [[RNCommandsHandler alloc] init]; return self; } @@ -31,7 +28,7 @@ RCT_EXPORT_MODULE(); - (void)setBridge:(RCTBridge *)bridge { _bridge = bridge; - _store.initialNotification = [_bridge.launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]; + [[RNNotifications sharedInstance] setInitialNotification:[_bridge.launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]]; } #pragma mark - JS interface diff --git a/RNNotifications/RNCommandsHandler.h b/RNNotifications/RNCommandsHandler.h index bb82398..1ce85e9 100644 --- a/RNNotifications/RNCommandsHandler.h +++ b/RNNotifications/RNCommandsHandler.h @@ -4,8 +4,6 @@ @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 2969d30..f2b9538 100644 --- a/RNNotifications/RNCommandsHandler.m +++ b/RNNotifications/RNCommandsHandler.m @@ -5,13 +5,6 @@ @implementation RNCommandsHandler { RNPushKit* _pushKit; - RNNotificationsStore* _store; -} - -- (instancetype)initWithStore:(RNNotificationsStore *)store { - self = [super init]; - _store = store; - return self; } - (void)requestPermissionsWithCategories:(NSArray *)json { @@ -45,11 +38,11 @@ } - (void)getInitialNotification:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject { - resolve(_store.initialNotification); + resolve([[RNNotifications sharedInstance] initialNotification]); } - (void)completionHandler:(NSString *)completionKey { - [_store completeAction:completionKey]; + [[RNNotifications sharedInstance] finishHandleNotificationKey:completionKey]; } - (void)abandonPermissions { diff --git a/RNNotifications/RNEventEmitter.h b/RNNotifications/RNEventEmitter.h index a67da2f..33e8db7 100644 --- a/RNNotifications/RNEventEmitter.h +++ b/RNNotifications/RNEventEmitter.h @@ -1,7 +1,7 @@ -#import "RCTEventEmitter.h" +#import -static NSString* const RNRegistered = @"notificationsRegistered"; -static NSString* const RNRegistrationFailed = @"notificationsRegistrationFailed"; +static NSString* const RNRegistered = @"remoteNotificationsRegistered"; +static NSString* const RNRegistrationFailed = @"remoteNotificationsRegistrationFailed"; static NSString* const RNPushKitRegistered = @"pushKitRegistered"; static NSString* const RNNotificationReceivedForeground = @"notificationReceivedForeground"; static NSString* const RNNotificationReceivedBackground = @"notificationReceivedBackground"; @@ -9,9 +9,7 @@ static NSString* const RNNotificationOpened = @"notificationOpened"; static NSString* const RNActionTriggered = @"notificationActionTriggered"; -@interface RNEventEmitter : RCTEventEmitter - -+ (instancetype)sharedInstance; +@interface RNEventEmitter : RCTEventEmitter + (void)sendEvent:(NSString *)event body:(NSDictionary *)body; diff --git a/RNNotifications/RNEventEmitter.m b/RNNotifications/RNEventEmitter.m index 8045bae..695bca0 100644 --- a/RNNotifications/RNEventEmitter.m +++ b/RNNotifications/RNEventEmitter.m @@ -14,30 +14,40 @@ RCT_EXPORT_MODULE(); RNActionTriggered]; } -# pragma mark public - -+ (instancetype)sharedInstance { - static RNEventEmitter *sharedInstance = nil; - static dispatch_once_t onceToken; - - dispatch_once(&onceToken, ^{ - sharedInstance = [[RNEventEmitter alloc] init]; - }); - return sharedInstance; +- (instancetype)init { + self = [super init]; + for (NSString *event in [self supportedEvents]) { + [self addListener:event]; + } + return self; } +# pragma mark public + + (void)sendEvent:(NSString *)event body:(NSDictionary *)body { - [[self sharedInstance] send:event body:body]; + [[NSNotificationCenter defaultCenter] postNotificationName:event + object:self + userInfo:body]; } - # pragma mark private -- (void)send:(NSString *)eventName body:(id)body { - if (self.bridge == nil) { - return; +- (void)startObserving { + for (NSString *event in [self supportedEvents]) { + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(handleNotification:) + name:event + object:nil]; } - [self sendEventWithName:eventName body:body]; } +- (void)stopObserving { + [[NSNotificationCenter defaultCenter] removeObserver:self]; +} + +- (void)handleNotification:(NSNotification *)notification { + [self sendEventWithName:notification.name body:notification.userInfo]; +} + + @end diff --git a/RNNotifications/RNNotificationEventHandler.h b/RNNotifications/RNNotificationEventHandler.h index a7f06f1..7b6cc0e 100644 --- a/RNNotifications/RNNotificationEventHandler.h +++ b/RNNotifications/RNNotificationEventHandler.h @@ -1,6 +1,7 @@ #import @import UserNotifications; #import "RNNotificationsStore.h" +#import "RNEventEmitter.h" @interface RNNotificationEventHandler : NSObject diff --git a/RNNotifications/RNNotifications.h b/RNNotifications/RNNotifications.h index 88db497..1945ade 100644 --- a/RNNotifications/RNNotifications.h +++ b/RNNotifications/RNNotifications.h @@ -4,11 +4,14 @@ @interface RNNotifications : NSObject +@property (nonatomic, retain) NSDictionary* initialNotification; + + (instancetype)sharedInstance; - (void)initialize; - (void)didRegisterForRemoteNotificationsWithDeviceToken:(id)deviceToken; - (void)didFailToRegisterForRemoteNotificationsWithError:(NSError *)error; +- (void)finishHandleNotificationKey:(NSString *)notificationKey; //- (void)setBadgeForNotification:(NSDictionary *)notification; diff --git a/RNNotifications/RNNotifications.m b/RNNotifications/RNNotifications.m index 180c66b..90e76ae 100644 --- a/RNNotifications/RNNotifications.m +++ b/RNNotifications/RNNotifications.m @@ -8,6 +8,14 @@ @implementation RNNotifications { RNNotificationCenterListener* _notificationCenterListener; RNNotificationEventHandler* _notificationEventHandler; + RNEventEmitter* _eventEmitter; + RNNotificationsStore* _store; +} + +- (instancetype)init { + self = [super init]; + _store = [RNNotificationsStore new]; + return self; } + (instancetype)sharedInstance { @@ -21,7 +29,7 @@ } - (void)initialize { - _notificationEventHandler = [RNNotificationEventHandler new]; + _notificationEventHandler = [[RNNotificationEventHandler alloc] initWithStore:_store]; _notificationCenterListener = [[RNNotificationCenterListener alloc] initWithNotificationEventHandler:_notificationEventHandler]; } @@ -39,4 +47,12 @@ } } +- (void)setInitialNotification:(NSDictionary *)notification { + [_store setInitialNotification:notification]; +} + +- (void)finishHandleNotificationKey:(NSString *)notificationKey { + [_store completeAction:notificationKey]; +} + @end diff --git a/RNNotifications/RNNotifications.xcodeproj/project.pbxproj b/RNNotifications/RNNotifications.xcodeproj/project.pbxproj index 08f319f..31e9b6b 100644 --- a/RNNotifications/RNNotifications.xcodeproj/project.pbxproj +++ b/RNNotifications/RNNotifications.xcodeproj/project.pbxproj @@ -69,8 +69,6 @@ 50351F9422CD7FF1000713B3 /* RCTConvert+Notifications.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "RCTConvert+Notifications.m"; sourceTree = ""; }; 50351F9622CD8604000713B3 /* RNCommandsHandler.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RNCommandsHandler.h; sourceTree = ""; }; 50351F9722CD8604000713B3 /* RNCommandsHandler.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RNCommandsHandler.m; sourceTree = ""; }; - 508CE7BB22D12B0A00357815 /* Tests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = Tests.m; sourceTree = ""; }; - 508CE7BD22D12B0A00357815 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 508CE7C822D12B2600357815 /* RNNotificationsTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = RNNotificationsTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 508CE7CA22D12B2600357815 /* RNNotificationsTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RNNotificationsTests.m; sourceTree = ""; }; 508CE7CC22D12B2600357815 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; @@ -119,15 +117,6 @@ name = Products; sourceTree = ""; }; - 508CE7BA22D12B0A00357815 /* Tests */ = { - isa = PBXGroup; - children = ( - 508CE7BB22D12B0A00357815 /* Tests.m */, - 508CE7BD22D12B0A00357815 /* Info.plist */, - ); - path = Tests; - sourceTree = ""; - }; 508CE7C922D12B2600357815 /* RNNotificationsTests */ = { isa = PBXGroup; children = ( @@ -196,7 +185,6 @@ D8A2F7541CB57F1A002CC8F5 /* RNNotifications.m */, 50351F9622CD8604000713B3 /* RNCommandsHandler.h */, 50351F9722CD8604000713B3 /* RNCommandsHandler.m */, - 508CE7BA22D12B0A00357815 /* Tests */, 508CE7C922D12B2600357815 /* RNNotificationsTests */, 134814211AA4EA7D00B7C361 /* Products */, 508CE7C822D12B2600357815 /* RNNotificationsTests.xctest */, diff --git a/RNNotifications/RNPushKitEventListener.m b/RNNotifications/RNPushKitEventListener.m index f9548ba..316ffaa 100644 --- a/RNNotifications/RNPushKitEventListener.m +++ b/RNNotifications/RNPushKitEventListener.m @@ -1,5 +1,4 @@ #import "RNPushKitEventListener.h" -#import "RNEventEmitter.h" #import "RNUtils.h" @implementation RNPushKitEventListener { diff --git a/example/index.ios.js b/example/index.ios.js index 7115e16..da2d5a7 100644 --- a/example/index.ios.js +++ b/example/index.ios.js @@ -42,6 +42,7 @@ class NotificationsExampleApp extends Component { }; NotificationsIOS.addEventListener('remoteNotificationsRegistered', this.onPushRegistered.bind(this)); + NotificationsIOS.addEventListener('remoteNotificationsRegistrationFailed', this.onPushRegisteredFailed.bind(this)); NotificationsIOS.consumeBackgroundQueue(); @@ -56,6 +57,10 @@ class NotificationsExampleApp extends Component { console.log('Device Token Received: ' + deviceToken); } + onPushRegisteredFailed(error) { + console.log('Remote notifiction registration failed: ' + error); + } + onPushKitRegistered(deviceToken) { console.log('PushKit Token Received: ' + deviceToken); } diff --git a/example/ios/NotificationsExampleApp/AppDelegate.h b/example/ios/NotificationsExampleApp/AppDelegate.h index 1f29849..a9654d5 100644 --- a/example/ios/NotificationsExampleApp/AppDelegate.h +++ b/example/ios/NotificationsExampleApp/AppDelegate.h @@ -9,9 +9,7 @@ #import -#import - -@interface AppDelegate : UIResponder +@interface AppDelegate : UIResponder @property (nonatomic, strong) UIWindow *window; diff --git a/example/ios/NotificationsExampleApp/AppDelegate.m b/example/ios/NotificationsExampleApp/AppDelegate.m index 9262ad4..be2acd1 100644 --- a/example/ios/NotificationsExampleApp/AppDelegate.m +++ b/example/ios/NotificationsExampleApp/AppDelegate.m @@ -8,8 +8,7 @@ @implementation AppDelegate -- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions -{ +- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { NSURL *jsCodeLocation; jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil]; @@ -32,14 +31,16 @@ } // Required to register for notifications -- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings -{ +- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings { // [[RNNotifications sharedInstance] didRegisterUserNotificationSettings:notificationSettings]; } -- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken -{ +- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { [[RNNotifications sharedInstance] didRegisterForRemoteNotificationsWithDeviceToken:deviceToken]; } +- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error { + [[RNNotifications sharedInstance] didFailToRegisterForRemoteNotificationsWithError:error]; +} + @end diff --git a/example/ios/NotificationsExampleApp/NotificationsExampleApp.entitlements b/example/ios/NotificationsExampleApp/NotificationsExampleApp.entitlements new file mode 100644 index 0000000..903def2 --- /dev/null +++ b/example/ios/NotificationsExampleApp/NotificationsExampleApp.entitlements @@ -0,0 +1,8 @@ + + + + + aps-environment + development + + -- 2.26.2