Commit d0fefcb8 authored by yogevbd's avatar yogevbd

Identical API for Android and iOS

parent 4520e23b
......@@ -10,7 +10,7 @@
@end
@interface RCTConvert (UNNotificationRequest)
+ (UNNotificationRequest *)UNNotificationRequest:(id)json withId:(NSString*)notificationId;
+ (UNNotificationRequest *)UNNotificationRequest:(id)json withId:(NSNumber*)notificationId;
@end
@interface RCTConvert (UNNotification)
......
......@@ -55,7 +55,7 @@
@implementation RCTConvert (UNNotificationRequest)
+ (UNNotificationRequest *)UNNotificationRequest:(id)json withId:(NSString*)notificationId
+ (UNNotificationRequest *)UNNotificationRequest:(id)json withId:(NSNumber*)notificationId
{
NSDictionary<NSString *, id> *details = [self NSDictionary:json];
......@@ -68,7 +68,7 @@
if ([RCTConvert BOOL:details[@"silent"]]) {
content.sound = nil;
}
content.userInfo = [RCTConvert NSDictionary:details[@"userInfo"]] ?: @{};
content.userInfo = [RCTConvert NSDictionary:details] ?: @{};
content.categoryIdentifier = [RCTConvert NSString:details[@"category"]];
NSDate *triggerDate = [RCTConvert NSDate:details[@"fireDate"]];
......@@ -84,7 +84,7 @@
repeats:NO];
}
return [UNNotificationRequest requestWithIdentifier:notificationId
return [UNNotificationRequest requestWithIdentifier:[NSString stringWithFormat:@"%@", notificationId]
content:content trigger:trigger];
}
......
......@@ -68,8 +68,8 @@ RCT_EXPORT_METHOD(setBadgesCount:(int)count) {
[_commandsHandler setBadgesCount:count];
}
RCT_EXPORT_METHOD(localNotification:(NSDictionary *)notification withId:(NSString *)notificationId) {
[_commandsHandler sendLocalNotification:notification withId:notificationId];
RCT_EXPORT_METHOD(postLocalNotification:(NSDictionary *)notification withId:(nonnull NSNumber *)notificationId) {
[_commandsHandler postLocalNotification:notification withId:notificationId];
}
RCT_EXPORT_METHOD(cancelLocalNotification:(NSString *)notificationId) {
......
......@@ -23,7 +23,7 @@
- (void)setBadgesCount:(int)count;
- (void)sendLocalNotification:(NSDictionary *)notification withId:(NSString *)notificationId;
- (void)postLocalNotification:(NSDictionary *)notification withId:(NSNumber *)notificationId;
- (void)cancelLocalNotification:(NSString *)notificationId;
......
......@@ -50,8 +50,8 @@
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:count];
}
- (void)sendLocalNotification:(NSDictionary *)notification withId:(NSString *)notificationId {
[_notificationCenter sendLocalNotification:notification withId:notificationId];
- (void)postLocalNotification:(NSDictionary *)notification withId:(NSNumber *)notificationId {
[_notificationCenter postLocalNotification:notification withId:notificationId];
}
- (void)cancelLocalNotification:(NSString *)notificationId {
......
......@@ -3,7 +3,7 @@
static NSString* const RNRegistered = @"remoteNotificationsRegistered";
static NSString* const RNRegistrationFailed = @"remoteNotificationsRegistrationFailed";
static NSString* const RNPushKitRegistered = @"pushKitRegistered";
static NSString* const RNNotificationReceivedForeground = @"notificationReceivedForeground";
static NSString* const RNNotificationReceived = @"notificationReceived";
static NSString* const RNNotificationOpened = @"notificationOpened";
static NSString* const RNPushKitNotificationReceived = @"pushKitNotificationReceived";
......
......@@ -8,7 +8,7 @@ RCT_EXPORT_MODULE();
return @[RNRegistered,
RNRegistrationFailed,
RNPushKitRegistered,
RNNotificationReceivedForeground,
RNNotificationReceived,
RNNotificationOpened,
RNPushKitNotificationReceived];
}
......
......@@ -16,7 +16,7 @@ typedef void (^RCTPromiseRejectBlock)(NSString *code, NSString *message, NSError
- (void)checkPermissions:(RCTPromiseResolveBlock)resolve;
- (void)sendLocalNotification:(NSDictionary *)notification withId:(NSString *)notificationId;
- (void)postLocalNotification:(NSDictionary *)notification withId:(NSNumber *)notificationId;
- (void)cancelLocalNotification:(NSString *)notificationId;
......
......@@ -30,7 +30,7 @@
[[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:categories];
}
- (void)sendLocalNotification:(NSDictionary *)notification withId:(NSString *)notificationId {
- (void)postLocalNotification:(NSDictionary *)notification withId:(NSNumber *)notificationId {
UNNotificationRequest* localNotification = [RCTConvert UNNotificationRequest:notification withId:notificationId];
[[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:localNotification withCompletionHandler:nil];
}
......
......@@ -25,12 +25,12 @@
- (void)didReceiveForegroundNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
[_store setPresentationCompletionHandler:completionHandler withCompletionKey:notification.request.identifier];
[RNEventEmitter sendEvent:RNNotificationReceivedForeground body:[RNNotificationParser parseNotification:notification]];
[RNEventEmitter sendEvent:RNNotificationReceived body:[RNNotificationParser parseNotification:notification]];
}
- (void)didReceiveNotificationResponse:(UNNotificationResponse *)response completionHandler:(void (^)(void))completionHandler {
[_store setActionCompletionHandler:completionHandler withCompletionKey:response.notification.request.identifier];
[RNEventEmitter sendEvent:RNNotificationOpened body:[RNNotificationParser parseNotificationResponse:response]];
[RNEventEmitter sendEvent:RNNotificationOpened body:[RNNotificationParser parseNotification:response.notification]];
}
@end
......@@ -60,9 +60,9 @@
UNNotification* notification = [self createNotificationWithIdentifier:@"id" andUserInfo:@{@"extraKey": @"extraValue"}];
void (^testBlock)(UNNotificationPresentationOptions) = ^void(UNNotificationPresentationOptions options) {};
[[_mockedNotificationCenter expect] postNotificationName:RNNotificationReceivedForeground object:[OCMArg any] userInfo:[OCMArg checkWithBlock:^BOOL(id obj) {
[[_mockedNotificationCenter expect] postNotificationName:RNNotificationReceived object:[OCMArg any] userInfo:[OCMArg checkWithBlock:^BOOL(id obj) {
return ([[obj valueForKey:@"identifier"] isEqualToString:@"id"] &&
[[[obj valueForKey:@"data"] valueForKey:@"extraKey"] isEqualToString:@"extraValue"]);
[[obj valueForKey:@"extraKey"] isEqualToString:@"extraValue"]);
}]];
[_uut didReceiveForegroundNotification:notification withCompletionHandler:testBlock];
[_mockedNotificationCenter verify];
......
......@@ -6,6 +6,5 @@ public interface Defs {
String TOKEN_RECEIVED_EVENT_NAME = "remoteNotificationsRegistered";
String NOTIFICATION_RECEIVED_EVENT_NAME = "notificationReceived";
String NOTIFICATION_RECEIVED_FOREGROUND_EVENT_NAME = "notificationReceivedInForeground";
String NOTIFICATION_OPENED_EVENT_NAME = "notificationOpened";
}
......@@ -14,6 +14,7 @@ import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReadableMap;
import com.wix.reactnativenotifications.core.AppLifecycleFacadeHolder;
import com.wix.reactnativenotifications.core.InitialNotificationHolder;
......@@ -41,7 +42,7 @@ public class RNNotificationsModule extends ReactContextBaseJavaModule implements
@Override
public String getName() {
return "WixRNNotifications";
return "RNBridgeModule";
}
@Override
......@@ -106,6 +107,11 @@ public class RNNotificationsModule extends ReactContextBaseJavaModule implements
notificationsDrawer.onNotificationClearRequest(notificationId);
}
@ReactMethod
public void setCategories(ReadableArray categories) {
}
@ReactMethod
public void isRegisteredForRemoteNotifications(Promise promise) {
boolean hasPermission = NotificationManagerCompat.from(getReactApplicationContext()).areNotificationsEnabled();
......
......@@ -23,7 +23,7 @@ import com.wix.reactnativenotifications.core.ProxyService;
import static com.wix.reactnativenotifications.Defs.NOTIFICATION_OPENED_EVENT_NAME;
import static com.wix.reactnativenotifications.Defs.NOTIFICATION_RECEIVED_EVENT_NAME;
import static com.wix.reactnativenotifications.Defs.NOTIFICATION_RECEIVED_FOREGROUND_EVENT_NAME;
public class PushNotification implements IPushNotification {
......@@ -64,9 +64,6 @@ public class PushNotification implements IPushNotification {
public void onReceived() throws InvalidNotificationException {
postNotification(null);
notifyReceivedToJS();
if (mAppLifecycleFacade.isAppVisible()) {
notifiyReceivedForegroundNotificationToJS();
}
}
@Override
......@@ -192,10 +189,6 @@ public class PushNotification implements IPushNotification {
mJsIOHelper.sendEventToJS(NOTIFICATION_RECEIVED_EVENT_NAME, mNotificationProps.asBundle(), mAppLifecycleFacade.getRunningReactContext());
}
private void notifiyReceivedForegroundNotificationToJS() {
mJsIOHelper.sendEventToJS(NOTIFICATION_RECEIVED_FOREGROUND_EVENT_NAME, mNotificationProps.asBundle(), mAppLifecycleFacade.getRunningReactContext());
}
private void notifyOpenedToJS() {
mJsIOHelper.sendEventToJS(NOTIFICATION_OPENED_EVENT_NAME, mNotificationProps.asBundle(), mAppLifecycleFacade.getRunningReactContext());
}
......
......@@ -3,7 +3,7 @@ apply plugin: "com.android.application"
project.ext.react = [
root : "../../../",
entryFile: "index.js",
bundleAssetName: "index.android.bundle",
bundleAssetName: "index.bundle",
bundleInAlpha: true,
bundleInBeta: true
]
......
......@@ -35,6 +35,6 @@ public class MainActivity extends ReactActivity {
}
private void startReactApplication() {
mReactRootView.startReactApplication(getReactInstanceManager(), "WixRNNotifications", null);
mReactRootView.startReactApplication(getReactInstanceManager(), "NotificationsExampleApp", null);
}
}
'use strict';
import React, {Component} from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View,
Button,
TouchableHighlight
} from 'react-native';
import {NotificationsAndroid, PendingNotifications} from 'react-native-notifications';
let mainScreen;
function onPushRegistered() {
if (mainScreen) {
mainScreen.onPushRegistered();
}
}
function onNotificationOpened(notification) {
if (mainScreen) {
mainScreen.onNotificationOpened(notification)
}
}
function onNotificationReceived(notification) {
if (mainScreen) {
mainScreen.onNotificationReceived(notification)
}
}
// It's highly recommended to keep listeners registration at global scope rather than at screen-scope seeing that
// component mount and unmount lifecycle tends to be asymmetric!
NotificationsAndroid.setRegistrationTokenUpdateListener(onPushRegistered);
NotificationsAndroid.setNotificationOpenedListener(onNotificationOpened);
NotificationsAndroid.setNotificationReceivedListener(onNotificationReceived);
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
},
titleText: {
fontSize: 24,
textAlign: 'center',
margin: 10,
},
bodyText: {
fontSize: 18,
textAlign: 'center',
margin: 10,
},
mainButtonText: {
fontSize: 25,
fontStyle: 'italic',
fontWeight: 'bold',
textAlign: 'center',
margin: 10,
},
plainButtonText: {
fontSize: 18,
fontStyle: 'italic',
textAlign: 'center',
margin: 10,
},
});
class MainComponent extends Component {
constructor(props) {
super(props);
this.onPostNotification = this.onPostNotification.bind(this);
this.onCancelNotification = this.onCancelNotification.bind(this);
this.state = {
elapsed: 0,
lastNotification: undefined
};
console.log('ReactScreen', 'ReactScreen');
mainScreen = this;
setInterval(this.onTick.bind(this), 1000);
}
componentDidMount() {
console.log('ReactScreen', 'componentDidMount');
PendingNotifications.getInitialNotification()
.then((notification) => {console.log("getInitialNotification:", notification); this.setState({initialNotification: (notification ? notification.getData() : undefined)});})
.catch((err) => console.error("getInitialNotifiation failed", err));
}
componentWillUnmount() {
console.log('ReactScreen', 'componentWillUnmount');
}
onTick() {
this.setState({elapsed: this.state.elapsed + 1});
}
onPostNotification() {
this.lastNotificationId = NotificationsAndroid.localNotification({title: "Local notification", body: "This notification was generated by the app!"});
}
onCancelNotification() {
if (this.lastNotificationId) {
NotificationsAndroid.cancelLocalNotification(this.lastNotificationId);
this.lastNotificationId = undefined;
}
}
render() {
return (
<View style={styles.container}>
<Text style={styles.titleText}>Wix React Native Notifications</Text>
<Text style={styles.bodyText}>{this.state.initialNotification ? 'Opened from notification' : ''}</Text>
<Text style={styles.bodyText}>Last notification: {this.state.lastNotification ? '\n'+this.state.lastNotification.body + ` (opened at ''${this.state.notificationRxTime})` : "N/A"}</Text>
<Text style={styles.bodyText}>Time elapsed: {this.state.elapsed}</Text>
<Text>{"\n\n"}</Text>
<TouchableHighlight onPress={() => this.onPostNotification()}>
<Text style={styles.mainButtonText}>Try Me!</Text>
</TouchableHighlight>
<TouchableHighlight onPress={() => this.onCancelNotification()}>
<Text style={styles.plainButtonText}>Undo last</Text>
</TouchableHighlight>
<TouchableHighlight onPress={() => this.onCheckPermissions()}>
<Text style={styles.plainButtonText}>Check permissions</Text>
</TouchableHighlight>
<Button title={'Send local notification'} onPress={this.sendLocalNotification} testID={'sendLocalNotification'}/>
</View>
)
}
async onCheckPermissions() {
const hasPermissions = await NotificationsAndroid.isRegisteredForRemoteNotifications();
if (hasPermissions) {
alert('Yay! You have permissions');
} else {
alert('Boo! You don\'t have permissions');
}
}
sendLocalNotification() {
NotificationsAndroid.localNotification({
title: "Local notification",
body: "This notification was generated by the app!",
extra: "data"
});
}
onPushRegistered() {
}
onNotificationOpened(notification) {
console.log("onNotificationOpened: ", notification);
this.setState({lastNotification: notification.getData(), notificationRxTime: this.state.elapsed});
}
onNotificationReceived(notification) {
console.log("onNotificationReceived: ", notification);
}
}
AppRegistry.registerComponent('WixRNNotifications', () => MainComponent);
......@@ -28,9 +28,9 @@ class NotificationsExampleApp extends Component {
completion({alert: true, sound: false, badge: false});
});
Notifications.events().registerRemoteNotificationOpened((response, completion) => {
Notifications.events().registerRemoteNotificationOpened((notification, completion) => {
this.setState({
notifications: [...this.state.notifications, `Notification Clicked: ${response.notification.link}`]
notifications: [...this.state.notifications, `Notification Clicked: ${notification.link}`]
});
completion();
......@@ -72,12 +72,12 @@ class NotificationsExampleApp extends Component {
}
sendLocalNotification() {
Notifications.localNotification({
Notifications.postLocalNotification({
body: 'Local notificiation!',
title: 'Local Notification Title',
sound: 'chime.aiff',
category: 'SOME_CATEGORY',
userInfo: { link: 'localNotificationLink' },
link: 'localNotificationLink',
});
}
......
......@@ -34,17 +34,17 @@ export class NotificationsRoot {
}
/**
* registerPushKit
* registerPushKit - iOS only
*/
public registerPushKit() {
return this.commands.registerPushKit();
}
/**
* Reset the app to a new layout
* postLocalNotification
*/
public localNotification(notification: Notification, id: string) {
return this.commands.sendLocalNotification(notification, id);
public postLocalNotification(notification: Notification, id: number) {
return this.commands.postLocalNotification(notification, id);
}
/**
......@@ -125,4 +125,11 @@ export class NotificationsRoot {
public events(): EventsRegistry {
return this.eventsRegistry;
}
/**
* getDeliveredNotifications
*/
public getDeliveredNotifications(): Array<Notification> {
return this.commands.getDeliveredNotifications();
}
}
import { NativeCommandsSender } from './NativeCommandsSender';
import { NotificationCompletion, Notification } from '../interfaces/Notification';
import { NotificationResponse } from '../interfaces/NotificationEvents';
import { Platform } from 'react-native';
export class CompletionCallbackWrapper {
constructor(
private readonly nativeCommandsSender: NativeCommandsSender
......@@ -10,20 +9,24 @@ export class CompletionCallbackWrapper {
public wrapReceivedCallback(callback: Function): (notification: Notification) => void {
return (notification) => {
const completion = (response: NotificationCompletion) => {
if (Platform.OS === 'ios') {
this.nativeCommandsSender.finishPresentingNotification(notification.identifier, response);
}
};
callback(notification, completion);
}
}
public wrapOpenedCallback(callback: Function): (response: NotificationResponse) => void {
return (response) => {
public wrapOpenedCallback(callback: Function): (notification: Notification) => void {
return (notification) => {
const completion = () => {
this.nativeCommandsSender.finishHandlingAction(response.notification.identifier);
if (Platform.OS === 'ios') {
this.nativeCommandsSender.finishHandlingAction(notification.identifier);
}
};
callback(response, completion);
callback(notification, completion);
}
}
}
......@@ -3,7 +3,7 @@ import { Notification, NotificationCategory, NotificationPermissions, Notificati
interface NativeCommandsModule {
getInitialNotification(): Promise<Notification>;
localNotification(notification: Notification, id: string): void;
postLocalNotification(notification: Notification, id: number): void;
requestPermissions(): void;
abandonPermissions(): void;
registerPushKit(): void;
......@@ -15,6 +15,7 @@ interface NativeCommandsModule {
checkPermissions(): Promise<NotificationPermissions>;
removeDeliveredNotifications(identifiers: Array<string>): void;
removeAllDeliveredNotifications(): void;
getDeliveredNotifications(): Array<Notification>;
setCategories(categories: [NotificationCategory?]): void;
finishPresentingNotification(notificationId: string, callback: NotificationCompletion): void;
finishHandlingAction(notificationId: string): void;
......@@ -26,8 +27,8 @@ export class NativeCommandsSender {
this.nativeCommandsModule = NativeModules.RNBridgeModule;
}
sendLocalNotification(notification: Notification, id: string) {
return this.nativeCommandsModule.localNotification(notification, id);
postLocalNotification(notification: Notification, id: number) {
return this.nativeCommandsModule.postLocalNotification(notification, id);
}
getInitialNotification(): Promise<Notification> {
......@@ -82,6 +83,10 @@ export class NativeCommandsSender {
return this.nativeCommandsModule.removeDeliveredNotifications(identifiers);
}
public getDeliveredNotifications(): Array<Notification> {
return this.nativeCommandsModule.getDeliveredNotifications();
}
finishPresentingNotification(notificationId: string, notificationCompletion: NotificationCompletion): void {
this.nativeCommandsModule.finishPresentingNotification(notificationId, notificationCompletion);
}
......
import { NativeModules, NativeEventEmitter, EventEmitter, EmitterSubscription } from 'react-native';
import {
Registered, RegistrationError, RegisteredPushKit, NotificationResponse
Registered, RegistrationError, RegisteredPushKit
} from '../interfaces/NotificationEvents';
import { Notification } from '../interfaces/Notification';
......@@ -19,14 +19,14 @@ export class NativeEventsReceiver {
}
public registerRemoteNotificationReceived(callback: (notification: Notification) => void): EmitterSubscription {
return this.emitter.addListener('notificationReceivedForeground', callback);
return this.emitter.addListener('notificationReceived', callback);
}
public registerPushKitNotificationReceived(callback: (event: object) => void): EmitterSubscription {
return this.emitter.addListener('notificationReceivedForeground', callback);
return this.emitter.addListener('pushKitNotificationReceived', callback);
}
public registerRemoteNotificationOpened(callback: (response: NotificationResponse, completion: () => void) => void): EmitterSubscription {
public registerRemoteNotificationOpened(callback: (response: Notification, completion: () => void) => void): EmitterSubscription {
return this.emitter.addListener('notificationOpened', callback);
}
......
import * as _ from 'lodash';
export class UniqueIdProvider {
generate(prefix?: string): string {
return _.uniqueId(prefix);
generate(): number {
return parseInt(_.uniqueId());
}
}
import * as _ from 'lodash';
import { mock, verify, instance, when, anything, anyString } from 'ts-mockito';
import { mock, verify, instance, when, anyNumber } from 'ts-mockito';
import { Commands } from './Commands';
import { NativeCommandsSender } from '../adapters/NativeCommandsSender';
......@@ -14,7 +14,7 @@ describe('Commands', () => {
beforeEach(() => {
mockedNativeCommandsSender = mock(NativeCommandsSender);
mockedUniqueIdProvider = mock(UniqueIdProvider);
when(mockedUniqueIdProvider.generate(anything())).thenCall((prefix) => `${prefix}+UNIQUE_ID`);
when(mockedUniqueIdProvider.generate()).thenCall(() => 12);
uut = new Commands(
instance(mockedNativeCommandsSender),
instance(mockedUniqueIdProvider)
......@@ -73,24 +73,24 @@ describe('Commands', () => {
});
});
describe('sendLocalNotification', () => {
describe('postLocalNotification', () => {
it('sends to native', () => {
const notification: Notification = {identifier: 'id', alert: 'alert', data: {}};
uut.sendLocalNotification(notification);
verify(mockedNativeCommandsSender.sendLocalNotification(notification, anyString())).called();
uut.postLocalNotification(notification);
verify(mockedNativeCommandsSender.postLocalNotification(notification, anyNumber())).called();
});
it('generates unique identifier', () => {
const notification: Notification = {identifier: 'id', data: {}, alert: 'alert'};
uut.sendLocalNotification(notification);
verify(mockedNativeCommandsSender.sendLocalNotification(notification, `Notification_+UNIQUE_ID`)).called();
uut.postLocalNotification(notification);
verify(mockedNativeCommandsSender.postLocalNotification(notification, anyNumber())).called();
});
it('use passed notification id', () => {
const notification: Notification = {identifier: 'id', data: {}, alert: 'alert'};
const passedId: string = "passedId";
uut.sendLocalNotification(notification, passedId);
verify(mockedNativeCommandsSender.sendLocalNotification(notification, passedId)).called();
const passedId: number = 2;
uut.postLocalNotification(notification, passedId);
verify(mockedNativeCommandsSender.postLocalNotification(notification, passedId)).called();
});
});
......@@ -176,4 +176,11 @@ describe('Commands', () => {
verify(mockedNativeCommandsSender.removeDeliveredNotifications(identifiers)).called();
});
});
describe('getDeliveredNotifications', () => {
it('sends to native', () => {
uut.getDeliveredNotifications();
verify(mockedNativeCommandsSender.getDeliveredNotifications()).called();
});
});
});
......@@ -9,9 +9,9 @@ export class Commands {
private readonly uniqueIdProvider: UniqueIdProvider
) {}
public sendLocalNotification(notification: Notification, id?: string) {
const notificationId = id ? id : this.uniqueIdProvider.generate('Notification_');
const result = this.nativeCommandsSender.sendLocalNotification(notification, notificationId);
public postLocalNotification(notification: Notification, id?: number) {
const notificationId: number = id ? id : this.uniqueIdProvider.generate();
const result = this.nativeCommandsSender.postLocalNotification(notification, notificationId);
return result;
}
......@@ -69,4 +69,8 @@ export class Commands {
public removeDeliveredNotifications(identifiers: Array<string>) {
return this.nativeCommandsSender.removeDeliveredNotifications(identifiers);
}
public getDeliveredNotifications(): Array<Notification> {
return this.nativeCommandsSender.getDeliveredNotifications();
}
}
......@@ -4,6 +4,7 @@ import { NotificationCompletion, Notification } from '../interfaces/Notification
import { CompletionCallbackWrapper } from '../adapters/CompletionCallbackWrapper';
import { NativeCommandsSender } from '../adapters/NativeCommandsSender.mock';
import { NotificationResponse } from '../interfaces/NotificationEvents';
import { Platform } from 'react-native';
describe('EventsRegistry', () => {
let uut: EventsRegistry;
......@@ -47,7 +48,7 @@ describe('EventsRegistry', () => {
call(expectedNotification);
});
it('calling completion should invoke finishPresentingNotification', () => {
it('should invoke finishPresentingNotification', () => {
const notification: Notification = {identifier: 'notificationId', data: {}, alert: 'alert'}
const response: NotificationCompletion = {alert: true}
......@@ -59,6 +60,20 @@ describe('EventsRegistry', () => {
const call = mockNativeEventsReceiver.registerRemoteNotificationReceived.mock.calls[0][0];
call(notification);
});
it('should not invoke finishPresentingNotification on Android', () => {
Platform.OS = 'android';
const expectedNotification: Notification = {identifier: 'notificationId', data: {}, alert: 'alert'}
const response: NotificationCompletion = {alert: true}
uut.registerNotificationReceived((notification, completion) => {
completion(response);
expect(expectedNotification).toEqual(notification);
expect(mockNativeCommandsSender.finishPresentingNotification).toBeCalledTimes(0);
});
const call = mockNativeEventsReceiver.registerRemoteNotificationReceived.mock.calls[0][0];
call(expectedNotification);
});
});
describe('', () => {
......@@ -96,17 +111,30 @@ describe('EventsRegistry', () => {
});
it('calling completion should invoke finishHandlingAction', () => {
const notification: Notification = {identifier: 'notificationId', data: {}, alert: 'alert'}
const expectedResponse: NotificationResponse = {identifier: 'responseId', notification};
const expectedNotification: Notification = {identifier: 'notificationId', data: {}, alert: 'alert'}
uut.registerRemoteNotificationOpened((response, completion) => {
uut.registerRemoteNotificationOpened((notification, completion) => {
completion();
expect(response).toEqual(expectedResponse);
expect(expectedNotification).toEqual(notification);
expect(mockNativeCommandsSender.finishHandlingAction).toBeCalledWith(notification.identifier);
});
const call = mockNativeEventsReceiver.registerRemoteNotificationOpened.mock.calls[0][0];
call(expectedResponse);
call(expectedNotification);
});
it('should not invoke finishHandlingAction on Android', () => {
Platform.OS = 'android';
const expectedNotification: Notification = {identifier: 'notificationId', data: {}, alert: 'alert'}
uut.registerRemoteNotificationOpened((notification, completion) => {
completion();
expect(expectedNotification).toEqual(notification);
expect(mockNativeCommandsSender.finishHandlingAction).toBeCalledTimes(0);
});
const call = mockNativeEventsReceiver.registerRemoteNotificationOpened.mock.calls[0][0];
call(expectedNotification);
});
});
......
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