From e819748f54e8c7c4c0ea2361c374ff178298e0fe Mon Sep 17 00:00:00 2001 From: yogevbd Date: Sun, 7 Jul 2019 19:50:18 +0300 Subject: [PATCH] WIP --- RNNotifications/RNCommandsHandler.m | 1 - RNNotifications/RNEventEmitter.h | 14 +++++++------- RNNotifications/RNNotificationEventHandler.h | 3 +++ RNNotifications/RNNotificationEventHandler.m | 11 ++++++++++- RNNotifications/RNNotifications.m | 19 +++++-------------- 5 files changed, 25 insertions(+), 23 deletions(-) diff --git a/RNNotifications/RNCommandsHandler.m b/RNNotifications/RNCommandsHandler.m index 1161d3e..2969d30 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 9b13075..a67da2f 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 bf56c21..a7f06f1 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 58cdebc..48f03c2 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 20772d2..180c66b 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 { -- 2.26.2