Skip to content

Commit

Permalink
Migrate getScheduledLocalNotifications off of deprecated UILocalNotif…
Browse files Browse the repository at this point in the history
…ication (#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
  • Loading branch information
Ingrid Wang authored and facebook-github-bot committed Oct 18, 2023
1 parent 63fdd1d commit 9f4d66d
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,20 @@ type Notification = {|
// Actual type: string | number
+fireDate?: ?number,
+alertBody?: ?string,
+alertAction?: ?string,
+userInfo?: ?Object,
+category?: ?string,
// Actual type: 'year' | 'month' | 'week' | 'day' | 'hour' | 'minute'
+repeatInterval?: ?string,
+applicationIconBadgeNumber?: ?number,
+isSilent?: ?boolean,
/**
* Custom notification sound to play. Write-only: soundName will be null when
* accessing already created notifications using getScheduledLocalNotifications
* or getDeliveredNotifications.
*/
+soundName?: ?string,
/** DEPRECATED. This was used for iOS's legacy UILocalNotification. */
+alertAction?: ?string,
|};

export interface Spec extends TurboModule {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ + (UILocalNotification *)UILocalNotification:(id)json
return notification;
}

@end

@implementation RCTConvert (UIBackgroundFetchResult)

RCT_ENUM_CONVERTER(
UIBackgroundFetchResult,
(@{
Expand All @@ -89,6 +93,7 @@ @implementation RCTPushNotificationManager

#if !TARGET_OS_UIKITFORMAC

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

static NSDictionary *RCTFormatUNNotification(UNNotification *notification)
/** For delivered notifications */
static NSDictionary<NSString *, id> *RCTFormatUNNotification(UNNotification *notification)
{
NSMutableDictionary *formattedNotification = [NSMutableDictionary dictionary];
UNNotificationContent *content = notification.request.content;
NSMutableDictionary *formattedLocalNotification = [NSMutableDictionary dictionary];
if (notification.date) {
formattedLocalNotification[@"fireDate"] = RCTFormatNotificationDateFromNSDate(notification.date);
}
[formattedLocalNotification addEntriesFromDictionary:RCTFormatUNNotificationContent(notification.request.content)];
return formattedLocalNotification;
}

formattedNotification[@"identifier"] = notification.request.identifier;
/** For scheduled notification requests */
static NSDictionary<NSString *, id> *RCTFormatUNNotificationRequest(UNNotificationRequest *request)
{
NSMutableDictionary *formattedLocalNotification = [NSMutableDictionary dictionary];
if (request.trigger) {
NSDate *triggerDate = nil;
if ([request.trigger isKindOfClass:[UNTimeIntervalNotificationTrigger class]]) {
triggerDate = [(UNTimeIntervalNotificationTrigger *)request.trigger nextTriggerDate];
} else if ([request.trigger isKindOfClass:[UNCalendarNotificationTrigger class]]) {
triggerDate = [(UNCalendarNotificationTrigger *)request.trigger nextTriggerDate];
}

if (notification.date) {
NSDateFormatter *formatter = [NSDateFormatter new];
[formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"];
NSString *dateString = [formatter stringFromDate:notification.date];
formattedNotification[@"date"] = dateString;
if (triggerDate) {
formattedLocalNotification[@"fireDate"] = RCTFormatNotificationDateFromNSDate(triggerDate);
}
}
[formattedLocalNotification addEntriesFromDictionary:RCTFormatUNNotificationContent(request.content)];
return formattedLocalNotification;
}

formattedNotification[@"title"] = RCTNullIfNil(content.title);
formattedNotification[@"body"] = RCTNullIfNil(content.body);
formattedNotification[@"category"] = RCTNullIfNil(content.categoryIdentifier);
formattedNotification[@"thread-id"] = RCTNullIfNil(content.threadIdentifier);
formattedNotification[@"userInfo"] = RCTNullIfNil(RCTJSONClean(content.userInfo));
static NSDictionary<NSString *, id> *RCTFormatUNNotificationContent(UNNotificationContent *content)
{
// Note: soundName is not set because this can't be read from UNNotificationSound.
// Note: alertAction is no longer relevant with UNNotification
NSMutableDictionary *formattedLocalNotification = [NSMutableDictionary dictionary];
formattedLocalNotification[@"alertTitle"] = RCTNullIfNil(content.title);
formattedLocalNotification[@"alertBody"] = RCTNullIfNil(content.body);
formattedLocalNotification[@"userInfo"] = RCTNullIfNil(RCTJSONClean(content.userInfo));
formattedLocalNotification[@"category"] = content.categoryIdentifier;
formattedLocalNotification[@"applicationIconBadgeNumber"] = content.badge;
formattedLocalNotification[@"remote"] = @NO;
return formattedLocalNotification;
}

return formattedNotification;
static NSString *RCTFormatNotificationDateFromNSDate(NSDate *date)
{
NSDateFormatter *formatter = [NSDateFormatter new];
[formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"];
return [formatter stringFromDate:date];
}

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

RCT_EXPORT_METHOD(getScheduledLocalNotifications : (RCTResponseSenderBlock)callback)
{
NSArray<UILocalNotification *> *scheduledLocalNotifications = RCTSharedApplication().scheduledLocalNotifications;
NSMutableArray<NSDictionary *> *formattedScheduledLocalNotifications = [NSMutableArray new];
for (UILocalNotification *notification in scheduledLocalNotifications) {
[formattedScheduledLocalNotifications addObject:RCTFormatLocalNotification(notification)];
}
callback(@[ formattedScheduledLocalNotifications ]);
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center getPendingNotificationRequestsWithCompletionHandler:^(NSArray<UNNotificationRequest *> *_Nonnull requests) {
NSMutableArray<NSDictionary *> *formattedScheduledLocalNotifications = [NSMutableArray new];
for (UNNotificationRequest *request in requests) {
[formattedScheduledLocalNotifications addObject:RCTFormatUNNotificationRequest(request)];
}
callback(@[ formattedScheduledLocalNotifications ]);
}];
}

RCT_EXPORT_METHOD(removeAllDeliveredNotifications)
Expand Down

0 comments on commit 9f4d66d

Please sign in to comment.