diff --git a/RNNotifications/RNBridgeModule.m b/RNNotifications/RNBridgeModule.m index e8e595c8864b4166037c4e41410a8724171f7103..9850b49ec92cf8716ff7e9afd9e3032ea653295e 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 bb82398e524ac326847d28e14d13431e89c5a9dd..1ce85e90f25830a347fdcf04226c0441d0fb6a4f 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 2969d30331ef818f0085328e5eaf1ab330b816fa..f2b95380d808a05c3482a3bda24044da3e799d6f 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 a67da2fc6a5af7eab016f063c0b9e82631743ead..33e8db75df7c1483536459364a86670de67abb38 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 8045bae525e0ff8513da2c70babdebb9c3e1a5e0..695bca03413a10c3969a7b98557de908eadcc856 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 a7f06f18b7c16e1d0ba98906c090a077c8abe3ac..7b6cc0e7c254b74485a249d439e262621dbe7cad 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 88db4973e51b9fdb6cc1356818b171ad48376581..1945adeed65169398d4c44cb2b61a7eb33107605 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 180c66b7cfc0bec00d451cc4d783b4915505466c..90e76aef713ec9d1f5a63808df5f25ec0157d99c 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 08f319ff9caa43cf515d07b2eb1d734d13d96b29..31e9b6b6460aa998a0bdb5716642d0cbbceba11f 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 f9548baebd41ef13dcab260860091e5d295bf840..316ffaa4cdf7733489612d29d9e663ab28c88f82 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 7115e16028d2dd6a9920c014340ac9e934c000d6..da2d5a713cdc9ab9fe5b3749a220241661a01e8f 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 1f298493ac1c6395a3e7927ed4dd7a9aa25dc5cc..a9654d5e01b18c52fc334bdec2a796ce7e055dbf 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 9262ad48e1be9db0c972e7d869b8c4d720f7547c..be2acd1c38199699818416b133e46103cc2ceded 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 0000000000000000000000000000000000000000..903def2af53062463744294d5ad4c4ef8d9a4381 --- /dev/null +++ b/example/ios/NotificationsExampleApp/NotificationsExampleApp.entitlements @@ -0,0 +1,8 @@ + + + + + aps-environment + development + +