separate notification dto identifier implementation

This commit is contained in:
Artal Druk
2020-01-16 15:59:58 +02:00
parent 617c3bb508
commit 325e892eae
6 changed files with 47 additions and 15 deletions

View File

@@ -1,4 +1,6 @@
import { Notification } from './Notification';
import {NotificationIOS} from "./NotificationIOS";
import {NotificationAndroid} from "./NotificationAndroid";
describe('Notification', () => {
it('Should create notification with payload', () => {
const payload = { p: 'p' };
@@ -6,12 +8,18 @@ describe('Notification', () => {
expect(notification.payload).toEqual(payload);
});
it('Should create notification with identifier', () => {
it('Should create iOS notification with identifier', () => {
const payload = { identifier: 'identifier' };
const notification = new Notification(payload);
const notification = new NotificationIOS(payload);
expect(notification.identifier).toEqual(payload.identifier);
});
it('Should create Android notification with identifier', () => {
const payload = { 'google.message_id': 'identifier' };
const notification = new NotificationAndroid(payload);
expect(notification.identifier).toEqual('identifier');
});
it('Should return title from payload', () => {
const payload = { title: 'title' };
const notification = new Notification(payload);

View File

@@ -1,10 +1,9 @@
export class Notification {
identifier: string;
identifier?: string;
payload: any;
constructor(payload: object) {
this.payload = payload;
this.identifier = this.payload.identifier;
}
get title(): string {

View File

@@ -1,7 +1,11 @@
import {Notification} from './Notification';
import * as _ from 'lodash';
export class NotificationAndroid extends Notification {
constructor(payload: object) {
super(payload);
this.identifier = this.payload["google.message_id"];
}
get title(): string {
return this.payload.title;
}

View File

@@ -2,6 +2,12 @@ import {Notification} from './Notification';
import * as _ from 'lodash';
export class NotificationIOS extends Notification {
identifier: string;
constructor(payload: object) {
super(payload);
this.identifier = this.payload.identifier;
}
get aps(): any {
return this.payload.aps || {};
}

View File

@@ -1,7 +1,8 @@
import { NativeCommandsSender } from './NativeCommandsSender';
import { Notification } from '../DTO/Notification';
import { NotificationCompletion } from '../interfaces/NotificationCompletion';
import { Platform } from 'react-native';
import {NotificationIOS} from "../DTO/NotificationIOS";
import {Notification} from "..";
export class CompletionCallbackWrapper {
constructor(
@@ -12,7 +13,7 @@ export class CompletionCallbackWrapper {
return (notification) => {
const completion = (response: NotificationCompletion) => {
if (Platform.OS === 'ios') {
this.nativeCommandsSender.finishPresentingNotification(notification.identifier, response);
this.nativeCommandsSender.finishPresentingNotification((notification as unknown as NotificationIOS).identifier, response);
}
};
@@ -24,7 +25,7 @@ export class CompletionCallbackWrapper {
return (notification) => {
const completion = () => {
if (Platform.OS === 'ios') {
this.nativeCommandsSender.finishHandlingAction(notification.identifier);
this.nativeCommandsSender.finishHandlingAction((notification as unknown as NotificationIOS).identifier);
}
};

View File

@@ -8,13 +8,16 @@ import { UniqueIdProvider } from '../adapters/UniqueIdProvider';
import { NotificationCategory } from '../interfaces/NotificationCategory';
import { NotificationPermissions } from '../interfaces/NotificationPermissions';
import { NotificationFactory } from '../DTO/NotificationFactory';
import {NotificationAndroid} from "../DTO/NotificationAndroid";
import {Platform} from "react-native";
import {NotificationIOS} from "../DTO/NotificationIOS";
describe('Commands', () => {
let uut: Commands;
let mockedNativeCommandsSender: NativeCommandsSender;
let mockedUniqueIdProvider: UniqueIdProvider;
let notificationFactory: NotificationFactory
beforeEach(() => {
notificationFactory = new NotificationFactory();
mockedNativeCommandsSender = mock(NativeCommandsSender);
@@ -33,10 +36,21 @@ describe('Commands', () => {
verify(mockedNativeCommandsSender.getInitialNotification()).called();
});
it('returns a promise with the initial notification', async () => {
const expectedNotification: Notification = new Notification({identifier: 'id'});
it('android - returns a promise with the initial notification', async () => {
Platform.OS = 'android';
const expectedNotification: Notification = new NotificationAndroid({'google.message_id': 'id'});
when(mockedNativeCommandsSender.getInitialNotification()).thenResolve(
{identifier: 'id'}
{'google.message_id': 'id'}
);
const result = await uut.getInitialNotification();
expect(result).toEqual(expectedNotification);
});
it('iOS - returns a promise with the initial notification', async () => {
Platform.OS = 'ios';
const expectedNotification: Notification = new NotificationIOS({identifier: 'id'});
when(mockedNativeCommandsSender.getInitialNotification()).thenResolve(
{identifier: 'id'}
);
const result = await uut.getInitialNotification();
expect(result).toEqual(expectedNotification);
@@ -99,7 +113,7 @@ describe('Commands', () => {
verify(mockedNativeCommandsSender.postLocalNotification(notification, passedId)).called();
});
});
describe('getBadgeCount', () => {
it('sends to native', () => {
uut.getBadgeCount();
@@ -151,7 +165,7 @@ describe('Commands', () => {
expect(isRegistered).toEqual(false);
});
});
describe('checkPermissions', () => {
it('sends to native', () => {
uut.checkPermissions();