@@ -69,6 +69,10 @@ + (UILocalNotification *)UILocalNotification:(id)json
6969 return notification;
7070}
7171
72+ @end
73+
74+ @implementation RCTConvert (UIBackgroundFetchResult)
75+
7276RCT_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. */
9297static 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
496530RCT_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
506542RCT_EXPORT_METHOD (removeAllDeliveredNotifications)
0 commit comments