diff --git a/docs/android-api.md b/docs/android-api.md index daa608d8425517fc05d6dd91cefa93fc6c79719a..7ce9e3f6a3035a5cd7c3fccfa3577a2678184c84 100755 --- a/docs/android-api.md +++ b/docs/android-api.md @@ -8,5 +8,5 @@ sidebar_label: Android specific refreshToken ```js -Notifications.refreshToken(); +Notifications.android.refreshToken(); ``` diff --git a/docs/general-api.md b/docs/general-api.md index 3a5b76db9ccf64894694021fad60adc9ec7370c1..49d09648717fae8baccb799d5fb10b4fa9b15a0e 100755 --- a/docs/general-api.md +++ b/docs/general-api.md @@ -8,7 +8,7 @@ sidebar_label: General Return the notification that caused the app to launch from dead state. ```js -const notification: Notification = await getInitialNotification(); +const notification: Notification = await Notifications.getInitialNotification(); ``` ## postLocalNotification(notification, id?) @@ -36,5 +36,5 @@ Notifications.cancelLocalNotification(id); Check if the app has permissions to send remote notifications. ```js -const hasPermissions: boolean = await getInitialNotification(); +const hasPermissions: boolean = await Notifications.getInitialNotification(); ``` diff --git a/docs/ios-api.md b/docs/ios-api.md index f4f783cd924a17bcf016a02045967efb16016f5f..8c034dc4357b6644ce60ccb316f5705c84655576 100755 --- a/docs/ios-api.md +++ b/docs/ios-api.md @@ -8,61 +8,68 @@ sidebar_label: iOS specific request permissions ```js -Notifications.requestPermissions(); +Notifications.ios.requestPermissions(); ``` ## checkPermissions checkPermissions ```js -Notifications.checkPermissions(); +Notifications.ios.checkPermissions(); +``` + +## abandonPermissions +Unregister for all remote notifications received via Apple Push Notification service + +```js +Notifications.ios.abandonPermissions(); ``` ## registerPushKit registerPushKit ```js -Notifications.registerPushKit(); +Notifications.ios.registerPushKit(); ``` ## cancelAllLocalNotifications cancelAllLocalNotifications ```js -Notifications.cancelAllLocalNotifications(); +Notifications.ios.cancelAllLocalNotifications(); ``` ## getDeliveredNotifications getDeliveredNotifications ```js -Notifications.getDeliveredNotifications(); +Notifications.ios.getDeliveredNotifications(); ``` ## removeAllDeliveredNotifications removeAllDeliveredNotifications ```js -Notifications.removeAllDeliveredNotifications(); +Notifications.ios.removeAllDeliveredNotifications(); ``` ## removeDeliveredNotifications removeDeliveredNotifications ```js -Notifications.removeDeliveredNotifications(); +Notifications.ios.removeDeliveredNotifications(); ``` ## getBadgeCount getBadgeCount ```js -Notifications.getBadgeCount(); +Notifications.ios.getBadgeCount(); ``` ## setBadgeCount setBadgeCount ```js -Notifications.setBadgeCount(1); +Notifications.ios.setBadgeCount(1); ``` \ No newline at end of file diff --git a/example/index.js b/example/index.js index c30bce596d842c0730572c1aa0d2163dd2e7c94f..8ab1e4161a407202967ae2aedd0a8d6426300ece 100644 --- a/example/index.js +++ b/example/index.js @@ -48,7 +48,7 @@ class NotificationsExampleApp extends Component { } requestPermissions() { - Notifications.requestPermissions(); + Notifications.ios.requestPermissions(); } setCategories() { diff --git a/lib/src/Notifications.ts b/lib/src/Notifications.ts index dc6e56a984e140bc586e67d9f209440f9e04ae42..1669402725ebbeb3b8be0789a1d857158aa55a0c 100644 --- a/lib/src/Notifications.ts +++ b/lib/src/Notifications.ts @@ -6,8 +6,13 @@ import { Notification } from './DTO/Notification'; import { UniqueIdProvider } from './adapters/UniqueIdProvider'; import { CompletionCallbackWrapper } from './adapters/CompletionCallbackWrapper'; import { NotificationCategory } from './interfaces/NotificationCategory'; +import { NotificationsIOS } from './NotificationsIOS'; +import { NotificationsAndroid } from './NotificationsAndroid'; export class NotificationsRoot { + public readonly ios: NotificationsIOS; + public readonly android: NotificationsAndroid; + private readonly nativeEventsReceiver: NativeEventsReceiver; private readonly nativeCommandsSender: NativeCommandsSender; private readonly commands: Commands; @@ -25,22 +30,11 @@ export class NotificationsRoot { this.uniqueIdProvider ); this.eventsRegistry = new EventsRegistry(this.nativeEventsReceiver, this.completionCallbackWrapper); - } - /** - * Request permissions to send remote notifications - iOS only - */ - public requestPermissions() { - return this.commands.requestPermissions(); + this.ios = new NotificationsIOS(this.commands); + this.android = new NotificationsAndroid(this.commands); } - /** - * registerPushKit - iOS only - */ - public registerPushKit() { - return this.commands.registerPushKit(); - } - /** * postLocalNotification */ @@ -62,21 +56,6 @@ export class NotificationsRoot { this.commands.setCategories(categories); } - /** - * getBadgesCount - */ - public getBadgeCount(): Promise { - return this.commands.getBadgeCount(); - } - - /** - * setBadgeCount - * @param count number of the new badge count - */ - public setBadgeCount(count: number) { - return this.commands.setBadgeCount(count); - } - /** * cancelLocalNotification */ @@ -84,13 +63,6 @@ export class NotificationsRoot { return this.commands.cancelLocalNotification(notificationId); } - /** - * cancelAllLocalNotifications - */ - public cancelAllLocalNotifications() { - this.commands.cancelAllLocalNotifications(); - } - /** * isRegisteredForRemoteNotifications */ @@ -98,39 +70,10 @@ export class NotificationsRoot { return this.commands.isRegisteredForRemoteNotifications(); } - /** - * checkPermissions - */ - public checkPermissions() { - return this.commands.checkPermissions(); - } - - /** - * removeAllDeliveredNotifications - */ - public removeAllDeliveredNotifications() { - return this.commands.removeAllDeliveredNotifications(); - } - - /** - * removeDeliveredNotifications - * @param identifiers Array of notification identifiers - */ - public removeDeliveredNotifications(identifiers: Array) { - return this.commands.removeDeliveredNotifications(identifiers); - } - /** * Obtain the events registry instance */ public events(): EventsRegistry { return this.eventsRegistry; } - - /** - * getDeliveredNotifications - */ - public getDeliveredNotifications(): Array { - return this.commands.getDeliveredNotifications(); - } } diff --git a/lib/src/NotificationsAndroid.ts b/lib/src/NotificationsAndroid.ts new file mode 100644 index 0000000000000000000000000000000000000000..8688512684815fd768fc43d72c980e87da6be9ce --- /dev/null +++ b/lib/src/NotificationsAndroid.ts @@ -0,0 +1,23 @@ +import { Commands } from './commands/Commands'; +import { Platform } from 'react-native'; + +export class NotificationsAndroid { + constructor(private readonly commands: Commands) { + return new Proxy(this, { + get(target, name) { + if (Platform.OS === 'android') { + return (target as any)[name]; + } else { + return () => {}; + } + } + }); + } + + /** + * Refresh FCM token + */ + public refreshToken() { + return this.commands.refreshToken(); + } +} diff --git a/lib/src/NotificationsIOS.ts b/lib/src/NotificationsIOS.ts new file mode 100644 index 0000000000000000000000000000000000000000..cb4a08b3f189ea2163bb2bc721983c7689f771b1 --- /dev/null +++ b/lib/src/NotificationsIOS.ts @@ -0,0 +1,89 @@ +import { Notification } from './DTO/Notification'; +import { Commands } from './commands/Commands'; +import { Platform } from 'react-native'; + +export class NotificationsIOS { + constructor(private readonly commands: Commands) { + return new Proxy(this, { + get(target, name) { + if (Platform.OS === 'ios') { + return (target as any)[name]; + } else { + return () => {}; + } + } + }); + } + + /** + * Request permissions to send remote notifications + */ + public requestPermissions() { + return this.commands.requestPermissions(); + } + + /** + * Unregister for all remote notifications received via Apple Push Notification service + */ + public abandonPermissions() { + return this.commands.abandonPermissions(); + } + + /** + * registerPushKit + */ + public registerPushKit() { + return this.commands.registerPushKit(); + } + + /** + * getBadgesCount + */ + public getBadgeCount(): Promise { + return this.commands.getBadgeCount(); + } + + /** + * setBadgeCount + * @param count number of the new badge count + */ + public setBadgeCount(count: number) { + return this.commands.setBadgeCount(count); + } + + /** + * cancelAllLocalNotifications + */ + public cancelAllLocalNotifications() { + this.commands.cancelAllLocalNotifications(); + } + + /** + * checkPermissions + */ + public checkPermissions() { + return this.commands.checkPermissions(); + } + + /** + * removeAllDeliveredNotifications + */ + public removeAllDeliveredNotifications() { + return this.commands.removeAllDeliveredNotifications(); + } + + /** + * removeDeliveredNotifications + * @param identifiers Array of notification identifiers + */ + public removeDeliveredNotifications(identifiers: Array) { + return this.commands.removeDeliveredNotifications(identifiers); + } + + /** + * getDeliveredNotifications + */ + public getDeliveredNotifications(): Array { + return this.commands.getDeliveredNotifications(); + } +} diff --git a/lib/src/adapters/NativeCommandsSender.ts b/lib/src/adapters/NativeCommandsSender.ts index f094b33edfe227af3e5f9c75f624c86ecd11b032..8a1853e165a06398098745f7a940457c6b6e5839 100644 --- a/lib/src/adapters/NativeCommandsSender.ts +++ b/lib/src/adapters/NativeCommandsSender.ts @@ -9,6 +9,7 @@ interface NativeCommandsModule { postLocalNotification(notification: Notification, id: number): void; requestPermissions(): void; abandonPermissions(): void; + refreshToken(): void; registerPushKit(): void; getBadgeCount(): Promise; setBadgeCount(count: number): void; @@ -46,6 +47,10 @@ export class NativeCommandsSender { return this.nativeCommandsModule.abandonPermissions(); } + refreshToken() { + return this.nativeCommandsModule.refreshToken(); + } + registerPushKit() { return this.nativeCommandsModule.registerPushKit(); } diff --git a/lib/src/commands/Commands.ts b/lib/src/commands/Commands.ts index 6d1be116cb1e60f3b2de37e695fa72fb5134fb79..735b8998b5e9e311e6ad3f7f9c0e28d187fa5241 100644 --- a/lib/src/commands/Commands.ts +++ b/lib/src/commands/Commands.ts @@ -75,4 +75,8 @@ export class Commands { public getDeliveredNotifications(): Array { return this.nativeCommandsSender.getDeliveredNotifications(); } + + public refreshToken() { + return this.nativeCommandsSender.refreshToken(); + } } diff --git a/test/index.android.spec.js b/test/index.android.spec.js deleted file mode 100644 index 020e5580fb189c567900f12b6da487e873a330b1..0000000000000000000000000000000000000000 --- a/test/index.android.spec.js +++ /dev/null @@ -1,218 +0,0 @@ -describe('Notifications-Android', () => { - let libUnderTest; - let deviceEventEmitterListenerStub; - let WixRNNotifications; - - beforeEach(() => { - jest.mock('react-native', () => { - return { - NativeModules: { - WixRNNotifications: { - refreshToken: jest.fn(), - getInitialNotification: jest.fn(), - postLocalNotification: jest.fn(), - cancelLocalNotification: jest.fn() - } - }, - DeviceEventEmitter: { - addListener: jest.fn() - } - }; - }); - - deviceEventEmitterListenerStub = require('react-native').DeviceEventEmitter.addListener; - WixRNNotifications = require('react-native').NativeModules.WixRNNotifications; - - libUnderTest = require('../lib/src/index.android'); - }); - - describe('Registration token API', () => { - it('should assign callback to native event upon listener registration', () => { - expect(deviceEventEmitterListenerStub).toHaveBeenCalledTimes(0); - const userListener = () => {}; - libUnderTest.NotificationsAndroid.setRegistrationTokenUpdateListener(userListener); - - expect(deviceEventEmitterListenerStub).toHaveBeenCalledWith('remoteNotificationsRegistered', userListener); - expect(deviceEventEmitterListenerStub).toHaveBeenCalledTimes(1); - }); - - it('should clear native event listener upon listener deregister', () => { - expect(deviceEventEmitterListenerStub).toHaveBeenCalledTimes(0); - const userListener = () => {}; - const nativeListener = { - remove: jest.fn() - }; - deviceEventEmitterListenerStub.mockReturnValueOnce(nativeListener); - - libUnderTest.NotificationsAndroid.setRegistrationTokenUpdateListener(userListener); - libUnderTest.NotificationsAndroid.clearRegistrationTokenUpdateListener(); - - expect(nativeListener.remove).toHaveBeenCalledTimes(1); - }); - - it('shouldn`t fail if deregister without registering', () => { - libUnderTest.NotificationsAndroid.clearRegistrationTokenUpdateListener(); - - expect(deviceEventEmitterListenerStub).toHaveBeenCalledTimes(0); - }); - }); - - describe('notification-opening API', () => { - it('should assign callback to native event upon registration', () => { - expect(deviceEventEmitterListenerStub).toHaveBeenCalledTimes(0); - const userListenerStub = jest.fn(); - - libUnderTest.NotificationsAndroid.setNotificationOpenedListener(userListenerStub); - - expect(deviceEventEmitterListenerStub).toHaveBeenCalledTimes(1); - expect(deviceEventEmitterListenerStub).toHaveBeenCalledWith('notificationOpened', expect.any(Function)); - }); - - it('should assign a wrapper-callback upon registration', () => { - expect(deviceEventEmitterListenerStub).toHaveBeenCalledTimes(0); - const userListenerStub = jest.fn(); - const notification = { foo: 'bar' }; - - libUnderTest.NotificationsAndroid.setNotificationOpenedListener(userListenerStub); - - expect(userListenerStub).toHaveBeenCalledTimes(0); - deviceEventEmitterListenerStub.mock.calls[0][1](notification); - expect(userListenerStub).toHaveBeenCalledTimes(1); - expect(userListenerStub.mock.calls[0][0].getData()).toEqual(notification); - }); - - it('should clear native event listener upon listener deregister', () => { - expect(deviceEventEmitterListenerStub).toHaveBeenCalledTimes(0); - const userListener = () => {}; - const nativeListener = { - remove: jest.fn() - }; - deviceEventEmitterListenerStub.mockReturnValueOnce(nativeListener); - - libUnderTest.NotificationsAndroid.setNotificationOpenedListener(userListener); - libUnderTest.NotificationsAndroid.clearNotificationOpenedListener(); - - expect(nativeListener.remove).toHaveBeenCalledTimes(1); - }); - - it('shouldnt fail if deregister without registering', () => { - libUnderTest.NotificationsAndroid.clearNotificationOpenedListener(); - - expect(deviceEventEmitterListenerStub).toHaveBeenCalledTimes(0); - }); - }); - - describe('notification-receive API', () => { - it('should assign callback to native event upon registration', () => { - expect(deviceEventEmitterListenerStub).toHaveBeenCalledTimes(0); - const userListenerStub = jest.fn(); - - libUnderTest.NotificationsAndroid.setNotificationReceivedListener(userListenerStub); - - expect(deviceEventEmitterListenerStub).toHaveBeenCalledTimes(1); - expect(deviceEventEmitterListenerStub).toHaveBeenCalledWith('notificationReceived', expect.any(Function)); - }); - - it('should assign a wrapper-callback upon registration', () => { - expect(deviceEventEmitterListenerStub).toHaveBeenCalledTimes(0); - const userListenerStub = jest.fn(); - const notification = { foo: 'bar' }; - - libUnderTest.NotificationsAndroid.setNotificationReceivedListener(userListenerStub); - - expect(userListenerStub).toHaveBeenCalledTimes(0); - deviceEventEmitterListenerStub.mock.calls[0][1](notification); - expect(userListenerStub).toHaveBeenCalledTimes(1); - expect(userListenerStub.mock.calls[0][0].getData()).toEqual(notification); - }); - - it('should clear native event listener upon listener deregister', () => { - expect(deviceEventEmitterListenerStub).toHaveBeenCalledTimes(0); - const userListener = () => {}; - const nativeListener = { - remove: jest.fn() - }; - deviceEventEmitterListenerStub.mockReturnValueOnce(nativeListener); - - libUnderTest.NotificationsAndroid.setNotificationReceivedListener(userListener); - libUnderTest.NotificationsAndroid.clearNotificationReceivedListener(); - - expect(nativeListener.remove).toHaveBeenCalledTimes(1); - }); - - it('shouldn`t fail if deregister without registering', () => { - libUnderTest.NotificationsAndroid.clearNotificationReceivedListener(); - - expect(deviceEventEmitterListenerStub).toHaveBeenCalledTimes(0); - }); - }); - - describe('Notification token', () => { - it('should refresh notification token upon refreshing request by the user', () => { - expect(WixRNNotifications.refreshToken).toHaveBeenCalledTimes(0); - libUnderTest.NotificationsAndroid.refreshToken(); - expect(WixRNNotifications.refreshToken).toHaveBeenCalledTimes(1); - }); - }); - - describe('Initial notification API', () => { - it('should return initial notification data if available', (done) => { - expect(WixRNNotifications.getInitialNotification).toHaveBeenCalledTimes(0); - const rawNotification = {foo: 'bar'}; - WixRNNotifications.getInitialNotification.mockReturnValueOnce(Promise.resolve(rawNotification)); - - libUnderTest.PendingNotifications.getInitialNotification() - .then((notification) => { - expect(notification.getData()).toEqual(rawNotification); - done(); - }) - .catch((err) => done(err)); - }); - - it('should return empty notification if not available', (done) => { - expect(WixRNNotifications.getInitialNotification).toHaveBeenCalledTimes(0); - WixRNNotifications.getInitialNotification.mockReturnValueOnce(Promise.resolve(null)); - - libUnderTest.PendingNotifications.getInitialNotification() - .then((notification) => { - expect(notification).toBeUndefined(); - done(); - }) - .catch((err) => done(err)); - }); - - }); - - describe('Local notification', () => { - const notification = { - title: 'notification-title', - body: 'notification-body' - }; - - it('should get published when posted manually', () => { - expect(WixRNNotifications.postLocalNotification).toHaveBeenCalledTimes(0); - - const id = libUnderTest.NotificationsAndroid.localNotification(notification); - expect(id).toBeDefined(); - expect(WixRNNotifications.postLocalNotification).toHaveBeenCalledWith(notification, id); - }); - - it('should be called with a unique ID', () => { - expect(WixRNNotifications.postLocalNotification).toHaveBeenCalledTimes(0); - - const id = libUnderTest.NotificationsAndroid.localNotification(notification); - const id2 = libUnderTest.NotificationsAndroid.localNotification(notification); - expect(id).toBeDefined(); - expect(id2).toBeDefined(); - expect(id).not.toBe(id2); - }); - - it('should be cancellable with an ID', () => { - expect(WixRNNotifications.cancelLocalNotification).toHaveBeenCalledTimes(0); - - libUnderTest.NotificationsAndroid.cancelLocalNotification(666); - - expect(WixRNNotifications.cancelLocalNotification).toHaveBeenCalledWith(666); - }); - }); -}); diff --git a/test/index.ios.spec.js b/test/index.ios.spec.js deleted file mode 100644 index 081c82334484b96925930a899fc2a9f33b5fa0f3..0000000000000000000000000000000000000000 --- a/test/index.ios.spec.js +++ /dev/null @@ -1,268 +0,0 @@ -describe('NotificationsIOS', () => { - let deviceEvents = [ - 'pushKitRegistered', - 'remoteNotificationsRegistered', - 'remoteNotificationsRegistrationFailed', - 'notificationReceivedForeground', - 'notificationOpened' - ]; - - let NotificationsIOS, NotificationAction, NotificationCategory; - let constantGuid = 'some-random-uuid'; - let identifiers = ['some-random-uuid', 'other-random-uuid']; - let someHandler = () => {}; - let nativeModule; - let DeviceEventEmitter; - - beforeEach(() => { - jest.mock('react-native', () => { - const RNBridgeModule = { - requestPermissionsWithCategories: jest.fn(), - abandonPermissions: jest.fn(), - registerPushKit: jest.fn(), - backgroundTimeRemaining: jest.fn(), - localNotification: jest.fn(), - cancelLocalNotification: jest.fn(), - cancelAllLocalNotifications: jest.fn(), - getBadgesCount: jest.fn(), - setBadgesCount: jest.fn(), - isRegisteredForRemoteNotifications: jest.fn(), - checkPermissions: jest.fn(), - removeAllDeliveredNotifications: jest.fn(), - removeDeliveredNotifications: jest.fn(), - getDeliveredNotifications: jest.fn() - }; - return { - NativeModules: { - RNBridgeModule - }, - DeviceEventEmitter: { - addListener: jest.fn(() => { - return { - remove: jest.fn() - }; - }) - } - }; - }); - - nativeModule = require('react-native').NativeModules.RNBridgeModule; - DeviceEventEmitter = require('react-native').DeviceEventEmitter; - jest.mock('uuid', () => { - return { - v4: () => 'some-random-uuid' - }; - }); - - let libUnderTest = require('../lib/src/index.ios'); - NotificationsIOS = libUnderTest.default; - NotificationAction = libUnderTest.NotificationAction; - NotificationCategory = libUnderTest.NotificationCategory; - }); - - describe('Add Event Listener', () => { - deviceEvents.forEach(event => { - it(`should subscribe the given handler to device event: ${event}`, () => { - NotificationsIOS.addEventListener(event, someHandler); - expect(DeviceEventEmitter.addListener).toHaveBeenCalledWith(event, expect.any(Function)); - }); - }); - - it('should not subscribe to unknown device events', () => { - NotificationsIOS.addEventListener('someUnsupportedEvent', someHandler); - - expect(DeviceEventEmitter.addListener).toHaveBeenCalledTimes(0); - }); - }); - - describe('Remove Event Listener', () => { - deviceEvents.forEach(event => { - it(`should unsubscribe the given handler from device event: ${event}`, () => { - const removeCallback = jest.fn(); - DeviceEventEmitter.addListener.mockReturnValueOnce({ - remove: removeCallback - }); - NotificationsIOS.addEventListener(event, someHandler); - NotificationsIOS.removeEventListener(event, someHandler); - - expect(removeCallback).toHaveBeenCalledTimes(1); - }); - }); - - it('should not unsubscribe to unknown device events', () => { - let someUnsupportedEvent = 'someUnsupportedEvent'; - const removeCallback = jest.fn(); - DeviceEventEmitter.addListener.mockReturnValueOnce({ - remove: removeCallback - }); - - NotificationsIOS.addEventListener(someUnsupportedEvent, someHandler); - NotificationsIOS.removeEventListener(someUnsupportedEvent, someHandler); - - expect(removeCallback).toHaveBeenCalledTimes(0); - }); - }); - - describe('Notification actions handling', () => { - let someAction, someCategory; - - let actionOpts = { - activationMode: 'foreground', - title: 'someAction', - behavior: 'default', - identifier: 'SOME_ACTION' - }; - - beforeEach(() => { - someAction = new NotificationAction(actionOpts, () => {}); - - someCategory = new NotificationCategory({ - identifier: 'SOME_CATEGORY', - actions: [someAction], - context: 'default' - }); - }); - - describe('register push notifications', () => { - it('should call native request permissions with array of categories', () => { - NotificationsIOS.requestPermissions([someCategory]); - - expect(nativeModule.requestPermissionsWithCategories).toHaveBeenCalledWith([{ - identifier: 'SOME_CATEGORY', - actions: [actionOpts], - context: 'default' - }]); - }); - - it('should call native request permissions with empty array if no categories specified', () => { - NotificationsIOS.requestPermissions(); - - expect(nativeModule.requestPermissionsWithCategories).toHaveBeenCalledWith([]); - }); - }); - - describe('get badges count', () => { - it('should call native getBadgesCount', () => { - const callback = (count) => console.log(count); - NotificationsIOS.getBadgesCount(callback); - - expect(nativeModule.getBadgesCount).toHaveBeenCalledWith(callback); - }); - }); - - describe('set badges count', () => { - it('should call native setBadgesCount', () => { - NotificationsIOS.setBadgesCount(44); - - expect(nativeModule.setBadgesCount).toHaveBeenCalledWith(44); - }); - }); - - }); - - describe('register push kit for background notifications', function () { - it('should call native register push kit method', function () { - NotificationsIOS.registerPushKit(); - - expect(nativeModule.registerPushKit).toHaveBeenCalledTimes(1); - }); - }); - - describe('Abandon push notifications permissions', () => { - it('should call native abandon permissions method', () => { - NotificationsIOS.abandonPermissions(); - - expect(nativeModule.abandonPermissions).toHaveBeenCalledTimes(1); - }); - }); - - describe('Get background remaining time', () => { - it('should call native background remaining time method', () => { - let someCallback = () => {}; - - NotificationsIOS.backgroundTimeRemaining(someCallback); - - expect(nativeModule.backgroundTimeRemaining).toHaveBeenCalledWith(someCallback); - }); - }); - - describe('Dispatch local notification', () => { - it('should return generated notification guid', () => { - expect(NotificationsIOS.localNotification({})).toEqual(constantGuid); - }); - - it('should call native local notification method with generated notification guid and notification object', () => { - let someLocalNotification = { - body: 'some body', - title: 'some title', - alertAction: 'some action', - sound: 'sound', - category: 'SOME_CATEGORY', - userInfo: { - 'key': 'value' - } - }; - - NotificationsIOS.localNotification(someLocalNotification); - - expect(nativeModule.localNotification).toHaveBeenCalledWith(someLocalNotification, constantGuid); - }); - }); - - describe('Cancel local notification', () => { - it('should call native cancel local notification method', () => { - NotificationsIOS.cancelLocalNotification(constantGuid); - - expect(nativeModule.cancelLocalNotification).toHaveBeenCalledWith(constantGuid); - }); - }); - - describe('Cancel all local notifications', () => { - it('should call native cancel all local notifications method', () => { - NotificationsIOS.cancelAllLocalNotifications(); - - expect(nativeModule.cancelAllLocalNotifications).toHaveBeenCalledWith(); - }); - }); - - describe('Is registered for remote notifications ', () => { - it('should call native is registered for remote notifications', () => { - NotificationsIOS.isRegisteredForRemoteNotifications(); - expect(nativeModule.isRegisteredForRemoteNotifications).toHaveBeenCalledWith(); - - }); - }); - - describe('Check permissions ', () => { - it('should call native check permissions', () => { - NotificationsIOS.checkPermissions(); - expect(nativeModule.checkPermissions).toHaveBeenCalledWith(); - - }); - }); - - describe('Remove all delivered notifications', () => { - it('should call native remove all delivered notifications method', () => { - NotificationsIOS.removeAllDeliveredNotifications(); - - expect(nativeModule.removeAllDeliveredNotifications).toHaveBeenCalledWith(); - }); - }); - - describe('Remove delivered notifications', () => { - it('should call native remove delivered notifications method', () => { - NotificationsIOS.removeDeliveredNotifications(identifiers); - - expect(nativeModule.removeDeliveredNotifications).toHaveBeenCalledWith(identifiers); - }); - }); - - describe('Get delivered notifications', () => { - it('should call native get delivered notifications method', () => { - const callback = (notifications) => console.log(notifications); - NotificationsIOS.getDeliveredNotifications(callback); - - expect(nativeModule.getDeliveredNotifications).toHaveBeenCalledWith(callback); - }); - }); -}); diff --git a/test/notification.ios.spec.js b/test/notification.ios.spec.js deleted file mode 100644 index 04441aa640690d195056d419ca64fb0b04ed07b7..0000000000000000000000000000000000000000 --- a/test/notification.ios.spec.js +++ /dev/null @@ -1,126 +0,0 @@ -import IOSNotification from '../lib/src/notification.ios'; - -describe('iOS Notification Object', () => { - let notification; - let someBadgeCount = 123, someSound = 'someSound', someCategory = 'some_notification_category', someThread = 'thread-1'; - - describe('for a regular iOS push notification', () => { - let regularNativeNotifications = [ - // basic example, without content-available = 1 (aka silent notification) - { - aps: { - alert: { - title: 'some title', - body: 'some body' - }, - badge: someBadgeCount, - sound: someSound, - category: someCategory, - thread: someThread - }, - key1: 'value1', - key2: 'value2' - }, - - // another example, with content-available but also with alert object (should not be a silent notification) - { - aps: { - 'content-available': 1, - alert: { - title: 'some title', - body: 'some body' - }, - badge: someBadgeCount, - sound: someSound, - category: someCategory, - thread: someThread - }, - key1: 'value1', - key2: 'value2' - } - ]; - - regularNativeNotifications.forEach(nativeNotification => { - beforeEach(() => { - notification = new IOSNotification(nativeNotification); - }); - - it('should return regular type', function () { - expect(notification.getType()).toEqual('regular'); - }); - - it('should return the alert object', () => { - expect(notification.getMessage()).toEqual(nativeNotification.aps.alert); - }); - - it('should return the sound', () => { - expect(notification.getSound()).toEqual(someSound); - }); - - it('should return the badge count', () => { - expect(notification.getBadgeCount()).toEqual(someBadgeCount); - }); - - it('should return the category', () => { - expect(notification.getCategory()).toEqual(someCategory); - }); - - it('should return the thread', () => { - expect(notification.getThread()).toEqual('thread-1'); - }); - - it('should return the custom data', () => { - expect(notification.getData()).toEqual({ key1: 'value1', key2: 'value2' }); - }); - }); - }); - - describe('for a managed iOS push notification (silent notification, with managedAps key and content-available = 1)', () => { - let managedNativeNotification = { - aps: { - 'content-available': 1, - badge: someBadgeCount - }, - managedAps: { - action: 'CREATE', - notificationId: '1', - alert: { - title: 'some title', - body: 'some body' - }, - sound: someSound, - category: someCategory - }, - key1: 'value1', - key2: 'value2' - }; - - beforeEach(() => { - notification = new IOSNotification(managedNativeNotification); - }); - - it('should return managed type', function () { - expect(notification.getType()).toEqual('managed'); - }); - - it('should return the alert object', () => { - expect(notification.getMessage()).toEqual(managedNativeNotification.managedAps.alert); - }); - - it('should return the sound', () => { - expect(notification.getSound()).toEqual(someSound); - }); - - it('should return the badge count', () => { - expect(notification.getBadgeCount()).toEqual(someBadgeCount); - }); - - it('should return the category', () => { - expect(notification.getCategory()).toEqual(someCategory); - }); - - it('should return the custom data', () => { - expect(notification.getData()).toEqual({ managedAps: managedNativeNotification.managedAps, key1: 'value1', key2: 'value2' }); - }); - }); -});