Skip to content

Commit

Permalink
Integrare UNUserNotification
Browse files Browse the repository at this point in the history
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 #13036

Differential Revision: D4761828

Pulled By: javache

fbshipit-source-id: 216e44a64f1bf88b5ae3045d1fa6eca8a1278a71
  • Loading branch information
gperdomor authored and facebook-github-bot committed Mar 23, 2017
1 parent 9d377e9 commit 3df654e
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
34 changes: 34 additions & 0 deletions Libraries/PushNotificationIOS/PushNotificationIOS.js
Expand Up @@ -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
*/
Expand Down
56 changes: 56 additions & 0 deletions Libraries/PushNotificationIOS/RCTPushNotificationManager.m
Expand Up @@ -9,6 +9,8 @@

#import "RCTPushNotificationManager.h"

#import <UserNotifications/UserNotifications.h>

#import <React/RCTBridge.h>
#import <React/RCTConvert.h>
#import <React/RCTEventDispatcher.h>
Expand Down Expand Up @@ -97,6 +99,29 @@ @implementation RCTPushNotificationManager
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()
Expand Down Expand Up @@ -407,6 +432,37 @@ - (void)handleRegisterUserNotificationSettings:(NSNotification *)notification
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

0 comments on commit 3df654e

Please sign in to comment.