1212#import " RCTBridge.h"
1313#import " RCTEventDispatcher.h"
1414
15+ #if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_8_0
16+
17+ #define UIUserNotificationTypeAlert UIRemoteNotificationTypeAlert
18+ #define UIUserNotificationTypeBadge UIRemoteNotificationTypeBadge
19+ #define UIUserNotificationTypeSound UIRemoteNotificationTypeSound
20+ #define UIUserNotificationTypeNone UIRemoteNotificationTypeNone
21+ #define UIUserNotificationType UIRemoteNotificationType
22+
23+ #endif
24+
1525NSString *const RCTRemoteNotificationReceived = @" RemoteNotificationReceived" ;
26+ NSString *const RCTRemoteNotificationsRegistered = @" RemoteNotificationsRegistered" ;
1627
1728@implementation RCTPushNotificationManager
1829{
@@ -30,6 +41,10 @@ - (instancetype)init
3041 selector: @selector (handleRemoteNotificationReceived: )
3142 name: RCTRemoteNotificationReceived
3243 object: nil ];
44+ [[NSNotificationCenter defaultCenter ] addObserver: self
45+ selector: @selector (handleRemoteNotificationsRegistered: )
46+ name: RCTRemoteNotificationsRegistered
47+ object: nil ];
3348 }
3449 return self;
3550}
@@ -52,6 +67,21 @@ + (void)application:(UIApplication *)application didRegisterUserNotificationSett
5267 }
5368}
5469
70+ + (void )application : (UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken : (NSData *)deviceToken
71+ {
72+ NSMutableString *hexString = [NSMutableString string ];
73+ const unsigned char *bytes = [deviceToken bytes ];
74+ for (int i = 0 ; i < [deviceToken length ]; i++) {
75+ [hexString appendFormat: @" %02x " , bytes[i]];
76+ }
77+ NSDictionary *userInfo = @{
78+ @" deviceToken" : [hexString copy ]
79+ };
80+ [[NSNotificationCenter defaultCenter ] postNotificationName: RCTRemoteNotificationsRegistered
81+ object: self
82+ userInfo: userInfo];
83+ }
84+
5585+ (void )application : (UIApplication *)application didReceiveRemoteNotification : (NSDictionary *)notification
5686{
5787 [[NSNotificationCenter defaultCenter ] postNotificationName: RCTRemoteNotificationReceived
@@ -65,6 +95,12 @@ - (void)handleRemoteNotificationReceived:(NSNotification *)notification
6595 body: [notification userInfo ]];
6696}
6797
98+ - (void )handleRemoteNotificationsRegistered : (NSNotification *)notification
99+ {
100+ [_bridge.eventDispatcher sendDeviceEventWithName: @" remoteNotificationsRegistered"
101+ body: [notification userInfo ]];
102+ }
103+
68104/* *
69105 * Update the application icon badge number on the home screen
70106 */
@@ -83,36 +119,35 @@ - (void)handleRemoteNotificationReceived:(NSNotification *)notification
83119 ]);
84120}
85121
86- RCT_EXPORT_METHOD (requestPermissions)
122+ RCT_EXPORT_METHOD (requestPermissions:( NSDictionary *)permissions )
87123{
88- Class _UIUserNotificationSettings;
89- if ((_UIUserNotificationSettings = NSClassFromString (@" UIUserNotificationSettings" ))) {
90- UIUserNotificationType types = UIUserNotificationTypeSound | UIUserNotificationTypeBadge | UIUserNotificationTypeAlert;
91- UIUserNotificationSettings *notificationSettings = [_UIUserNotificationSettings settingsForTypes: types categories: nil ];
92- [[UIApplication sharedApplication ] registerUserNotificationSettings: notificationSettings];
124+ UIUserNotificationType types = UIRemoteNotificationTypeNone;
125+ if (permissions) {
126+ if ([permissions[@" alert" ] boolValue ]) {
127+ types |= UIUserNotificationTypeAlert;
128+ }
129+ if ([permissions[@" badge" ] boolValue ]) {
130+ types |= UIUserNotificationTypeBadge;
131+ }
132+ if ([permissions[@" sound" ] boolValue ]) {
133+ types |= UIUserNotificationTypeSound;
134+ }
93135 } else {
136+ types = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;
137+ }
94138
95- #if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_8_0
96-
97- [[UIApplication sharedApplication ] registerForRemoteNotificationTypes:
98- UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert];
99-
139+ #if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_8_0
140+ id notificationSettings = [UIUserNotificationSettings settingsForTypes: types categories: nil ];
141+ [[UIApplication sharedApplication ] registerUserNotificationSettings: notificationSettings];
142+ [[UIApplication sharedApplication ] registerForRemoteNotifications ];
143+ #else
144+ [[UIApplication sharedApplication ] registerForRemoteNotificationTypes: types];
100145#endif
101146
102- }
103147}
104148
105149RCT_EXPORT_METHOD (checkPermissions:(RCTResponseSenderBlock)callback)
106150{
107-
108- #if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_8_0
109-
110- #define UIUserNotificationTypeAlert UIRemoteNotificationTypeAlert
111- #define UIUserNotificationTypeBadge UIRemoteNotificationTypeBadge
112- #define UIUserNotificationTypeSound UIRemoteNotificationTypeSound
113-
114- #endif
115-
116151 NSUInteger types = 0 ;
117152 if ([UIApplication instancesRespondToSelector: @selector (currentUserNotificationSettings )]) {
118153 types = [[[UIApplication sharedApplication ] currentUserNotificationSettings ] types ];
0 commit comments