Commit df53f390 authored by yogevbd's avatar yogevbd

WIP

parent 961f9a8b
#import "RNBridgeModule.h"
#import "RNCommandsHandler.h"
#import "RCTConvert+Notifications.h"
#import "RNEventEmitter.h"
#import "RNNotifications.h"
#import "RNNotificationsStore.h"
#import <React/RCTBridgeDelegate.h>
@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
......
......@@ -4,8 +4,6 @@
@interface RNCommandsHandler : NSObject
- (instancetype)initWithStore:(RNNotificationsStore *)store;
- (void)requestPermissionsWithCategories:(NSArray *)json;
- (void)getInitialNotification:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject;
......
......@@ -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 {
......
#import "RCTEventEmitter.h"
#import <React/RCTEventEmitter.h>
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 <RCTBridgeModule>
+ (void)sendEvent:(NSString *)event body:(NSDictionary *)body;
......
......@@ -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
#import <Foundation/Foundation.h>
@import UserNotifications;
#import "RNNotificationsStore.h"
#import "RNEventEmitter.h"
@interface RNNotificationEventHandler : NSObject
......
......@@ -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;
......
......@@ -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
......@@ -69,8 +69,6 @@
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>"; };
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; };
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>"; };
......@@ -119,15 +117,6 @@
name = Products;
sourceTree = "<group>";
};
508CE7BA22D12B0A00357815 /* Tests */ = {
isa = PBXGroup;
children = (
508CE7BB22D12B0A00357815 /* Tests.m */,
508CE7BD22D12B0A00357815 /* Info.plist */,
);
path = Tests;
sourceTree = "<group>";
};
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 */,
......
#import "RNPushKitEventListener.h"
#import "RNEventEmitter.h"
#import "RNUtils.h"
@implementation RNPushKitEventListener {
......
......@@ -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);
}
......
......@@ -9,9 +9,7 @@
#import <UIKit/UIKit.h>
#import <PushKit/PushKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate, PKPushRegistryDelegate>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (nonatomic, strong) UIWindow *window;
......
......@@ -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
<?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