Commit 6497ee02 authored by Lidan Hifi's avatar Lidan Hifi

added cancelLocalNotification and cancelAllLocalNotifications support

parent b02b6f09
...@@ -158,7 +158,7 @@ Triggering local notifications is fully compatible with React Native `PushNotifi ...@@ -158,7 +158,7 @@ Triggering local notifications is fully compatible with React Native `PushNotifi
Example: Example:
```javascript ```javascript
NotificationsIOS.localNotification({ let localNotification = NotificationsIOS.localNotification({
alertBody: "Local notificiation!", alertBody: "Local notificiation!",
alertTitle: "Local Notification Title", alertTitle: "Local Notification Title",
alertAction: "Click here to open", alertAction: "Click here to open",
...@@ -178,6 +178,30 @@ Notification object contains: ...@@ -178,6 +178,30 @@ Notification object contains:
- `category`- The category of this notification, required for [interactive notifications](#interactive--actionable-notifications-ios-only) (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. - `userInfo`- An optional object containing additional notification data.
### Cancel Local Notification
```NotificationsIOS.localNotification``` method returns `notificationId`, which is used in order to cancel created local notification. You can cancel local notification by calling `NotificationsIOS.cancelLocalNotification(notificationId)` method.
Example:
```javascript
let someLocalNotification = NotificationsIOS.localNotification({
alertBody: "Local notificiation!",
alertTitle: "Local Notification Title",
alertAction: "Click here to open",
soundName: "chime.aiff",
category: "SOME_CATEGORY",
userInfo: { }
});
NotificationsIOS.cancelLocalNotification(someLocalNotification);
```
### Cancel All Local Notifications
```javascript
NotificationsIOS.cancelAllLocalNotifications();
```
--- ---
## Managed Notifications (iOS only) ## Managed Notifications (iOS only)
......
...@@ -191,6 +191,10 @@ RCT_EXPORT_MODULE() ...@@ -191,6 +191,10 @@ RCT_EXPORT_MODULE()
{ {
UIApplicationState state = [UIApplication sharedApplication].applicationState; UIApplicationState state = [UIApplication sharedApplication].applicationState;
NSMutableDictionary* newUserInfo = notification.userInfo.mutableCopy;
[newUserInfo removeObjectForKey:@"__id"];
notification.userInfo = newUserInfo;
if (state == UIApplicationStateActive) { if (state == UIApplicationStateActive) {
[self didReceiveNotificationOnForegroundState:notification.userInfo]; [self didReceiveNotificationOnForegroundState:notification.userInfo];
} else if (state == UIApplicationStateInactive) { } else if (state == UIApplicationStateInactive) {
...@@ -483,13 +487,34 @@ RCT_EXPORT_METHOD(consumeBackgroundQueue) ...@@ -483,13 +487,34 @@ RCT_EXPORT_METHOD(consumeBackgroundQueue)
} }
} }
RCT_EXPORT_METHOD(localNotification:(NSDictionary *)notification) RCT_EXPORT_METHOD(localNotification:(NSDictionary *)notification withId:(NSString *)notificationId)
{ {
UILocalNotification* localNotification = [RCTConvert UILocalNotification:notification];
NSMutableArray* userInfo = localNotification.userInfo.mutableCopy;
[userInfo setValue:notificationId forKey:@"__id"];
localNotification.userInfo = userInfo;
if ([notification objectForKey:@"fireDate"] != nil) { if ([notification objectForKey:@"fireDate"] != nil) {
[RCTSharedApplication() scheduleLocalNotification:[RCTConvert UILocalNotification:notification]]; [RCTSharedApplication() scheduleLocalNotification:localNotification];
} else { } else {
[RCTSharedApplication() presentLocalNotificationNow:[RCTConvert UILocalNotification:notification]]; [RCTSharedApplication() presentLocalNotificationNow:localNotification];
} }
} }
RCT_EXPORT_METHOD(cancelLocalNotification:(NSString *)notificationId)
{
for (UILocalNotification* notification in [UIApplication sharedApplication].scheduledLocalNotifications) {
NSDictionary* notificationInfo = notification.userInfo;
if ([[notificationInfo objectForKey:@"__id"] isEqualToString:notificationId]) {
[[UIApplication sharedApplication] cancelLocalNotification:notification];
}
}
}
RCT_EXPORT_METHOD(cancelAllLocalNotifications)
{
[RCTSharedApplication() cancelAllLocalNotifications];
}
@end @end
...@@ -77,7 +77,7 @@ class NotificationsExampleApp extends Component { ...@@ -77,7 +77,7 @@ class NotificationsExampleApp extends Component {
onNotificationReceivedBackground(notification) { onNotificationReceivedBackground(notification) {
NotificationsIOS.log("Notification Received Background: " + JSON.stringify(notification)); NotificationsIOS.log("Notification Received Background: " + JSON.stringify(notification));
NotificationsIOS.localNotification({ let localNotification = NotificationsIOS.localNotification({
alertBody: "Received background notificiation!", alertBody: "Received background notificiation!",
alertTitle: "Local Notification Title", alertTitle: "Local Notification Title",
alertAction: "Click here to open", alertAction: "Click here to open",
...@@ -87,6 +87,8 @@ class NotificationsExampleApp extends Component { ...@@ -87,6 +87,8 @@ class NotificationsExampleApp extends Component {
}); });
// NotificationsIOS.backgroundTimeRemaining(time => NotificationsIOS.log("remaining background time: " + time)); // NotificationsIOS.backgroundTimeRemaining(time => NotificationsIOS.log("remaining background time: " + time));
// NotificationsIOS.cancelLocalNotification(localNotification);
} }
onNotificationOpened(notification) { onNotificationOpened(notification) {
......
...@@ -33,7 +33,7 @@ ...@@ -33,7 +33,7 @@
* on the same Wi-Fi network. * on the same Wi-Fi network.
*/ */
jsCodeLocation = [NSURL URLWithString:@"http://172.31.9.91:8081/index.ios.bundle?platform=ios&dev=true"]; jsCodeLocation = [NSURL URLWithString:@"http://localhost:8081/index.ios.bundle?platform=ios&dev=true"];
/** /**
* OPTION 2 * OPTION 2
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
"use strict"; "use strict";
import { NativeModules, DeviceEventEmitter, NativeAppEventEmitter } from "react-native"; import { NativeModules, DeviceEventEmitter, NativeAppEventEmitter } from "react-native";
import Map from "core-js/library/es6/map"; import Map from "core-js/library/es6/map";
import uuid from "uuid";
const NativeRNNotifications = NativeModules.RNNotifications; // eslint-disable-line no-unused-vars const NativeRNNotifications = NativeModules.RNNotifications; // eslint-disable-line no-unused-vars
import IOSNotification from "./notification.ios"; import IOSNotification from "./notification.ios";
...@@ -179,6 +180,17 @@ export default class NotificationsIOS { ...@@ -179,6 +180,17 @@ export default class NotificationsIOS {
* - `fireDate` : The date and time when the system should deliver the notification. if not specified, the notification will be dispatched immediately. * - `fireDate` : The date and time when the system should deliver the notification. if not specified, the notification will be dispatched immediately.
*/ */
static localNotification(notification: Object) { static localNotification(notification: Object) {
NativeRNNotifications.localNotification(notification); let notificationId = uuid.v4();
NativeRNNotifications.localNotification(notification, notificationId);
return notificationId;
}
static cancelLocalNotification(notificationId: String) {
NativeRNNotifications.cancelLocalNotification(notificationId);
}
static cancelAllLocalNotifications() {
NativeRNNotifications.cancelAllLocalNotifications();
} }
} }
...@@ -21,7 +21,8 @@ ...@@ -21,7 +21,8 @@
], ],
"nativePackage": true, "nativePackage": true,
"dependencies": { "dependencies": {
"core-js": "^1.0.0" "core-js": "^1.0.0",
"uuid": "^2.0.3"
}, },
"peerDependencies": { "peerDependencies": {
"react-native": ">=0.25.1" "react-native": ">=0.25.1"
......
...@@ -24,9 +24,12 @@ describe("NotificationsIOS", () => { ...@@ -24,9 +24,12 @@ describe("NotificationsIOS", () => {
nativeRegisterPushKit, nativeRegisterPushKit,
nativeBackgroundTimeRemaining, nativeBackgroundTimeRemaining,
nativeConsumeBackgroundQueue, nativeConsumeBackgroundQueue,
nativeLocalNotification; nativeLocalNotification,
nativeCancelLocalNotification,
nativeCancelAllLocalNotifications;
let NotificationsIOS, NotificationAction, NotificationCategory; let NotificationsIOS, NotificationAction, NotificationCategory;
let someHandler = () => {}; let someHandler = () => {};
let constantGuid = "some-random-uuid";
/*eslint-enable indent*/ /*eslint-enable indent*/
before(() => { before(() => {
...@@ -40,8 +43,13 @@ describe("NotificationsIOS", () => { ...@@ -40,8 +43,13 @@ describe("NotificationsIOS", () => {
nativeBackgroundTimeRemaining = sinon.spy(); nativeBackgroundTimeRemaining = sinon.spy();
nativeConsumeBackgroundQueue = sinon.spy(); nativeConsumeBackgroundQueue = sinon.spy();
nativeLocalNotification = sinon.spy(); nativeLocalNotification = sinon.spy();
nativeCancelLocalNotification = sinon.spy();
nativeCancelAllLocalNotifications = sinon.spy();
let libUnderTest = proxyquire("../index.ios", { let libUnderTest = proxyquire("../index.ios", {
"uuid": {
v4: () => constantGuid
},
"react-native": { "react-native": {
NativeModules: { NativeModules: {
RNNotifications: { RNNotifications: {
...@@ -50,7 +58,9 @@ describe("NotificationsIOS", () => { ...@@ -50,7 +58,9 @@ describe("NotificationsIOS", () => {
registerPushKit: nativeRegisterPushKit, registerPushKit: nativeRegisterPushKit,
backgroundTimeRemaining: nativeBackgroundTimeRemaining, backgroundTimeRemaining: nativeBackgroundTimeRemaining,
consumeBackgroundQueue: nativeConsumeBackgroundQueue, consumeBackgroundQueue: nativeConsumeBackgroundQueue,
localNotification: nativeLocalNotification localNotification: nativeLocalNotification,
cancelLocalNotification: nativeCancelLocalNotification,
cancelAllLocalNotifications: nativeCancelAllLocalNotifications
} }
}, },
NativeAppEventEmitter: { NativeAppEventEmitter: {
...@@ -87,6 +97,8 @@ describe("NotificationsIOS", () => { ...@@ -87,6 +97,8 @@ describe("NotificationsIOS", () => {
nativeBackgroundTimeRemaining.reset(); nativeBackgroundTimeRemaining.reset();
nativeConsumeBackgroundQueue.reset(); nativeConsumeBackgroundQueue.reset();
nativeLocalNotification.reset(); nativeLocalNotification.reset();
nativeCancelLocalNotification.reset();
nativeCancelAllLocalNotifications.reset();
}); });
after(() => { after(() => {
...@@ -100,6 +112,8 @@ describe("NotificationsIOS", () => { ...@@ -100,6 +112,8 @@ describe("NotificationsIOS", () => {
nativeBackgroundTimeRemaining = null; nativeBackgroundTimeRemaining = null;
nativeConsumeBackgroundQueue = null; nativeConsumeBackgroundQueue = null;
nativeLocalNotification = null; nativeLocalNotification = null;
nativeCancelLocalNotification = null;
nativeCancelAllLocalNotifications = null;
NotificationsIOS = null; NotificationsIOS = null;
NotificationAction = null; NotificationAction = null;
...@@ -229,8 +243,12 @@ describe("NotificationsIOS", () => { ...@@ -229,8 +243,12 @@ describe("NotificationsIOS", () => {
}); });
}); });
describe("Get background remaining time", () => { describe("Dispatch local notification", () => {
it("should call native consume background queue method", () => { it("should return generated notification guid", () => {
expect(NotificationsIOS.localNotification({})).to.equal(constantGuid);
});
it("should call native local notification method with generated notification guid and notification object", () => {
let someLocalNotification = { let someLocalNotification = {
alertBody: "some body", alertBody: "some body",
alertTitle: "some title", alertTitle: "some title",
...@@ -244,7 +262,23 @@ describe("NotificationsIOS", () => { ...@@ -244,7 +262,23 @@ describe("NotificationsIOS", () => {
NotificationsIOS.localNotification(someLocalNotification); NotificationsIOS.localNotification(someLocalNotification);
expect(nativeLocalNotification).to.have.been.calledWith(someLocalNotification); expect(nativeLocalNotification).to.have.been.calledWith(someLocalNotification, constantGuid);
});
});
describe("Cancel local notification", () => {
it("should call native cancel local notification method", () => {
NotificationsIOS.cancelLocalNotification(constantGuid);
expect(nativeCancelLocalNotification).to.have.been.calledWith(constantGuid);
});
});
describe("Cancel all local notifications", () => {
it("should call native cancel all local notifications method", () => {
NotificationsIOS.cancelAllLocalNotifications();
expect(nativeCancelAllLocalNotifications).to.have.been.calledWith();
}); });
}); });
}); });
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