Skip to content

Commit fbcb2e7

Browse files
authored
Merge pull request #66 from Naturalclar/feat/removeDeprecatedMethods
feat: replace apple-deprecated notification method
2 parents bbd12f2 + 15d1085 commit fbcb2e7

File tree

2 files changed

+128
-66
lines changed

2 files changed

+128
-66
lines changed

ios/RNCPushNotificationIOS.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ extern NSString *const RNCRemoteNotificationReceived;
1313

1414
typedef void (^RNCRemoteNotificationCallback)(UIBackgroundFetchResult result);
1515

16-
#if !TARGET_OS_TV
16+
#if !TARGET_OS_TV && !TARGET_OS_UIKITFORMAC
1717
+ (void)didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings;
1818
+ (void)didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken;
1919
+ (void)didReceiveRemoteNotification:(NSDictionary *)notification;

ios/RNCPushNotificationIOS.m

Lines changed: 127 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
static NSString *const kLocalNotificationReceived = @"LocalNotificationReceived";
2020
static NSString *const kRemoteNotificationsRegistered = @"RemoteNotificationsRegistered";
21-
static NSString *const kRegisterUserNotificationSettings = @"RegisterUserNotificationSettings";
2221
static NSString *const kRemoteNotificationRegistrationFailed = @"RemoteNotificationRegistrationFailed";
2322

2423
static NSString *const kErrorUnableToRequestPermissions = @"E_UNABLE_TO_REQUEST_PERMISSIONS";
@@ -68,20 +67,20 @@ + (UILocalNotification *)UILocalNotification:(id)json
6867
}
6968

7069
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)
7574

7675
@end
77-
#endif //TARGET_OS_TV
76+
#else
77+
@interface RNCPushNotificationIOS () <NativePushNotificationManagerIOS>
78+
@end
79+
#endif //TARGET_OS_TV / TARGET_OS_UIKITFORMAC
7880

7981
@implementation RNCPushNotificationIOS
80-
{
81-
RCTPromiseResolveBlock _requestPermissionsResolveBlock;
82-
}
8382

84-
#if !TARGET_OS_TV
83+
#if !TARGET_OS_TV && !TARGET_OS_UIKITFORMAC
8584

8685
static NSDictionary *RCTFormatLocalNotification(UILocalNotification *notification)
8786
{
@@ -102,6 +101,7 @@ @implementation RNCPushNotificationIOS
102101
return formattedLocalNotification;
103102
}
104103

104+
API_AVAILABLE(ios(10.0))
105105
static NSDictionary *RCTFormatUNNotification(UNNotification *notification)
106106
{
107107
NSMutableDictionary *formattedNotification = [NSMutableDictionary dictionary];
@@ -125,7 +125,7 @@ @implementation RNCPushNotificationIOS
125125
return formattedNotification;
126126
}
127127

128-
#endif //TARGET_OS_TV
128+
#endif //TARGET_OS_TV / TARGET_OS_UIKITFORMAC
129129

130130
RCT_EXPORT_MODULE()
131131

@@ -134,7 +134,7 @@ - (dispatch_queue_t)methodQueue
134134
return dispatch_get_main_queue();
135135
}
136136

137-
#if !TARGET_OS_TV
137+
#if !TARGET_OS_TV && !TARGET_OS_UIKITFORMAC
138138
- (void)startObserving
139139
{
140140
[[NSNotificationCenter defaultCenter] addObserver:self
@@ -145,10 +145,6 @@ - (void)startObserving
145145
selector:@selector(handleRemoteNotificationReceived:)
146146
name:RCTRemoteNotificationReceived
147147
object:nil];
148-
[[NSNotificationCenter defaultCenter] addObserver:self
149-
selector:@selector(handleRegisterUserNotificationSettings:)
150-
name:kRegisterUserNotificationSettings
151-
object:nil];
152148
[[NSNotificationCenter defaultCenter] addObserver:self
153149
selector:@selector(handleRemoteNotificationsRegistered:)
154150
name:kRemoteNotificationsRegistered
@@ -174,19 +170,13 @@ - (void)stopObserving
174170

175171
+ (void)didRegisterUserNotificationSettings:(__unused UIUserNotificationSettings *)notificationSettings
176172
{
177-
if ([UIApplication instancesRespondToSelector:@selector(registerForRemoteNotifications)]) {
178-
[RCTSharedApplication() registerForRemoteNotifications];
179-
[[NSNotificationCenter defaultCenter] postNotificationName:kRegisterUserNotificationSettings
180-
object:self
181-
userInfo:@{@"notificationSettings": notificationSettings}];
182-
}
183173
}
184174

185175
+ (void)didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
186176
{
187177
NSMutableString *hexString = [NSMutableString string];
188178
NSUInteger deviceTokenLength = deviceToken.length;
189-
const unsigned char *bytes = deviceToken.bytes;
179+
const unsigned char *bytes = reinterpret_cast<const unsigned char *>(deviceToken.bytes);
190180
for (NSUInteger i = 0; i < deviceTokenLength; i++) {
191181
[hexString appendFormat:@"%02x", bytes[i]];
192182
}
@@ -258,32 +248,13 @@ - (void)handleRemoteNotificationRegistrationError:(NSNotification *)notification
258248
{
259249
NSError *error = notification.userInfo[@"error"];
260250
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+
};
265255
[self sendEventWithName:@"remoteNotificationRegistrationError" body:errorDetails];
266256
}
267257

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-
287258
RCT_EXPORT_METHOD(onFinishRemoteNotification:(NSString *)notificationId fetchResult:(UIBackgroundFetchResult)result) {
288259
RNCRemoteNotificationCallback completionHandler = self.remoteNotificationCallbacks[notificationId];
289260
if (!completionHandler) {
@@ -318,15 +289,9 @@ - (void)handleRegisterUserNotificationSettings:(NSNotification *)notification
318289
reject(kErrorUnableToRequestPermissions, nil, RCTErrorWithMessage(@"Requesting push notifications is currently unavailable in an app extension"));
319290
return;
320291
}
321-
322-
if (_requestPermissionsResolveBlock != nil) {
323-
RCTLogError(@"Cannot call requestPermissions twice before the first has returned.");
324-
return;
325-
}
326-
292+
327293
// Add a listener to make sure that startObserving has been called
328294
[self addListener:@"remoteNotificationsRegistered"];
329-
_requestPermissionsResolveBlock = resolve;
330295

331296
UIUserNotificationType types = UIUserNotificationTypeNone;
332297
if (permissions) {
@@ -343,9 +308,19 @@ - (void)handleRegisterUserNotificationSettings:(NSNotification *)notification
343308
types = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;
344309
}
345310

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+
}];
349324
}
350325

351326
RCT_EXPORT_METHOD(abandonPermissions)
@@ -356,16 +331,24 @@ - (void)handleRegisterUserNotificationSettings:(NSNotification *)notification
356331
RCT_EXPORT_METHOD(checkPermissions:(RCTResponseSenderBlock)callback)
357332
{
358333
if (RCTRunningInAppExtension()) {
359-
callback(@[@{@"alert": @NO, @"badge": @NO, @"sound": @NO}]);
334+
callback(@[RCTSettingsDictForUNNotificationSettings(NO, NO, NO)]);
360335
return;
361336
}
362337

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+
}
369352
}
370353

371354
RCT_EXPORT_METHOD(presentLocalNotification:(UILocalNotification *)notification)
@@ -464,13 +447,92 @@ - (void)handleRegisterUserNotificationSettings:(NSNotification *)notification
464447
}
465448
}
466449

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+
468530

469531
- (NSArray<NSString *> *)supportedEvents
470532
{
471533
return @[];
472534
}
473535

474-
#endif //TARGET_OS_TV
536+
#endif //TARGET_OS_TV / TARGET_OS_UIKITFORMAC
475537

476538
@end

0 commit comments

Comments
 (0)