Commit d945506c authored by Ryan Eberhardt's avatar Ryan Eberhardt Committed by Ryan Eberhardt

Use UserNotifications to create local notifications

a15e038a adds support for enumerating/dismissing delivered notifications
by ID, but UserNotifications notifications and UILocalNotification
notifications have a different set of IDs (a notification created using
UILocalNotification will have an ID that does not match the
notification's ID in the UserNotifications world and hence cannot be
used to dismiss that notification). This commit creates the
notifications using UserNotifications if the user is on iOS 10.
parent 6b7bb9c9
......@@ -326,7 +326,6 @@ Example:
let localNotification = NotificationsIOS.localNotification({
alertBody: "Local notificiation!",
alertTitle: "Local Notification Title",
alertAction: "Click here to open",
soundName: "chime.aiff",
category: "SOME_CATEGORY",
userInfo: { }
......@@ -338,7 +337,7 @@ Notification object contains:
- **`fireDate`**- The date and time when the system should deliver the notification (optinal - default is immidiate dispatch).
- `alertBody`- The message displayed in the notification alert.
- `alertTitle`- The title of the notification, displayed in the notifications center.
- `alertAction`- The "action" displayed beneath an actionable notification.
- `alertAction`- The "action" displayed beneath an actionable notification on the lockscreen (e.g. "Slide to **open**"). Note that Apple no longer shows this in iOS 10.
- `soundName`- The sound played when the notification is fired (optional).
- `category`- The category of this notification, required for [interactive notifications](#interactive--actionable-notifications-ios-only) (optional).
- `userInfo`- An optional object containing additional notification data.
......@@ -357,8 +356,9 @@ NotificationsAndroid.localNotification({
Upon notification opening (tapping by the device user), all data fields will be delivered as-is).
### Cancel Local Notification
The `NotificationsIOS.localNotification()` and `NotificationsAndroid.localNotification()` methods return unique `notificationId` values, which can be used in order to cancel specific local notifications. You can cancel local notification by calling `NotificationsIOS.cancelLocalNotification(notificationId)` or `NotificationsAndroid.cancelLocalNotification(notificationId)`.
### Cancel Scheduled Local Notifications
The `NotificationsIOS.localNotification()` and `NotificationsAndroid.localNotification()` methods return unique `notificationId` values, which can be used in order to cancel specific local notifications that were scheduled for delivery on `fireDate` and have not yet been delivered. You can cancel local notification by calling `NotificationsIOS.cancelLocalNotification(notificationId)` or `NotificationsAndroid.cancelLocalNotification(notificationId)`.
Example (iOS):
......@@ -366,7 +366,6 @@ Example (iOS):
let someLocalNotification = NotificationsIOS.localNotification({
alertBody: "Local notificiation!",
alertTitle: "Local Notification Title",
alertAction: "Click here to open",
soundName: "chime.aiff",
category: "SOME_CATEGORY",
userInfo: { }
......@@ -375,12 +374,26 @@ let someLocalNotification = NotificationsIOS.localNotification({
NotificationsIOS.cancelLocalNotification(someLocalNotification);
```
### Cancel All Local Notifications (iOS-only!)
To cancel all local notifications (**iOS only!**), use `cancelAllLocalNotifications()`:
```javascript
NotificationsIOS.cancelAllLocalNotifications();
```
### Cancel Delivered Local Notifications (iOS 10+ only)
To dismiss notifications from the notification center that have already been shown to the user, call `NotificationsIOS.removeDeliveredNotifications([notificationId])`:
```javascript
let someLocalNotification = NotificationsIOS.localNotification({...});
NotificationsIOS.removeDeliveredNotifications([someLocalNotification]);
```
Call `removeAllDeliveredNotifications()` to dismiss all delivered notifications
(note that this will dismiss push notifications in addition to local
notifications).
---
## Managed Notifications (iOS only)
......@@ -444,13 +457,13 @@ Now the server should push the notification a bit differently- background instea
### getDeliveredNotifications
`PushNotification.getDeliveredNotifications(callback: (notifications: [Object]) => void)`
`PushNotification.getDeliveredNotifications(callback: (notifications: Array<Object>) => void)`
Provides you with a list of the app’s notifications that are still displayed in Notification Center.
### removeDeliveredNotifications
`PushNotification.removeDeliveredNotifications(identifiers: [string])`
`PushNotification.removeDeliveredNotifications(identifiers: Array<String>)`
Removes the specified notifications from Notification Center.
......
......@@ -17,6 +17,8 @@
#import "RNNotificationsBridgeQueue.h"
#import <UserNotifications/UserNotifications.h>
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
NSString* const RNNotificationCreateAction = @"CREATE";
NSString* const RNNotificationClearAction = @"CLEAR";
......@@ -108,6 +110,38 @@ RCT_ENUM_CONVERTER(UIUserNotificationActionBehavior, (@{
}
@end
@implementation RCTConvert (UNNotificationRequest)
+ (UNNotificationRequest *)UNNotificationRequest:(id)json withId:(NSString*)notificationId
{
NSDictionary<NSString *, id> *details = [self NSDictionary:json];
UNMutableNotificationContent *content = [UNMutableNotificationContent new];
content.body = [RCTConvert NSString:details[@"alertBody"]];
content.title = [RCTConvert NSString:details[@"alertTitle"]];
content.sound = [RCTConvert NSString:details[@"soundName"]]
? [UNNotificationSound soundNamed:[RCTConvert NSString:details[@"soundName"]]]
: [UNNotificationSound defaultSound];
content.userInfo = [RCTConvert NSDictionary:details[@"userInfo"]] ?: @{};
content.categoryIdentifier = [RCTConvert NSString:details[@"category"]];
NSDate *triggerDate = [RCTConvert NSDate:details[@"fireDate"]];
UNCalendarNotificationTrigger *trigger = nil;
if (triggerDate != nil) {
NSDateComponents *triggerDateComponents = [[NSCalendar currentCalendar]
components:NSCalendarUnitYear +
NSCalendarUnitMonth + NSCalendarUnitDay +
NSCalendarUnitHour + NSCalendarUnitMinute +
NSCalendarUnitSecond + NSCalendarUnitTimeZone
fromDate:triggerDate];
trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:triggerDateComponents
repeats:NO];
}
return [UNNotificationRequest requestWithIdentifier:notificationId
content:content trigger:trigger];
}
@end
static NSDictionary *RCTFormatUNNotification(UNNotification *notification)
{
NSMutableDictionary *formattedNotification = [NSMutableDictionary dictionary];
......@@ -545,6 +579,10 @@ RCT_EXPORT_METHOD(consumeBackgroundQueue)
RCT_EXPORT_METHOD(localNotification:(NSDictionary *)notification withId:(NSString *)notificationId)
{
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"10")) {
UNNotificationRequest* localNotification = [RCTConvert UNNotificationRequest:notification withId:notificationId];
[[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:localNotification withCompletionHandler:nil];
} else {
UILocalNotification* localNotification = [RCTConvert UILocalNotification:notification];
NSMutableArray* userInfo = localNotification.userInfo.mutableCopy;
[userInfo setValue:notificationId forKey:@"__id"];
......@@ -555,10 +593,15 @@ RCT_EXPORT_METHOD(localNotification:(NSDictionary *)notification withId:(NSStrin
} else {
[[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];
}
}
}
RCT_EXPORT_METHOD(cancelLocalNotification:(NSString *)notificationId)
{
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"10")) {
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center removePendingNotificationRequestsWithIdentifiers:@[notificationId]];
} else {
for (UILocalNotification* notification in [UIApplication sharedApplication].scheduledLocalNotifications) {
NSDictionary* notificationInfo = notification.userInfo;
......@@ -566,6 +609,7 @@ RCT_EXPORT_METHOD(cancelLocalNotification:(NSString *)notificationId)
[[UIApplication sharedApplication] cancelLocalNotification:notification];
}
}
}
}
RCT_EXPORT_METHOD(cancelAllLocalNotifications)
......
......@@ -229,7 +229,7 @@ export default class NotificationsIOS {
*
* @param identifiers Array of notification identifiers
*/
static removeDeliveredNotifications(identifiers: [string]) {
static removeDeliveredNotifications(identifiers: Array<String>) {
return NativeRNNotifications.removeDeliveredNotifications(identifiers);
}
......@@ -248,7 +248,7 @@ export default class NotificationsIOS {
* - `thread-id` : The thread identifier of this notification, if has one.
* - `fireDate` : The date and time when the system should deliver the notification. if not specified, the notification will be dispatched immediately.
*/
static getDeliveredNotifications(callback: (notifications: [Object]) => void) {
static getDeliveredNotifications(callback: (notifications: Array<Object>) => void) {
return NativeRNNotifications.getDeliveredNotifications(callback);
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment