-
Notifications
You must be signed in to change notification settings - Fork 29
/
RCTRemotePushManager.m
90 lines (63 loc) · 3.32 KB
/
RCTRemotePushManager.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#import "RCTRemotePushManager.h"
#import "RCTBridge.h"
#import "RCTEventDispatcher.h"
@implementation RCTRemotePushManager
RCT_EXPORT_MODULE()
@synthesize bridge = _bridge;
NSDictionary *startupNotification;
- (id) init
{
self = [super init];
if (!self) return nil;
// Registered for remote notifications
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(registeredForRemoteNotifications:) name:@"registeredForRemoteNotifications" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(registeredForRemoteNotifications:) name:@"registeredForRemoteNotificationsError" object:nil];
// Received remote notification
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivedRemoteNotification:) name:@"receivedRemoteNotification" object:nil];
// Received remote notification
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivedRemoteNotificationOnStart:) name:@"receivedRemoteNotificationOnStart" object:nil];
return self;
}
- (void) dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
RCT_EXPORT_METHOD(requestPermissions)
{
//Register for remote notifications straightaway
[[UIApplication sharedApplication] registerForRemoteNotifications];
// Now register for user notifications
UIUserNotificationType types = UIUserNotificationTypeSound | UIUserNotificationTypeBadge | UIUserNotificationTypeAlert;
UIUserNotificationSettings *notificationSettings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];
}
- (void) registeredForRemoteNotifications:(NSNotification *) notification {
NSDictionary *userInfo = notification.userInfo;
NSData *deviceToken = [userInfo objectForKey:@"deviceToken"];
NSString *deviceTokenStr = [[[[deviceToken description]
stringByReplacingOccurrencesOfString: @"<" withString: @""]
stringByReplacingOccurrencesOfString: @">" withString: @""]
stringByReplacingOccurrencesOfString: @" " withString: @""];
[self.bridge.eventDispatcher sendDeviceEventWithName:@"registeredForRemoteNotifications"
body:@{@"token": deviceTokenStr}];
}
- (void) registeredForRemoteNotificationsError:(NSNotification *) notification {
[self.bridge.eventDispatcher sendDeviceEventWithName:@"registeredForRemoteNotifications"
body:@{@"error": @"Could not register for remote noticiations"}];
}
- (void) receivedRemoteNotification:(NSNotification *) notification {
NSDictionary *userInfo = notification.userInfo;
[self.bridge.eventDispatcher sendDeviceEventWithName:@"receivedRemoteNotification" body: userInfo];
}
- (void) receivedRemoteNotificationOnStart:(NSNotification *) notification {
startupNotification = notification.userInfo;
}
RCT_EXPORT_METHOD(getStartupNotifications: (RCTResponseSenderBlock)callback)
{
if (startupNotification) {
callback(@[[NSNull null], startupNotification]);
} else {
callback(@[[NSNull null], [NSNull null]]);
}
}
@end