Commit df53f390 authored by yogevbd's avatar yogevbd

WIP

parent 961f9a8b
#import "RNBridgeModule.h" #import "RNBridgeModule.h"
#import "RNCommandsHandler.h" #import "RNCommandsHandler.h"
#import "RCTConvert+Notifications.h" #import "RCTConvert+Notifications.h"
#import "RNEventEmitter.h"
#import "RNNotifications.h" #import "RNNotifications.h"
#import "RNNotificationsStore.h" #import <React/RCTBridgeDelegate.h>
@implementation RNBridgeModule { @implementation RNBridgeModule {
RNNotificationsStore* _store;
RNCommandsHandler* _commandsHandler; RNCommandsHandler* _commandsHandler;
} }
...@@ -16,7 +14,6 @@ RCT_EXPORT_MODULE(); ...@@ -16,7 +14,6 @@ RCT_EXPORT_MODULE();
- (instancetype)init { - (instancetype)init {
self = [super init]; self = [super init];
_store = [RNNotificationsStore new];
_commandsHandler = [[RNCommandsHandler alloc] init]; _commandsHandler = [[RNCommandsHandler alloc] init];
return self; return self;
} }
...@@ -31,7 +28,7 @@ RCT_EXPORT_MODULE(); ...@@ -31,7 +28,7 @@ RCT_EXPORT_MODULE();
- (void)setBridge:(RCTBridge *)bridge { - (void)setBridge:(RCTBridge *)bridge {
_bridge = bridge; _bridge = bridge;
_store.initialNotification = [_bridge.launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]; [[RNNotifications sharedInstance] setInitialNotification:[_bridge.launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]];
} }
#pragma mark - JS interface #pragma mark - JS interface
......
...@@ -4,8 +4,6 @@ ...@@ -4,8 +4,6 @@
@interface RNCommandsHandler : NSObject @interface RNCommandsHandler : NSObject
- (instancetype)initWithStore:(RNNotificationsStore *)store;
- (void)requestPermissionsWithCategories:(NSArray *)json; - (void)requestPermissionsWithCategories:(NSArray *)json;
- (void)getInitialNotification:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject; - (void)getInitialNotification:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject;
......
...@@ -5,13 +5,6 @@ ...@@ -5,13 +5,6 @@
@implementation RNCommandsHandler { @implementation RNCommandsHandler {
RNPushKit* _pushKit; RNPushKit* _pushKit;
RNNotificationsStore* _store;
}
- (instancetype)initWithStore:(RNNotificationsStore *)store {
self = [super init];
_store = store;
return self;
} }
- (void)requestPermissionsWithCategories:(NSArray *)json { - (void)requestPermissionsWithCategories:(NSArray *)json {
...@@ -45,11 +38,11 @@ ...@@ -45,11 +38,11 @@
} }
- (void)getInitialNotification:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject { - (void)getInitialNotification:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject {
resolve(_store.initialNotification); resolve([[RNNotifications sharedInstance] initialNotification]);
} }
- (void)completionHandler:(NSString *)completionKey { - (void)completionHandler:(NSString *)completionKey {
[_store completeAction:completionKey]; [[RNNotifications sharedInstance] finishHandleNotificationKey:completionKey];
} }
- (void)abandonPermissions { - (void)abandonPermissions {
......
#import "RCTEventEmitter.h" #import <React/RCTEventEmitter.h>
static NSString* const RNRegistered = @"notificationsRegistered"; static NSString* const RNRegistered = @"remoteNotificationsRegistered";
static NSString* const RNRegistrationFailed = @"notificationsRegistrationFailed"; static NSString* const RNRegistrationFailed = @"remoteNotificationsRegistrationFailed";
static NSString* const RNPushKitRegistered = @"pushKitRegistered"; static NSString* const RNPushKitRegistered = @"pushKitRegistered";
static NSString* const RNNotificationReceivedForeground = @"notificationReceivedForeground"; static NSString* const RNNotificationReceivedForeground = @"notificationReceivedForeground";
static NSString* const RNNotificationReceivedBackground = @"notificationReceivedBackground"; static NSString* const RNNotificationReceivedBackground = @"notificationReceivedBackground";
...@@ -9,9 +9,7 @@ static NSString* const RNNotificationOpened = @"notificationOpened"; ...@@ -9,9 +9,7 @@ static NSString* const RNNotificationOpened = @"notificationOpened";
static NSString* const RNActionTriggered = @"notificationActionTriggered"; static NSString* const RNActionTriggered = @"notificationActionTriggered";
@interface RNEventEmitter : RCTEventEmitter @interface RNEventEmitter : RCTEventEmitter <RCTBridgeModule>
+ (instancetype)sharedInstance;
+ (void)sendEvent:(NSString *)event body:(NSDictionary *)body; + (void)sendEvent:(NSString *)event body:(NSDictionary *)body;
......
...@@ -14,30 +14,40 @@ RCT_EXPORT_MODULE(); ...@@ -14,30 +14,40 @@ RCT_EXPORT_MODULE();
RNActionTriggered]; RNActionTriggered];
} }
# pragma mark public - (instancetype)init {
self = [super init];
+ (instancetype)sharedInstance { for (NSString *event in [self supportedEvents]) {
static RNEventEmitter *sharedInstance = nil; [self addListener:event];
static dispatch_once_t onceToken; }
return self;
dispatch_once(&onceToken, ^{
sharedInstance = [[RNEventEmitter alloc] init];
});
return sharedInstance;
} }
# pragma mark public
+ (void)sendEvent:(NSString *)event body:(NSDictionary *)body { + (void)sendEvent:(NSString *)event body:(NSDictionary *)body {
[[self sharedInstance] send:event body:body]; [[NSNotificationCenter defaultCenter] postNotificationName:event
object:self
userInfo:body];
} }
# pragma mark private # pragma mark private
- (void)send:(NSString *)eventName body:(id)body { - (void)startObserving {
if (self.bridge == nil) { for (NSString *event in [self supportedEvents]) {
return; [[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 @end
#import <Foundation/Foundation.h> #import <Foundation/Foundation.h>
@import UserNotifications; @import UserNotifications;
#import "RNNotificationsStore.h" #import "RNNotificationsStore.h"
#import "RNEventEmitter.h"
@interface RNNotificationEventHandler : NSObject @interface RNNotificationEventHandler : NSObject
......
...@@ -4,11 +4,14 @@ ...@@ -4,11 +4,14 @@
@interface RNNotifications : NSObject @interface RNNotifications : NSObject
@property (nonatomic, retain) NSDictionary* initialNotification;
+ (instancetype)sharedInstance; + (instancetype)sharedInstance;
- (void)initialize; - (void)initialize;
- (void)didRegisterForRemoteNotificationsWithDeviceToken:(id)deviceToken; - (void)didRegisterForRemoteNotificationsWithDeviceToken:(id)deviceToken;
- (void)didFailToRegisterForRemoteNotificationsWithError:(NSError *)error; - (void)didFailToRegisterForRemoteNotificationsWithError:(NSError *)error;
- (void)finishHandleNotificationKey:(NSString *)notificationKey;
//- (void)setBadgeForNotification:(NSDictionary *)notification; //- (void)setBadgeForNotification:(NSDictionary *)notification;
......
...@@ -8,6 +8,14 @@ ...@@ -8,6 +8,14 @@
@implementation RNNotifications { @implementation RNNotifications {
RNNotificationCenterListener* _notificationCenterListener; RNNotificationCenterListener* _notificationCenterListener;
RNNotificationEventHandler* _notificationEventHandler; RNNotificationEventHandler* _notificationEventHandler;
RNEventEmitter* _eventEmitter;
RNNotificationsStore* _store;
}
- (instancetype)init {
self = [super init];
_store = [RNNotificationsStore new];
return self;
} }
+ (instancetype)sharedInstance { + (instancetype)sharedInstance {
...@@ -21,7 +29,7 @@ ...@@ -21,7 +29,7 @@
} }
- (void)initialize { - (void)initialize {
_notificationEventHandler = [RNNotificationEventHandler new]; _notificationEventHandler = [[RNNotificationEventHandler alloc] initWithStore:_store];
_notificationCenterListener = [[RNNotificationCenterListener alloc] initWithNotificationEventHandler:_notificationEventHandler]; _notificationCenterListener = [[RNNotificationCenterListener alloc] initWithNotificationEventHandler:_notificationEventHandler];
} }
...@@ -39,4 +47,12 @@ ...@@ -39,4 +47,12 @@
} }
} }
- (void)setInitialNotification:(NSDictionary *)notification {
[_store setInitialNotification:notification];
}
- (void)finishHandleNotificationKey:(NSString *)notificationKey {
[_store completeAction:notificationKey];
}
@end @end
...@@ -69,8 +69,6 @@ ...@@ -69,8 +69,6 @@
50351F9422CD7FF1000713B3 /* RCTConvert+Notifications.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "RCTConvert+Notifications.m"; sourceTree = "<group>"; }; 50351F9422CD7FF1000713B3 /* RCTConvert+Notifications.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "RCTConvert+Notifications.m"; sourceTree = "<group>"; };
50351F9622CD8604000713B3 /* RNCommandsHandler.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RNCommandsHandler.h; sourceTree = "<group>"; }; 50351F9622CD8604000713B3 /* RNCommandsHandler.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RNCommandsHandler.h; sourceTree = "<group>"; };
50351F9722CD8604000713B3 /* RNCommandsHandler.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RNCommandsHandler.m; sourceTree = "<group>"; }; 50351F9722CD8604000713B3 /* RNCommandsHandler.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RNCommandsHandler.m; sourceTree = "<group>"; };
508CE7BB22D12B0A00357815 /* Tests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = Tests.m; sourceTree = "<group>"; };
508CE7BD22D12B0A00357815 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
508CE7C822D12B2600357815 /* RNNotificationsTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = RNNotificationsTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 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 = "<group>"; }; 508CE7CA22D12B2600357815 /* RNNotificationsTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RNNotificationsTests.m; sourceTree = "<group>"; };
508CE7CC22D12B2600357815 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; }; 508CE7CC22D12B2600357815 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
...@@ -119,15 +117,6 @@ ...@@ -119,15 +117,6 @@
name = Products; name = Products;
sourceTree = "<group>"; sourceTree = "<group>";
}; };
508CE7BA22D12B0A00357815 /* Tests */ = {
isa = PBXGroup;
children = (
508CE7BB22D12B0A00357815 /* Tests.m */,
508CE7BD22D12B0A00357815 /* Info.plist */,
);
path = Tests;
sourceTree = "<group>";
};
508CE7C922D12B2600357815 /* RNNotificationsTests */ = { 508CE7C922D12B2600357815 /* RNNotificationsTests */ = {
isa = PBXGroup; isa = PBXGroup;
children = ( children = (
...@@ -196,7 +185,6 @@ ...@@ -196,7 +185,6 @@
D8A2F7541CB57F1A002CC8F5 /* RNNotifications.m */, D8A2F7541CB57F1A002CC8F5 /* RNNotifications.m */,
50351F9622CD8604000713B3 /* RNCommandsHandler.h */, 50351F9622CD8604000713B3 /* RNCommandsHandler.h */,
50351F9722CD8604000713B3 /* RNCommandsHandler.m */, 50351F9722CD8604000713B3 /* RNCommandsHandler.m */,
508CE7BA22D12B0A00357815 /* Tests */,
508CE7C922D12B2600357815 /* RNNotificationsTests */, 508CE7C922D12B2600357815 /* RNNotificationsTests */,
134814211AA4EA7D00B7C361 /* Products */, 134814211AA4EA7D00B7C361 /* Products */,
508CE7C822D12B2600357815 /* RNNotificationsTests.xctest */, 508CE7C822D12B2600357815 /* RNNotificationsTests.xctest */,
......
#import "RNPushKitEventListener.h" #import "RNPushKitEventListener.h"
#import "RNEventEmitter.h"
#import "RNUtils.h" #import "RNUtils.h"
@implementation RNPushKitEventListener { @implementation RNPushKitEventListener {
......
...@@ -42,6 +42,7 @@ class NotificationsExampleApp extends Component { ...@@ -42,6 +42,7 @@ class NotificationsExampleApp extends Component {
}; };
NotificationsIOS.addEventListener('remoteNotificationsRegistered', this.onPushRegistered.bind(this)); NotificationsIOS.addEventListener('remoteNotificationsRegistered', this.onPushRegistered.bind(this));
NotificationsIOS.addEventListener('remoteNotificationsRegistrationFailed', this.onPushRegisteredFailed.bind(this));
NotificationsIOS.consumeBackgroundQueue(); NotificationsIOS.consumeBackgroundQueue();
...@@ -56,6 +57,10 @@ class NotificationsExampleApp extends Component { ...@@ -56,6 +57,10 @@ class NotificationsExampleApp extends Component {
console.log('Device Token Received: ' + deviceToken); console.log('Device Token Received: ' + deviceToken);
} }
onPushRegisteredFailed(error) {
console.log('Remote notifiction registration failed: ' + error);
}
onPushKitRegistered(deviceToken) { onPushKitRegistered(deviceToken) {
console.log('PushKit Token Received: ' + deviceToken); console.log('PushKit Token Received: ' + deviceToken);
} }
......
...@@ -9,9 +9,7 @@ ...@@ -9,9 +9,7 @@
#import <UIKit/UIKit.h> #import <UIKit/UIKit.h>
#import <PushKit/PushKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate>
@interface AppDelegate : UIResponder <UIApplicationDelegate, PKPushRegistryDelegate>
@property (nonatomic, strong) UIWindow *window; @property (nonatomic, strong) UIWindow *window;
......
...@@ -8,8 +8,7 @@ ...@@ -8,8 +8,7 @@
@implementation AppDelegate @implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
{
NSURL *jsCodeLocation; NSURL *jsCodeLocation;
jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil]; jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil];
...@@ -32,14 +31,16 @@ ...@@ -32,14 +31,16 @@
} }
// Required to register for notifications // Required to register for notifications
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings - (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
{
// [[RNNotifications sharedInstance] didRegisterUserNotificationSettings:notificationSettings]; // [[RNNotifications sharedInstance] didRegisterUserNotificationSettings:notificationSettings];
} }
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
{
[[RNNotifications sharedInstance] didRegisterForRemoteNotificationsWithDeviceToken:deviceToken]; [[RNNotifications sharedInstance] didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
} }
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
[[RNNotifications sharedInstance] didFailToRegisterForRemoteNotificationsWithError:error];
}
@end @end
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>aps-environment</key>
<string>development</string>
</dict>
</plist>
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment