diff --git a/RNNotifications/RNCommandsHandler.m b/RNNotifications/RNCommandsHandler.m index 1161d3e935e3c3d41ebbf40e8b0281d5c502753f..2969d30331ef818f0085328e5eaf1ab330b816fa 100644 --- a/RNNotifications/RNCommandsHandler.m +++ b/RNNotifications/RNCommandsHandler.m @@ -11,7 +11,6 @@ - (instancetype)initWithStore:(RNNotificationsStore *)store { self = [super init]; _store = store; - return self; } diff --git a/RNNotifications/RNEventEmitter.h b/RNNotifications/RNEventEmitter.h index 9b13075a06dc9f060c581c43ef4dd1989d8cd05a..a67da2fc6a5af7eab016f063c0b9e82631743ead 100644 --- a/RNNotifications/RNEventEmitter.h +++ b/RNNotifications/RNEventEmitter.h @@ -1,12 +1,12 @@ #import "RCTEventEmitter.h" -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"; +static NSString* const RNRegistered = @"notificationsRegistered"; +static NSString* const RNRegistrationFailed = @"notificationsRegistrationFailed"; +static NSString* const RNPushKitRegistered = @"pushKitRegistered"; +static NSString* const RNNotificationReceivedForeground = @"notificationReceivedForeground"; +static NSString* const RNNotificationReceivedBackground = @"notificationReceivedBackground"; +static NSString* const RNNotificationOpened = @"notificationOpened"; +static NSString* const RNActionTriggered = @"notificationActionTriggered"; @interface RNEventEmitter : RCTEventEmitter diff --git a/RNNotifications/RNNotificationEventHandler.h b/RNNotifications/RNNotificationEventHandler.h index bf56c2184e3f00818387bc2105d36e31306c8a5d..a7f06f18b7c16e1d0ba98906c090a077c8abe3ac 100644 --- a/RNNotifications/RNNotificationEventHandler.h +++ b/RNNotifications/RNNotificationEventHandler.h @@ -6,6 +6,9 @@ - (instancetype)initWithStore:(RNNotificationsStore *)store; +- (void)didRegisterForRemoteNotificationsWithDeviceToken:(id)deviceToken; +- (void)didFailToRegisterForRemoteNotificationsWithError:(NSError *)error; + - (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 58cdebc7178c61cdc3b18269a5bf784c1b5ca15c..48f03c269f84b6c27d7d98eb4575035841e1306b 100644 --- a/RNNotifications/RNNotificationEventHandler.m +++ b/RNNotifications/RNNotificationEventHandler.m @@ -1,5 +1,6 @@ #import "RNNotificationEventHandler.h" #import "RNEventEmitter.h" +#import "RNUtils.h" @implementation RNNotificationEventHandler { RNNotificationsStore* _store; @@ -8,10 +9,18 @@ - (instancetype)initWithStore:(RNNotificationsStore *)store { self = [super init]; _store = store; - return self; } +- (void)didRegisterForRemoteNotificationsWithDeviceToken:(id)deviceToken { + NSString *tokenRepresentation = [deviceToken isKindOfClass:[NSString class]] ? deviceToken : [RNUtils deviceTokenToString:deviceToken]; + [RNEventEmitter sendEvent:RNRegistered body:@{@"deviceToken": tokenRepresentation}]; +} + +- (void)didFailToRegisterForRemoteNotificationsWithError:(NSError *)error { + [RNEventEmitter sendEvent:RNRegistrationFailed body:@{@"code": [NSNumber numberWithInteger:error.code], @"domain": error.domain, @"localizedDescription": error.localizedDescription}]; +} + - (void)didReceiveForegroundPayload:(NSDictionary *)payload { [RNEventEmitter sendEvent:RNNotificationReceivedForeground body:payload]; } diff --git a/RNNotifications/RNNotifications.m b/RNNotifications/RNNotifications.m index 20772d205e0f5b438bd656d2eccb23976a2bdddd..180c66b7cfc0bec00d451cc4d783b4915505466c 100644 --- a/RNNotifications/RNNotifications.m +++ b/RNNotifications/RNNotifications.m @@ -2,20 +2,12 @@ #import #import #import -#import #import "RNNotifications.h" -#import -#import -#import -#import "RNEventEmitter.h" #import "RNNotificationCenterListener.h" -#import "RNUtils.h" - -NSString* const RNNotificationCreateAction = @"CREATE"; -NSString* const RNNotificationClearAction = @"CLEAR"; @implementation RNNotifications { RNNotificationCenterListener* _notificationCenterListener; + RNNotificationEventHandler* _notificationEventHandler; } + (instancetype)sharedInstance { @@ -29,17 +21,16 @@ NSString* const RNNotificationClearAction = @"CLEAR"; } - (void)initialize { - RNNotificationEventHandler* notificationEventHandler = [RNNotificationEventHandler new]; - _notificationCenterListener = [[RNNotificationCenterListener alloc] initWithNotificationEventHandler:notificationEventHandler]; + _notificationEventHandler = [RNNotificationEventHandler new]; + _notificationCenterListener = [[RNNotificationCenterListener alloc] initWithNotificationEventHandler:_notificationEventHandler]; } - (void)didRegisterForRemoteNotificationsWithDeviceToken:(id)deviceToken { - NSString *tokenRepresentation = [deviceToken isKindOfClass:[NSString class]] ? deviceToken : [RNUtils deviceTokenToString:deviceToken]; - [RNEventEmitter sendEvent:RNRegistered body:@{@"deviceToken": tokenRepresentation}]; + [_notificationEventHandler didRegisterForRemoteNotificationsWithDeviceToken:deviceToken]; } - (void)didFailToRegisterForRemoteNotificationsWithError:(NSError *)error { - [RNEventEmitter sendEvent:RNRegistrationFailed body:@{@"code": [NSNumber numberWithInteger:error.code], @"domain": error.domain, @"localizedDescription": error.localizedDescription}]; + [_notificationEventHandler didFailToRegisterForRemoteNotificationsWithError:error]; } - (void)setBadgeForNotification:(NSDictionary *)notification {