diff --git a/RNNotifications/RCTConvert+Notifications.h b/RNNotifications/RCTConvert+Notifications.h index 519ce17c0b51c8c631d45de4e1a40bfc7cf211d9..8b759d535dce1ad28214e229fc466b86e08ce754 100644 --- a/RNNotifications/RCTConvert+Notifications.h +++ b/RNNotifications/RCTConvert+Notifications.h @@ -1,18 +1,6 @@ #import "RCTConvert.h" @import UserNotifications; -@interface RCTConvert (UIUserNotificationActivationMode) - -@end - -@interface RCTConvert (UIUserNotificationActionContext) - -@end - -@interface RCTConvert (UIUserNotificationActionBehavior) - -@end - @interface RCTConvert (UIMutableUserNotificationAction) + (UIMutableUserNotificationAction *)UIMutableUserNotificationAction:(id)json; @end @@ -21,10 +9,6 @@ + (UNNotificationCategory *)UNMutableUserNotificationCategory:(id)json; @end -@interface RCTConvert (UILocalNotification) -+ (UILocalNotification *)UILocalNotification:(id)json; -@end - @interface RCTConvert (UNNotificationRequest) + (UNNotificationRequest *)UNNotificationRequest:(id)json withId:(NSString*)notificationId; @end diff --git a/RNNotifications/RCTConvert+Notifications.m b/RNNotifications/RCTConvert+Notifications.m index 431cc4e74752e8c6d953267d5bb4abc438fcedc1..d74a592c770f32dc54480ccc9e98de9c49ec2655 100644 --- a/RNNotifications/RCTConvert+Notifications.m +++ b/RNNotifications/RCTConvert+Notifications.m @@ -1,8 +1,6 @@ #import "RCTConvert+Notifications.h" -/* - * Converters for Interactive Notifications - */ + @implementation RCTConvert (UIUserNotificationActivationMode) RCT_ENUM_CONVERTER(UIUserNotificationActivationMode, (@{ @"foreground": @(UIUserNotificationActivationModeForeground), @@ -10,13 +8,6 @@ RCT_ENUM_CONVERTER(UIUserNotificationActivationMode, (@{ }), UIUserNotificationActivationModeForeground, integerValue) @end -//@implementation RCTConvert (UIUserNotificationActionContext) -//RCT_ENUM_CONVERTER(UIUserNotificationActionContext, (@{ -// @"default": @(UIUserNotificationActionContextDefault), -// @"minimal": @(UIUserNotificationActionContextMinimal) -// }), UIUserNotificationActionContextDefault, integerValue) -//@end - @implementation RCTConvert (UNNotificationActionOptions) + (UNNotificationActionOptions)UNUserNotificationActionOptions:(id)json { @@ -37,8 +28,8 @@ RCT_ENUM_CONVERTER(UIUserNotificationActivationMode, (@{ @end @implementation RCTConvert (UNMutableUserNotificationAction) -+ (UNNotificationAction *)UNMutableUserNotificationAction:(id)json -{ + ++ (UNNotificationAction *)UNMutableUserNotificationAction:(id)json { UNNotificationAction* action; NSDictionary *details = [self NSDictionary:json]; @@ -48,15 +39,14 @@ RCT_ENUM_CONVERTER(UIUserNotificationActivationMode, (@{ action = [UNNotificationAction actionWithIdentifier:details[@"identifier"] title:details[@"title"] options:[RCTConvert UNUserNotificationActionOptions:details]]; } -// action.behavior = [RCTConvert UIUserNotificationActionBehavior:details[@"behavior"]]; - return action; } + @end @implementation RCTConvert (UNMutableUserNotificationCategory) -+ (UNNotificationCategory *)UNMutableUserNotificationCategory:(id)json -{ + ++ (UNNotificationCategory *)UNMutableUserNotificationCategory:(id)json { NSDictionary *details = [self NSDictionary:json]; NSMutableArray* actions = [NSMutableArray new]; @@ -68,39 +58,20 @@ RCT_ENUM_CONVERTER(UIUserNotificationActivationMode, (@{ return category; } -@end -@implementation RCTConvert (UILocalNotification) -+ (UILocalNotification *)UILocalNotification:(id)json -{ - NSDictionary *details = [self NSDictionary:json]; - - UILocalNotification* notification = [UILocalNotification new]; - notification.fireDate = [RCTConvert NSDate:details[@"fireDate"]]; - notification.alertBody = [RCTConvert NSString:details[@"alertBody"]]; - notification.alertTitle = [RCTConvert NSString:details[@"alertTitle"]]; - notification.alertAction = [RCTConvert NSString:details[@"alertAction"]]; - notification.soundName = [RCTConvert NSString:details[@"soundName"]] ?: UILocalNotificationDefaultSoundName; - if ([RCTConvert BOOL:details[@"silent"]]) { - notification.soundName = nil; - } - notification.userInfo = [RCTConvert NSDictionary:details[@"userInfo"]] ?: @{}; - notification.category = [RCTConvert NSString:details[@"category"]]; - - return notification; -} @end @implementation RCTConvert (UNNotificationRequest) + + (UNNotificationRequest *)UNNotificationRequest:(id)json withId:(NSString*)notificationId { NSDictionary *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"]]] + content.body = [RCTConvert NSString:details[@"body"]]; + content.title = [RCTConvert NSString:details[@"title"]]; + content.sound = [RCTConvert NSString:details[@"sound"]] + ? [UNNotificationSound soundNamed:[RCTConvert NSString:details[@"sound"]]] : [UNNotificationSound defaultSound]; if ([RCTConvert BOOL:details[@"silent"]]) { content.sound = nil; @@ -124,9 +95,11 @@ RCT_ENUM_CONVERTER(UIUserNotificationActivationMode, (@{ return [UNNotificationRequest requestWithIdentifier:notificationId content:content trigger:trigger]; } + @end @implementation RCTConvert (UNNotification) + + (NSDictionary *)UNNotificationPayload:(UNNotification *)notification { NSMutableDictionary *formattedNotification = [NSMutableDictionary dictionary]; UNNotificationContent *content = notification.request.content; diff --git a/RNNotifications/RNCommandsHandler.m b/RNNotifications/RNCommandsHandler.m index d5fca1164a9f5144a7a8b2834e24df69a52ba518..5544089f0fc74e9dcf887c756e3f7434a7a107ca 100644 --- a/RNNotifications/RNCommandsHandler.m +++ b/RNNotifications/RNCommandsHandler.m @@ -54,21 +54,15 @@ } - (void)cancelAllLocalNotifications { - [RCTSharedApplication() cancelAllLocalNotifications]; + [_notificationCenter cancelAllLocalNotifications]; } - (void)isRegisteredForRemoteNotifications:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject { - BOOL ans = [[[UIApplication sharedApplication] currentUserNotificationSettings] types] != 0; - resolve(@(ans)); + [_notificationCenter isRegisteredForRemoteNotifications:resolve]; } - (void)checkPermissions:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject { - UIUserNotificationSettings *currentSettings = [[UIApplication sharedApplication] currentUserNotificationSettings]; - resolve(@{ - @"badge": @((currentSettings.types & UIUserNotificationTypeBadge) > 0), - @"sound": @((currentSettings.types & UIUserNotificationTypeSound) > 0), - @"alert": @((currentSettings.types & UIUserNotificationTypeAlert) > 0), - }); + [_notificationCenter checkPermissions:resolve]; } - (void)removeAllDeliveredNotifications { diff --git a/RNNotifications/RNEventEmitter.h b/RNNotifications/RNEventEmitter.h index e2008e2558e43081a62dcdf74e9fe1bce6d91c5f..1e7239b5572a73bb380ca5b8f20a361f78fb1c61 100644 --- a/RNNotifications/RNEventEmitter.h +++ b/RNNotifications/RNEventEmitter.h @@ -4,7 +4,6 @@ static NSString* const RNRegistered = @"remoteNotificationsR static NSString* const RNRegistrationFailed = @"remoteNotificationsRegistrationFailed"; static NSString* const RNPushKitRegistered = @"pushKitRegistered"; static NSString* const RNNotificationReceivedForeground = @"notificationReceivedForeground"; -static NSString* const RNNotificationReceivedBackground = @"notificationReceivedBackground"; static NSString* const RNNotificationOpened = @"notificationOpened"; diff --git a/RNNotifications/RNEventEmitter.m b/RNNotifications/RNEventEmitter.m index f8c9edc0ca7f90228cc6812bfdd3fbad2b361a2c..9078003ecb6c5bed755c0b4147384b139d7f311e 100644 --- a/RNNotifications/RNEventEmitter.m +++ b/RNNotifications/RNEventEmitter.m @@ -9,7 +9,6 @@ RCT_EXPORT_MODULE(); RNRegistrationFailed, RNPushKitRegistered, RNNotificationReceivedForeground, - RNNotificationReceivedBackground, RNNotificationOpened]; } diff --git a/RNNotifications/RNNotificationCenter.h b/RNNotifications/RNNotificationCenter.h index b5da6a3cc88c818cabad96b19698ae7333737f1a..472ef2eee561fdd1a2a367cd530ead586f9dbb21 100644 --- a/RNNotifications/RNNotificationCenter.h +++ b/RNNotifications/RNNotificationCenter.h @@ -4,11 +4,22 @@ @interface RNNotificationCenter : NSObject +- (void)isRegisteredForRemoteNotifications:(RCTPromiseResolveBlock)resolve; + - (void)requestPermissionsWithCategories:(NSArray *)json; + +- (void)checkPermissions:(RCTPromiseResolveBlock)resolve; + - (void)sendLocalNotification:(NSDictionary *)notification withId:(NSString *)notificationId; + - (void)cancelLocalNotification:(NSString *)notificationId; + - (void)removeAllDeliveredNotifications; + - (void)removeDeliveredNotifications:(NSArray *)identifiers; + - (void)getDeliveredNotifications:(RCTResponseSenderBlock)callback; +- (void)cancelAllLocalNotifications; + @end diff --git a/RNNotifications/RNNotificationCenter.m b/RNNotifications/RNNotificationCenter.m index 803ba56e7d819c826a8d50d587fb83440ac07227..2f70dbc130be14fa0884cdfa80ac179c3c3729f4 100644 --- a/RNNotifications/RNNotificationCenter.m +++ b/RNNotifications/RNNotificationCenter.m @@ -65,4 +65,28 @@ }]; } +- (void)cancelAllLocalNotifications { + [[UNUserNotificationCenter currentNotificationCenter] removeAllPendingNotificationRequests]; +} + +- (void)isRegisteredForRemoteNotifications:(RCTPromiseResolveBlock)resolve { + [[UNUserNotificationCenter currentNotificationCenter] getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) { + if (settings.alertSetting == UNNotificationSettingEnabled || settings.soundSetting == UNNotificationSettingEnabled || settings.badgeSetting == UNNotificationSettingEnabled) { + resolve(@(YES)); + } else { + resolve(@(NO)); + } + }]; +} + +- (void)checkPermissions:(RCTPromiseResolveBlock)resolve { + [[UNUserNotificationCenter currentNotificationCenter] getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) { + resolve(@{ + @"badge": @(settings.badgeSetting == UNNotificationSettingEnabled), + @"sound": @(settings.soundSetting == UNNotificationSettingEnabled), + @"alert": @(settings.alertSetting == UNNotificationSettingEnabled), + }); + }]; +} + @end diff --git a/docs/localNotifications.md b/docs/localNotifications.md index 9747146c6d99bec2f91fa6573abe2075ffb5b486..8f1d2b94838dec8baa1359ca8c0d56785289fb8d 100644 --- a/docs/localNotifications.md +++ b/docs/localNotifications.md @@ -10,9 +10,9 @@ Example: ```javascript let localNotification = NotificationsIOS.localNotification({ - alertBody: "Local notificiation!", - alertTitle: "Local Notification Title", - soundName: "chime.aiff", + body: "Local notificiation!", + title: "Local Notification Title", + sound: "chime.aiff", silent: false, category: "SOME_CATEGORY", userInfo: { } @@ -22,10 +22,10 @@ let localNotification = NotificationsIOS.localNotification({ 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. +- `body`- The message displayed in the notification alert. +- `title`- The title of the notification, displayed in the notifications center. - `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 -- will play default sound if unspecified). This must be the filename of a sound included in the application bundle; the sound must be 30 seconds or less and should be encoded with linear PCM or IMA4. +- `sound`- The sound played when the notification is fired (optional -- will play default sound if unspecified). This must be the filename of a sound included in the application bundle; the sound must be 30 seconds or less and should be encoded with linear PCM or IMA4. - `silent`- Whether the notification sound should be suppressed (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. @@ -38,9 +38,9 @@ Example: ```javascript let someLocalNotification = NotificationsIOS.localNotification({ - alertBody: "Local notificiation!", - alertTitle: "Local Notification Title", - soundName: "chime.aiff", + body: "Local notificiation!", + title: "Local Notification Title", + sound: "chime.aiff", category: "SOME_CATEGORY", userInfo: { } }); diff --git a/example/index.ios.js b/example/index.ios.js index f487aae6f0e4a3c20d670c522420d12cad2cc02d..b622618526dd5386cdf41136f90d695d27383a58 100644 --- a/example/index.ios.js +++ b/example/index.ios.js @@ -13,11 +13,6 @@ let upvoteAction = new NotificationAction({ activationMode: 'background', title: String.fromCodePoint(0x1F44D), identifier: 'UPVOTE_ACTION' -}, (action, completed) => { - NotificationsIOS.log('ACTION RECEIVED'); - NotificationsIOS.log(JSON.stringify(action)); - - completed(); }); let replyAction = new NotificationAction({ @@ -30,11 +25,6 @@ let replyAction = new NotificationAction({ placeholder: 'Insert message' }, identifier: 'REPLY_ACTION' -}, (action, completed) => { - console.log('ACTION RECEIVED'); - console.log(action); - - completed(); }); class NotificationsExampleApp extends Component { @@ -75,11 +65,12 @@ class NotificationsExampleApp extends Component { notifications: [...this.state.notifications, notification] }); - completion({}); + completion({alert: true, sound: false, badge: false}); } - onNotificationOpened(notification) { - console.log('Notification Opened: ' + JSON.stringify(notification)); + onNotificationOpened(notification, completion, action) { + console.log('Notification Opened: ' + JSON.stringify(notification) + JSON.stringify(action)); + completion(); } renderNotification(notification) { @@ -97,6 +88,8 @@ class NotificationsExampleApp extends Component { return (