18
18
19
19
static NSString *const kLocalNotificationReceived = @" LocalNotificationReceived" ;
20
20
static NSString *const kRemoteNotificationsRegistered = @" RemoteNotificationsRegistered" ;
21
- static NSString *const kRegisterUserNotificationSettings = @" RegisterUserNotificationSettings" ;
22
21
static NSString *const kRemoteNotificationRegistrationFailed = @" RemoteNotificationRegistrationFailed" ;
23
22
24
23
static NSString *const kErrorUnableToRequestPermissions = @" E_UNABLE_TO_REQUEST_PERMISSIONS" ;
@@ -68,20 +67,20 @@ + (UILocalNotification *)UILocalNotification:(id)json
68
67
}
69
68
70
69
RCT_ENUM_CONVERTER (UIBackgroundFetchResult, (@{
71
- @" UIBackgroundFetchResultNewData" : @(UIBackgroundFetchResultNewData),
72
- @" UIBackgroundFetchResultNoData" : @(UIBackgroundFetchResultNoData),
73
- @" UIBackgroundFetchResultFailed" : @(UIBackgroundFetchResultFailed),
74
- }), UIBackgroundFetchResultNoData, integerValue)
70
+ @" UIBackgroundFetchResultNewData" : @(UIBackgroundFetchResultNewData),
71
+ @" UIBackgroundFetchResultNoData" : @(UIBackgroundFetchResultNoData),
72
+ @" UIBackgroundFetchResultFailed" : @(UIBackgroundFetchResultFailed),
73
+ }), UIBackgroundFetchResultNoData, integerValue)
75
74
76
75
@end
77
- #endif // TARGET_OS_TV
76
+ #else
77
+ @interface RNCPushNotificationIOS () <NativePushNotificationManagerIOS>
78
+ @end
79
+ #endif // TARGET_OS_TV / TARGET_OS_UIKITFORMAC
78
80
79
81
@implementation RNCPushNotificationIOS
80
- {
81
- RCTPromiseResolveBlock _requestPermissionsResolveBlock;
82
- }
83
82
84
- #if !TARGET_OS_TV
83
+ #if !TARGET_OS_TV && !TARGET_OS_UIKITFORMAC
85
84
86
85
static NSDictionary *RCTFormatLocalNotification (UILocalNotification *notification)
87
86
{
@@ -102,6 +101,7 @@ @implementation RNCPushNotificationIOS
102
101
return formattedLocalNotification;
103
102
}
104
103
104
+ API_AVAILABLE (ios(10.0 ))
105
105
static NSDictionary *RCTFormatUNNotification(UNNotification *notification)
106
106
{
107
107
NSMutableDictionary *formattedNotification = [NSMutableDictionary dictionary ];
@@ -125,7 +125,7 @@ @implementation RNCPushNotificationIOS
125
125
return formattedNotification;
126
126
}
127
127
128
- #endif // TARGET_OS_TV
128
+ #endif // TARGET_OS_TV / TARGET_OS_UIKITFORMAC
129
129
130
130
RCT_EXPORT_MODULE ()
131
131
@@ -134,7 +134,7 @@ - (dispatch_queue_t)methodQueue
134
134
return dispatch_get_main_queue ();
135
135
}
136
136
137
- #if !TARGET_OS_TV
137
+ #if !TARGET_OS_TV && !TARGET_OS_UIKITFORMAC
138
138
- (void )startObserving
139
139
{
140
140
[[NSNotificationCenter defaultCenter ] addObserver: self
@@ -145,10 +145,6 @@ - (void)startObserving
145
145
selector: @selector (handleRemoteNotificationReceived: )
146
146
name: RCTRemoteNotificationReceived
147
147
object: nil ];
148
- [[NSNotificationCenter defaultCenter ] addObserver: self
149
- selector: @selector (handleRegisterUserNotificationSettings: )
150
- name: kRegisterUserNotificationSettings
151
- object: nil ];
152
148
[[NSNotificationCenter defaultCenter ] addObserver: self
153
149
selector: @selector (handleRemoteNotificationsRegistered: )
154
150
name: kRemoteNotificationsRegistered
@@ -174,19 +170,13 @@ - (void)stopObserving
174
170
175
171
+ (void )didRegisterUserNotificationSettings : (__unused UIUserNotificationSettings *)notificationSettings
176
172
{
177
- if ([UIApplication instancesRespondToSelector: @selector (registerForRemoteNotifications )]) {
178
- [RCTSharedApplication () registerForRemoteNotifications ];
179
- [[NSNotificationCenter defaultCenter ] postNotificationName: kRegisterUserNotificationSettings
180
- object: self
181
- userInfo: @{@" notificationSettings" : notificationSettings}];
182
- }
183
173
}
184
174
185
175
+ (void )didRegisterForRemoteNotificationsWithDeviceToken : (NSData *)deviceToken
186
176
{
187
177
NSMutableString *hexString = [NSMutableString string ];
188
178
NSUInteger deviceTokenLength = deviceToken.length ;
189
- const unsigned char *bytes = deviceToken.bytes ;
179
+ const unsigned char *bytes = reinterpret_cast< const unsigned char *>( deviceToken.bytes ) ;
190
180
for (NSUInteger i = 0 ; i < deviceTokenLength; i++) {
191
181
[hexString appendFormat: @" %02x " , bytes[i]];
192
182
}
@@ -258,32 +248,13 @@ - (void)handleRemoteNotificationRegistrationError:(NSNotification *)notification
258
248
{
259
249
NSError *error = notification.userInfo [@" error" ];
260
250
NSDictionary *errorDetails = @{
261
- @" message" : error.localizedDescription ,
262
- @" code" : @(error.code ),
263
- @" details" : error.userInfo ,
264
- };
251
+ @" message" : error.localizedDescription ,
252
+ @" code" : @(error.code ),
253
+ @" details" : error.userInfo ,
254
+ };
265
255
[self sendEventWithName: @" remoteNotificationRegistrationError" body: errorDetails];
266
256
}
267
257
268
- - (void )handleRegisterUserNotificationSettings : (NSNotification *)notification
269
- {
270
- if (_requestPermissionsResolveBlock == nil ) {
271
- return ;
272
- }
273
-
274
- UIUserNotificationSettings *notificationSettings = notification.userInfo [@" notificationSettings" ];
275
- NSDictionary *notificationTypes = @{
276
- @" alert" : @((notificationSettings.types & UIUserNotificationTypeAlert) > 0 ),
277
- @" sound" : @((notificationSettings.types & UIUserNotificationTypeSound) > 0 ),
278
- @" badge" : @((notificationSettings.types & UIUserNotificationTypeBadge) > 0 ),
279
- };
280
-
281
- _requestPermissionsResolveBlock (notificationTypes);
282
- // Clean up listener added in requestPermissions
283
- [self removeListeners: 1 ];
284
- _requestPermissionsResolveBlock = nil ;
285
- }
286
-
287
258
RCT_EXPORT_METHOD (onFinishRemoteNotification:(NSString *)notificationId fetchResult:(UIBackgroundFetchResult)result) {
288
259
RNCRemoteNotificationCallback completionHandler = self.remoteNotificationCallbacks [notificationId];
289
260
if (!completionHandler) {
@@ -318,15 +289,9 @@ - (void)handleRegisterUserNotificationSettings:(NSNotification *)notification
318
289
reject (kErrorUnableToRequestPermissions , nil , RCTErrorWithMessage (@" Requesting push notifications is currently unavailable in an app extension" ));
319
290
return ;
320
291
}
321
-
322
- if (_requestPermissionsResolveBlock != nil ) {
323
- RCTLogError (@" Cannot call requestPermissions twice before the first has returned." );
324
- return ;
325
- }
326
-
292
+
327
293
// Add a listener to make sure that startObserving has been called
328
294
[self addListener: @" remoteNotificationsRegistered" ];
329
- _requestPermissionsResolveBlock = resolve;
330
295
331
296
UIUserNotificationType types = UIUserNotificationTypeNone;
332
297
if (permissions) {
@@ -343,9 +308,19 @@ - (void)handleRegisterUserNotificationSettings:(NSNotification *)notification
343
308
types = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;
344
309
}
345
310
346
- UIUserNotificationSettings *notificationSettings =
347
- [UIUserNotificationSettings settingsForTypes: types categories: nil ];
348
- [RCTSharedApplication () registerUserNotificationSettings: notificationSettings];
311
+ [UNUserNotificationCenter .currentNotificationCenter
312
+ requestAuthorizationWithOptions: types
313
+ completionHandler: ^(BOOL granted, NSError *_Nullable error) {
314
+
315
+ if (error != NULL ) {
316
+ reject (@" -1" , @" Error - Push authorization request failed." , error);
317
+ } else {
318
+ [RCTSharedApplication () registerForRemoteNotifications ];
319
+ [UNUserNotificationCenter .currentNotificationCenter getNotificationSettingsWithCompletionHandler: ^(UNNotificationSettings * _Nonnull settings) {
320
+ resolve (RCTPromiseResolveValueForUNNotificationSettings (settings));
321
+ }];
322
+ }
323
+ }];
349
324
}
350
325
351
326
RCT_EXPORT_METHOD (abandonPermissions)
@@ -356,16 +331,24 @@ - (void)handleRegisterUserNotificationSettings:(NSNotification *)notification
356
331
RCT_EXPORT_METHOD (checkPermissions:(RCTResponseSenderBlock)callback)
357
332
{
358
333
if (RCTRunningInAppExtension ()) {
359
- callback (@[@{ @" alert " : @ NO , @" badge " : @ NO , @" sound " : @ NO } ]);
334
+ callback (@[RCTSettingsDictForUNNotificationSettings ( NO , NO , NO ) ]);
360
335
return ;
361
336
}
362
337
363
- NSUInteger types = [RCTSharedApplication () currentUserNotificationSettings ].types ;
364
- callback (@[@{
365
- @" alert" : @((types & UIUserNotificationTypeAlert) > 0 ),
366
- @" badge" : @((types & UIUserNotificationTypeBadge) > 0 ),
367
- @" sound" : @((types & UIUserNotificationTypeSound) > 0 ),
368
- }]);
338
+ [UNUserNotificationCenter .currentNotificationCenter getNotificationSettingsWithCompletionHandler: ^(UNNotificationSettings * _Nonnull settings) {
339
+ callback (@[RCTPromiseResolveValueForUNNotificationSettings (settings)]);
340
+ }];
341
+ }
342
+
343
+ static inline NSDictionary *RCTPromiseResolveValueForUNNotificationSettings (UNNotificationSettings * _Nonnull settings) {
344
+ return RCTSettingsDictForUNNotificationSettings (settings.alertSetting == UNNotificationSettingEnabled ,
345
+ settings.badgeSetting == UNNotificationSettingEnabled ,
346
+ settings.soundSetting == UNNotificationSettingEnabled );
347
+ }
348
+
349
+ static inline NSDictionary *RCTSettingsDictForUNNotificationSettings (BOOL alert, BOOL badge, BOOL sound) {
350
+ return @{@" alert" : @(alert), @" badge" : @(badge), @" sound" : @(sound)};
351
+ }
369
352
}
370
353
371
354
RCT_EXPORT_METHOD (presentLocalNotification:(UILocalNotification *)notification)
@@ -464,13 +447,92 @@ - (void)handleRegisterUserNotificationSettings:(NSNotification *)notification
464
447
}
465
448
}
466
449
467
- #else // TARGET_OS_TV
450
+ #else // TARGET_OS_TV / TARGET_OS_UIKITFORMAC
451
+
452
+ RCT_EXPORT_METHOD (onFinishRemoteNotification:(NSString *)notificationId fetchResult:(NSString *)fetchResult)
453
+ {
454
+ RCTLogError (@" Not implemented: %@ " , NSStringFromSelector (_cmd));
455
+ }
456
+
457
+ RCT_EXPORT_METHOD (setApplicationIconBadgeNumber:(double )number)
458
+ {
459
+ RCTLogError (@" Not implemented: %@ " , NSStringFromSelector (_cmd));
460
+ }
461
+
462
+ RCT_EXPORT_METHOD (getApplicationIconBadgeNumber:(RCTResponseSenderBlock)callback)
463
+ {
464
+ RCTLogError (@" Not implemented: %@ " , NSStringFromSelector (_cmd));
465
+ }
466
+
467
+ RCT_EXPORT_METHOD (requestPermissions:(JS::NativePushNotificationManagerIOS::SpecRequestPermissionsPermission &)permissions
468
+ resolve:(RCTPromiseResolveBlock)resolve
469
+ reject:(RCTPromiseRejectBlock)reject)
470
+ {
471
+ RCTLogError (@" Not implemented: %@ " , NSStringFromSelector (_cmd));
472
+ }
473
+
474
+ RCT_EXPORT_METHOD (abandonPermissions)
475
+ {
476
+ RCTLogError (@" Not implemented: %@ " , NSStringFromSelector (_cmd));
477
+ }
478
+
479
+ RCT_EXPORT_METHOD (checkPermissions:(RCTResponseSenderBlock)callback)
480
+ {
481
+ RCTLogError (@" Not implemented: %@ " , NSStringFromSelector (_cmd));
482
+ }
483
+
484
+ RCT_EXPORT_METHOD (presentLocalNotification:(JS::NativePushNotificationManagerIOS::Notification &)notification)
485
+ {
486
+ RCTLogError (@" Not implemented: %@ " , NSStringFromSelector (_cmd));
487
+ }
488
+
489
+ RCT_EXPORT_METHOD (scheduleLocalNotification:(JS::NativePushNotificationManagerIOS::Notification &)notification)
490
+ {
491
+ RCTLogError (@" Not implemented: %@ " , NSStringFromSelector (_cmd));
492
+ }
493
+
494
+ RCT_EXPORT_METHOD (cancelAllLocalNotifications)
495
+ {
496
+ RCTLogError (@" Not implemented: %@ " , NSStringFromSelector (_cmd));
497
+ }
498
+
499
+ RCT_EXPORT_METHOD (cancelLocalNotifications:(NSDictionary <NSString *, id > *)userInfo)
500
+ {
501
+ RCTLogError (@" Not implemented: %@ " , NSStringFromSelector (_cmd));
502
+ }
503
+
504
+ RCT_EXPORT_METHOD (getInitialNotification:(RCTPromiseResolveBlock)resolve
505
+ reject:(__unused RCTPromiseRejectBlock)reject)
506
+ {
507
+ RCTLogError (@" Not implemented: %@ " , NSStringFromSelector (_cmd));
508
+ }
509
+
510
+ RCT_EXPORT_METHOD (getScheduledLocalNotifications:(RCTResponseSenderBlock)callback)
511
+ {
512
+ RCTLogError (@" Not implemented: %@ " , NSStringFromSelector (_cmd));
513
+ }
514
+
515
+ RCT_EXPORT_METHOD (removeAllDeliveredNotifications)
516
+ {
517
+ RCTLogError (@" Not implemented: %@ " , NSStringFromSelector (_cmd));
518
+ }
519
+
520
+ RCT_EXPORT_METHOD (removeDeliveredNotifications:(NSArray <NSString *> *)identifiers)
521
+ {
522
+ RCTLogError (@" Not implemented: %@ " , NSStringFromSelector (_cmd));
523
+ }
524
+
525
+ RCT_EXPORT_METHOD (getDeliveredNotifications:(RCTResponseSenderBlock)callback)
526
+ {
527
+ RCTLogError (@" Not implemented: %@ " , NSStringFromSelector (_cmd));
528
+ }
529
+
468
530
469
531
- (NSArray <NSString *> *)supportedEvents
470
532
{
471
533
return @[];
472
534
}
473
535
474
- #endif // TARGET_OS_TV
536
+ #endif // TARGET_OS_TV / TARGET_OS_UIKITFORMAC
475
537
476
538
@end
0 commit comments