|
9 | 9 |
|
10 | 10 | #import "RCTPushNotificationManager.h"
|
11 | 11 |
|
| 12 | +#import <UserNotifications/UserNotifications.h> |
| 13 | + |
12 | 14 | #import <React/RCTBridge.h>
|
13 | 15 | #import <React/RCTConvert.h>
|
14 | 16 | #import <React/RCTEventDispatcher.h>
|
@@ -97,6 +99,29 @@ @implementation RCTPushNotificationManager
|
97 | 99 | return formattedLocalNotification;
|
98 | 100 | }
|
99 | 101 |
|
| 102 | +static NSDictionary *RCTFormatUNNotification(UNNotification *notification) |
| 103 | +{ |
| 104 | + NSMutableDictionary *formattedNotification = [NSMutableDictionary dictionary]; |
| 105 | + UNNotificationContent *content = notification.request.content; |
| 106 | + |
| 107 | + formattedNotification[@"identifier"] = notification.request.identifier; |
| 108 | + |
| 109 | + if (notification.date) { |
| 110 | + NSDateFormatter *formatter = [NSDateFormatter new]; |
| 111 | + [formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"]; |
| 112 | + NSString *dateString = [formatter stringFromDate:notification.date]; |
| 113 | + formattedNotification[@"date"] = dateString; |
| 114 | + } |
| 115 | + |
| 116 | + formattedNotification[@"title"] = RCTNullIfNil(content.title); |
| 117 | + formattedNotification[@"body"] = RCTNullIfNil(content.body); |
| 118 | + formattedNotification[@"category"] = RCTNullIfNil(content.categoryIdentifier); |
| 119 | + formattedNotification[@"thread-id"] = RCTNullIfNil(content.threadIdentifier); |
| 120 | + formattedNotification[@"userInfo"] = RCTNullIfNil(RCTJSONClean(content.userInfo)); |
| 121 | + |
| 122 | + return formattedNotification; |
| 123 | +} |
| 124 | + |
100 | 125 | #endif //TARGET_OS_TV
|
101 | 126 |
|
102 | 127 | RCT_EXPORT_MODULE()
|
@@ -407,6 +432,37 @@ - (void)handleRegisterUserNotificationSettings:(NSNotification *)notification
|
407 | 432 | callback(@[formattedScheduledLocalNotifications]);
|
408 | 433 | }
|
409 | 434 |
|
| 435 | +RCT_EXPORT_METHOD(removeAllDeliveredNotifications) |
| 436 | +{ |
| 437 | + if ([UNUserNotificationCenter class]) { |
| 438 | + UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; |
| 439 | + [center removeAllDeliveredNotifications]; |
| 440 | + } |
| 441 | +} |
| 442 | + |
| 443 | +RCT_EXPORT_METHOD(removeDeliveredNotifications:(NSArray<NSString *> *)identifiers) |
| 444 | +{ |
| 445 | + if ([UNUserNotificationCenter class]) { |
| 446 | + UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; |
| 447 | + [center removeDeliveredNotificationsWithIdentifiers:identifiers]; |
| 448 | + } |
| 449 | +} |
| 450 | + |
| 451 | +RCT_EXPORT_METHOD(getDeliveredNotifications:(RCTResponseSenderBlock)callback) |
| 452 | +{ |
| 453 | + if ([UNUserNotificationCenter class]) { |
| 454 | + UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; |
| 455 | + [center getDeliveredNotificationsWithCompletionHandler:^(NSArray<UNNotification *> *_Nonnull notifications) { |
| 456 | + NSMutableArray<NSDictionary *> *formattedNotifications = [NSMutableArray new]; |
| 457 | + |
| 458 | + for (UNNotification *notification in notifications) { |
| 459 | + [formattedNotifications addObject:RCTFormatUNNotification(notification)]; |
| 460 | + } |
| 461 | + callback(@[formattedNotifications]); |
| 462 | + }]; |
| 463 | + } |
| 464 | +} |
| 465 | + |
410 | 466 | #endif //TARGET_OS_TV
|
411 | 467 |
|
412 | 468 | @end
|
0 commit comments