Integrare UNUserNotification
Summary: Implementing removeAllDeliveredNotifications and removeDeliveredNotifications for remove notifications from notification center, and getDeliveredNotifications Thanks for submitting a PR! Please read these instructions carefully: - [x] Explain the **motivation** for making this change. - [x] Provide a **test plan** demonstrating that the code is solid. - [x] Match the **code formatting** of the rest of the codebase. - [x] Target the `master` branch, NOT a "stable" branch. Currently, calling PushNotificationIOS.cancelAllLocalNotifications not remove the notification from the Notification Center In iOS 10, a new UNUserNotification class was introduced, this class has a method which get and remove the notifications from notification center This PR try to solve that. In my case, i'm working with an messaging app, every message is a new notification, when the user tap a notification, the app is opened and the rest of notifications should be gon Closes https://github.com/facebook/react-native/pull/13036 Differential Revision: D4761828 Pulled By: javache fbshipit-source-id: 216e44a64f1bf88b5ae3045d1fa6eca8a1278a71
This commit is contained in:
parent
9d377e98a0
commit
3df654e28e
|
@ -164,6 +164,40 @@ class PushNotificationIOS {
|
|||
RCTPushNotificationManager.cancelAllLocalNotifications();
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove all delivered notifications from Notification Center
|
||||
*/
|
||||
static removeAllDeliveredNotifications(): void {
|
||||
RCTPushNotificationManager.removeAllDeliveredNotifications();
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides you with a list of the app’s notifications that are still displayed in Notification Center
|
||||
*
|
||||
* @param callback Function which receive an array of delivered notifications
|
||||
*
|
||||
* A delivered notification is an object containing:
|
||||
*
|
||||
* - `identifier` : The identifier of this notification.
|
||||
* - `title` : The title of this notification.
|
||||
* - `body` : The body of this notification.
|
||||
* - `category` : The category of this notification, if has one.
|
||||
* - `userInfo` : An optional object containing additional notification data.
|
||||
* - `thread-id` : The thread identifier of this notification, if has one.
|
||||
*/
|
||||
static getDeliveredNotifications(callback: (notifications: [Object]) => void): void {
|
||||
RCTPushNotificationManager.getDeliveredNotifications(callback);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the specified notifications from Notification Center
|
||||
*
|
||||
* @param identifiers Array of notification identifiers
|
||||
*/
|
||||
static removeDeliveredNotifications(identifiers: [string]): void {
|
||||
RCTPushNotificationManager.removeDeliveredNotifications(identifiers);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the badge number for the app icon on the home screen
|
||||
*/
|
||||
|
|
|
@ -9,6 +9,8 @@
|
|||
|
||||
#import "RCTPushNotificationManager.h"
|
||||
|
||||
#import <UserNotifications/UserNotifications.h>
|
||||
|
||||
#import <React/RCTBridge.h>
|
||||
#import <React/RCTConvert.h>
|
||||
#import <React/RCTEventDispatcher.h>
|
||||
|
@ -97,6 +99,29 @@ static NSDictionary *RCTFormatLocalNotification(UILocalNotification *notificatio
|
|||
return formattedLocalNotification;
|
||||
}
|
||||
|
||||
static NSDictionary *RCTFormatUNNotification(UNNotification *notification)
|
||||
{
|
||||
NSMutableDictionary *formattedNotification = [NSMutableDictionary dictionary];
|
||||
UNNotificationContent *content = notification.request.content;
|
||||
|
||||
formattedNotification[@"identifier"] = notification.request.identifier;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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));
|
||||
|
||||
return formattedNotification;
|
||||
}
|
||||
|
||||
#endif //TARGET_OS_TV
|
||||
|
||||
RCT_EXPORT_MODULE()
|
||||
|
@ -407,6 +432,37 @@ RCT_EXPORT_METHOD(getScheduledLocalNotifications:(RCTResponseSenderBlock)callbac
|
|||
callback(@[formattedScheduledLocalNotifications]);
|
||||
}
|
||||
|
||||
RCT_EXPORT_METHOD(removeAllDeliveredNotifications)
|
||||
{
|
||||
if ([UNUserNotificationCenter class]) {
|
||||
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
|
||||
[center removeAllDeliveredNotifications];
|
||||
}
|
||||
}
|
||||
|
||||
RCT_EXPORT_METHOD(removeDeliveredNotifications:(NSArray<NSString *> *)identifiers)
|
||||
{
|
||||
if ([UNUserNotificationCenter class]) {
|
||||
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
|
||||
[center removeDeliveredNotificationsWithIdentifiers:identifiers];
|
||||
}
|
||||
}
|
||||
|
||||
RCT_EXPORT_METHOD(getDeliveredNotifications:(RCTResponseSenderBlock)callback)
|
||||
{
|
||||
if ([UNUserNotificationCenter class]) {
|
||||
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
|
||||
[center getDeliveredNotificationsWithCompletionHandler:^(NSArray<UNNotification *> *_Nonnull notifications) {
|
||||
NSMutableArray<NSDictionary *> *formattedNotifications = [NSMutableArray new];
|
||||
|
||||
for (UNNotification *notification in notifications) {
|
||||
[formattedNotifications addObject:RCTFormatUNNotification(notification)];
|
||||
}
|
||||
callback(@[formattedNotifications]);
|
||||
}];
|
||||
}
|
||||
}
|
||||
|
||||
#endif //TARGET_OS_TV
|
||||
|
||||
@end
|
||||
|
|
Loading…
Reference in New Issue