Skip to content

Commit 9f4d66d

Browse files
ingridwangfacebook-github-bot
authored andcommitted
Migrate getScheduledLocalNotifications off of deprecated UILocalNotification (#40948)
Summary: Pull Request resolved: #40948 ## Changelog: [iOS][Breaking] alertAction is deprecated in PushNotificationIOS. getScheduledLocalNotifications now uses new iOS APIs which do not expose this property. Reviewed By: cipolleschi Differential Revision: D50275541 fbshipit-source-id: e4ecad858cd06350c749e7f5a837f36316656183
1 parent 63fdd1d commit 9f4d66d

2 files changed

Lines changed: 64 additions & 22 deletions

File tree

packages/react-native/Libraries/PushNotificationIOS/NativePushNotificationManagerIOS.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,20 @@ type Notification = {|
2323
// Actual type: string | number
2424
+fireDate?: ?number,
2525
+alertBody?: ?string,
26-
+alertAction?: ?string,
2726
+userInfo?: ?Object,
2827
+category?: ?string,
2928
// Actual type: 'year' | 'month' | 'week' | 'day' | 'hour' | 'minute'
3029
+repeatInterval?: ?string,
3130
+applicationIconBadgeNumber?: ?number,
3231
+isSilent?: ?boolean,
32+
/**
33+
* Custom notification sound to play. Write-only: soundName will be null when
34+
* accessing already created notifications using getScheduledLocalNotifications
35+
* or getDeliveredNotifications.
36+
*/
3337
+soundName?: ?string,
38+
/** DEPRECATED. This was used for iOS's legacy UILocalNotification. */
39+
+alertAction?: ?string,
3440
|};
3541

3642
export interface Spec extends TurboModule {

packages/react-native/Libraries/PushNotificationIOS/RCTPushNotificationManager.mm

Lines changed: 57 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ + (UILocalNotification *)UILocalNotification:(id)json
6969
return notification;
7070
}
7171

72+
@end
73+
74+
@implementation RCTConvert (UIBackgroundFetchResult)
75+
7276
RCT_ENUM_CONVERTER(
7377
UIBackgroundFetchResult,
7478
(@{
@@ -89,6 +93,7 @@ @implementation RCTPushNotificationManager
8993

9094
#if !TARGET_OS_UIKITFORMAC
9195

96+
/** DEPRECATED. UILocalNotification was deprecated in iOS 10. Please don't add new callsites. */
9297
static NSDictionary *RCTFormatLocalNotification(UILocalNotification *notification)
9398
{
9499
NSMutableDictionary *formattedLocalNotification = [NSMutableDictionary dictionary];
@@ -108,27 +113,56 @@ @implementation RCTPushNotificationManager
108113
return formattedLocalNotification;
109114
}
110115

111-
static NSDictionary *RCTFormatUNNotification(UNNotification *notification)
116+
/** For delivered notifications */
117+
static NSDictionary<NSString *, id> *RCTFormatUNNotification(UNNotification *notification)
112118
{
113-
NSMutableDictionary *formattedNotification = [NSMutableDictionary dictionary];
114-
UNNotificationContent *content = notification.request.content;
119+
NSMutableDictionary *formattedLocalNotification = [NSMutableDictionary dictionary];
120+
if (notification.date) {
121+
formattedLocalNotification[@"fireDate"] = RCTFormatNotificationDateFromNSDate(notification.date);
122+
}
123+
[formattedLocalNotification addEntriesFromDictionary:RCTFormatUNNotificationContent(notification.request.content)];
124+
return formattedLocalNotification;
125+
}
115126

116-
formattedNotification[@"identifier"] = notification.request.identifier;
127+
/** For scheduled notification requests */
128+
static NSDictionary<NSString *, id> *RCTFormatUNNotificationRequest(UNNotificationRequest *request)
129+
{
130+
NSMutableDictionary *formattedLocalNotification = [NSMutableDictionary dictionary];
131+
if (request.trigger) {
132+
NSDate *triggerDate = nil;
133+
if ([request.trigger isKindOfClass:[UNTimeIntervalNotificationTrigger class]]) {
134+
triggerDate = [(UNTimeIntervalNotificationTrigger *)request.trigger nextTriggerDate];
135+
} else if ([request.trigger isKindOfClass:[UNCalendarNotificationTrigger class]]) {
136+
triggerDate = [(UNCalendarNotificationTrigger *)request.trigger nextTriggerDate];
137+
}
117138

118-
if (notification.date) {
119-
NSDateFormatter *formatter = [NSDateFormatter new];
120-
[formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"];
121-
NSString *dateString = [formatter stringFromDate:notification.date];
122-
formattedNotification[@"date"] = dateString;
139+
if (triggerDate) {
140+
formattedLocalNotification[@"fireDate"] = RCTFormatNotificationDateFromNSDate(triggerDate);
141+
}
123142
}
143+
[formattedLocalNotification addEntriesFromDictionary:RCTFormatUNNotificationContent(request.content)];
144+
return formattedLocalNotification;
145+
}
124146

125-
formattedNotification[@"title"] = RCTNullIfNil(content.title);
126-
formattedNotification[@"body"] = RCTNullIfNil(content.body);
127-
formattedNotification[@"category"] = RCTNullIfNil(content.categoryIdentifier);
128-
formattedNotification[@"thread-id"] = RCTNullIfNil(content.threadIdentifier);
129-
formattedNotification[@"userInfo"] = RCTNullIfNil(RCTJSONClean(content.userInfo));
147+
static NSDictionary<NSString *, id> *RCTFormatUNNotificationContent(UNNotificationContent *content)
148+
{
149+
// Note: soundName is not set because this can't be read from UNNotificationSound.
150+
// Note: alertAction is no longer relevant with UNNotification
151+
NSMutableDictionary *formattedLocalNotification = [NSMutableDictionary dictionary];
152+
formattedLocalNotification[@"alertTitle"] = RCTNullIfNil(content.title);
153+
formattedLocalNotification[@"alertBody"] = RCTNullIfNil(content.body);
154+
formattedLocalNotification[@"userInfo"] = RCTNullIfNil(RCTJSONClean(content.userInfo));
155+
formattedLocalNotification[@"category"] = content.categoryIdentifier;
156+
formattedLocalNotification[@"applicationIconBadgeNumber"] = content.badge;
157+
formattedLocalNotification[@"remote"] = @NO;
158+
return formattedLocalNotification;
159+
}
130160

131-
return formattedNotification;
161+
static NSString *RCTFormatNotificationDateFromNSDate(NSDate *date)
162+
{
163+
NSDateFormatter *formatter = [NSDateFormatter new];
164+
[formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"];
165+
return [formatter stringFromDate:date];
132166
}
133167

134168
#endif // TARGET_OS_UIKITFORMAC
@@ -495,12 +529,14 @@ - (void)handleRemoteNotificationRegistrationError:(NSNotification *)notification
495529

496530
RCT_EXPORT_METHOD(getScheduledLocalNotifications : (RCTResponseSenderBlock)callback)
497531
{
498-
NSArray<UILocalNotification *> *scheduledLocalNotifications = RCTSharedApplication().scheduledLocalNotifications;
499-
NSMutableArray<NSDictionary *> *formattedScheduledLocalNotifications = [NSMutableArray new];
500-
for (UILocalNotification *notification in scheduledLocalNotifications) {
501-
[formattedScheduledLocalNotifications addObject:RCTFormatLocalNotification(notification)];
502-
}
503-
callback(@[ formattedScheduledLocalNotifications ]);
532+
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
533+
[center getPendingNotificationRequestsWithCompletionHandler:^(NSArray<UNNotificationRequest *> *_Nonnull requests) {
534+
NSMutableArray<NSDictionary *> *formattedScheduledLocalNotifications = [NSMutableArray new];
535+
for (UNNotificationRequest *request in requests) {
536+
[formattedScheduledLocalNotifications addObject:RCTFormatUNNotificationRequest(request)];
537+
}
538+
callback(@[ formattedScheduledLocalNotifications ]);
539+
}];
504540
}
505541

506542
RCT_EXPORT_METHOD(removeAllDeliveredNotifications)

0 commit comments

Comments
 (0)