From a8cb47e0118219fa66216cd230912fd82465eb5d Mon Sep 17 00:00:00 2001 From: Jacob Rosenthal Date: Tue, 14 Jul 2015 09:07:24 -0700 Subject: [PATCH] add local notification api schedule and present Summary: Add local notifications to the push library. ``` var PushNotificationIOS = React.PushNotificationIOS; PushNotificationIOS.requestPermissions(); var notification = { "fireDate": Date.now() + 10000, "alertBody":"Whats up pumpkin" }; PushNotificationIOS.scheduleLocalNotification(notification); //lock screen or move away from app ``` Apple has another api for pushing immediately instead of scheduling, like when your background delegate has been called with some new data (bluetooth, location, etc) ``` var PushNotificationIOS = React.PushNotificationIOS; PushNotificationIOS.requestPermissions(); var notification = { "alertBody":"Whats up pumpkin" }; PushNotificationIOS.presentLocalNotification(notification); //lock screen or move away from app ``` Closed https://github.com/facebook/react-native/pull/843 looks related: See https://developer.apple.com/library/ios/documentation/iPhone/Reference/UILocalNotification_Class/ for much more available in Closes https://github.com/facebook/react-native/pull/1616 Github Author: Jacob Rosenthal --- .../PushNotificationIOS.js | 25 ++++++++++++++++ .../RCTPushNotificationManager.m | 29 +++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/Libraries/PushNotificationIOS/PushNotificationIOS.js b/Libraries/PushNotificationIOS/PushNotificationIOS.js index 1781ddff1..c71722ae8 100644 --- a/Libraries/PushNotificationIOS/PushNotificationIOS.js +++ b/Libraries/PushNotificationIOS/PushNotificationIOS.js @@ -36,6 +36,31 @@ class PushNotificationIOS { _sound: string; _badgeCount: number; + /** + * Schedules the localNotification for immediate presentation. + * + * details is an object containing: + * + * - `alertBody` : The message displayed in the notification alert. + * + */ + static presentLocalNotification(details: Object) { + RCTPushNotificationManager.presentLocalNotification(details); + } + + /** + * Schedules the localNotification for future presentation. + * + * details is an object containing: + * + * - `fireDate` : The date and time when the system should deliver the notification. + * - `alertBody` : The message displayed in the notification alert. + * + */ + static scheduleLocalNotification(details: Object) { + RCTPushNotificationManager.scheduleLocalNotification(details); + } + /** * Sets the badge number for the app icon on the home screen */ diff --git a/Libraries/PushNotificationIOS/RCTPushNotificationManager.m b/Libraries/PushNotificationIOS/RCTPushNotificationManager.m index 6a9420819..ac683fc2a 100644 --- a/Libraries/PushNotificationIOS/RCTPushNotificationManager.m +++ b/Libraries/PushNotificationIOS/RCTPushNotificationManager.m @@ -10,6 +10,7 @@ #import "RCTPushNotificationManager.h" #import "RCTBridge.h" +#import "RCTConvert.h" #import "RCTEventDispatcher.h" #import "RCTUtils.h" @@ -26,6 +27,19 @@ NSString *const RCTRemoteNotificationReceived = @"RemoteNotificationReceived"; NSString *const RCTRemoteNotificationsRegistered = @"RemoteNotificationsRegistered"; +@implementation RCTConvert (UILocalNotification) + ++ (UILocalNotification *)UILocalNotification:(id)json +{ + NSDictionary *details = [self NSDictionary:json]; + UILocalNotification *notification = [[UILocalNotification alloc] init]; + notification.fireDate = [RCTConvert NSDate:details[@"fireDate"]] ?: [NSDate date]; + notification.alertBody = [RCTConvert NSString:details[@"alertBody"]]; + return notification; +} + +@end + @implementation RCTPushNotificationManager { NSDictionary *_initialNotification; @@ -139,11 +153,15 @@ RCT_EXPORT_METHOD(requestPermissions:(NSDictionary *)permissions) } #if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_8_0 + id notificationSettings = [UIUserNotificationSettings settingsForTypes:types categories:nil]; [[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings]; [[UIApplication sharedApplication] registerForRemoteNotifications]; + #else + [[UIApplication sharedApplication] registerForRemoteNotificationTypes:types]; + #endif } @@ -183,4 +201,15 @@ RCT_EXPORT_METHOD(checkPermissions:(RCTResponseSenderBlock)callback) }; } +RCT_EXPORT_METHOD(presentLocalNotification:(UILocalNotification *)notification) +{ + [[UIApplication sharedApplication] presentLocalNotificationNow:notification]; +} + + +RCT_EXPORT_METHOD(scheduleLocalNotification:(UILocalNotification *)notification) +{ + [[UIApplication sharedApplication] scheduleLocalNotification:notification]; +} + @end