Skip to content

Commit 325e892

Browse files
committed
separate notification dto identifier implementation
1 parent 617c3bb commit 325e892

File tree

6 files changed

+47
-15
lines changed

6 files changed

+47
-15
lines changed

lib/src/DTO/Notification.test.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
11
import { Notification } from './Notification';
2+
import {NotificationIOS} from "./NotificationIOS";
3+
import {NotificationAndroid} from "./NotificationAndroid";
24
describe('Notification', () => {
35
it('Should create notification with payload', () => {
46
const payload = { p: 'p' };
57
const notification = new Notification(payload);
68
expect(notification.payload).toEqual(payload);
79
});
810

9-
it('Should create notification with identifier', () => {
11+
it('Should create iOS notification with identifier', () => {
1012
const payload = { identifier: 'identifier' };
11-
const notification = new Notification(payload);
13+
const notification = new NotificationIOS(payload);
1214
expect(notification.identifier).toEqual(payload.identifier);
1315
});
14-
16+
17+
it('Should create Android notification with identifier', () => {
18+
const payload = { 'google.message_id': 'identifier' };
19+
const notification = new NotificationAndroid(payload);
20+
expect(notification.identifier).toEqual('identifier');
21+
});
22+
1523
it('Should return title from payload', () => {
1624
const payload = { title: 'title' };
1725
const notification = new Notification(payload);

lib/src/DTO/Notification.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
export class Notification {
2-
identifier: string;
2+
identifier?: string;
33
payload: any;
44

55
constructor(payload: object) {
66
this.payload = payload;
7-
this.identifier = this.payload.identifier;
87
}
98

109
get title(): string {

lib/src/DTO/NotificationAndroid.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import {Notification} from './Notification';
2-
import * as _ from 'lodash';
32

43
export class NotificationAndroid extends Notification {
4+
constructor(payload: object) {
5+
super(payload);
6+
this.identifier = this.payload["google.message_id"];
7+
}
8+
59
get title(): string {
610
return this.payload.title;
711
}

lib/src/DTO/NotificationIOS.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ import {Notification} from './Notification';
22
import * as _ from 'lodash';
33

44
export class NotificationIOS extends Notification {
5+
identifier: string;
6+
constructor(payload: object) {
7+
super(payload);
8+
this.identifier = this.payload.identifier;
9+
}
10+
511
get aps(): any {
612
return this.payload.aps || {};
713
}

lib/src/adapters/CompletionCallbackWrapper.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { NativeCommandsSender } from './NativeCommandsSender';
2-
import { Notification } from '../DTO/Notification';
32
import { NotificationCompletion } from '../interfaces/NotificationCompletion';
43
import { Platform } from 'react-native';
4+
import {NotificationIOS} from "../DTO/NotificationIOS";
5+
import {Notification} from "..";
56

67
export class CompletionCallbackWrapper {
78
constructor(
@@ -12,7 +13,7 @@ export class CompletionCallbackWrapper {
1213
return (notification) => {
1314
const completion = (response: NotificationCompletion) => {
1415
if (Platform.OS === 'ios') {
15-
this.nativeCommandsSender.finishPresentingNotification(notification.identifier, response);
16+
this.nativeCommandsSender.finishPresentingNotification((notification as unknown as NotificationIOS).identifier, response);
1617
}
1718
};
1819

@@ -24,7 +25,7 @@ export class CompletionCallbackWrapper {
2425
return (notification) => {
2526
const completion = () => {
2627
if (Platform.OS === 'ios') {
27-
this.nativeCommandsSender.finishHandlingAction(notification.identifier);
28+
this.nativeCommandsSender.finishHandlingAction((notification as unknown as NotificationIOS).identifier);
2829
}
2930
};
3031

lib/src/commands/Commands.test.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,16 @@ import { UniqueIdProvider } from '../adapters/UniqueIdProvider';
88
import { NotificationCategory } from '../interfaces/NotificationCategory';
99
import { NotificationPermissions } from '../interfaces/NotificationPermissions';
1010
import { NotificationFactory } from '../DTO/NotificationFactory';
11+
import {NotificationAndroid} from "../DTO/NotificationAndroid";
12+
import {Platform} from "react-native";
13+
import {NotificationIOS} from "../DTO/NotificationIOS";
1114

1215
describe('Commands', () => {
1316
let uut: Commands;
1417
let mockedNativeCommandsSender: NativeCommandsSender;
1518
let mockedUniqueIdProvider: UniqueIdProvider;
1619
let notificationFactory: NotificationFactory
17-
20+
1821
beforeEach(() => {
1922
notificationFactory = new NotificationFactory();
2023
mockedNativeCommandsSender = mock(NativeCommandsSender);
@@ -33,10 +36,21 @@ describe('Commands', () => {
3336
verify(mockedNativeCommandsSender.getInitialNotification()).called();
3437
});
3538

36-
it('returns a promise with the initial notification', async () => {
37-
const expectedNotification: Notification = new Notification({identifier: 'id'});
39+
it('android - returns a promise with the initial notification', async () => {
40+
Platform.OS = 'android';
41+
const expectedNotification: Notification = new NotificationAndroid({'google.message_id': 'id'});
3842
when(mockedNativeCommandsSender.getInitialNotification()).thenResolve(
39-
{identifier: 'id'}
43+
{'google.message_id': 'id'}
44+
);
45+
const result = await uut.getInitialNotification();
46+
expect(result).toEqual(expectedNotification);
47+
});
48+
49+
it('iOS - returns a promise with the initial notification', async () => {
50+
Platform.OS = 'ios';
51+
const expectedNotification: Notification = new NotificationIOS({identifier: 'id'});
52+
when(mockedNativeCommandsSender.getInitialNotification()).thenResolve(
53+
{identifier: 'id'}
4054
);
4155
const result = await uut.getInitialNotification();
4256
expect(result).toEqual(expectedNotification);
@@ -99,7 +113,7 @@ describe('Commands', () => {
99113
verify(mockedNativeCommandsSender.postLocalNotification(notification, passedId)).called();
100114
});
101115
});
102-
116+
103117
describe('getBadgeCount', () => {
104118
it('sends to native', () => {
105119
uut.getBadgeCount();
@@ -151,7 +165,7 @@ describe('Commands', () => {
151165
expect(isRegistered).toEqual(false);
152166
});
153167
});
154-
168+
155169
describe('checkPermissions', () => {
156170
it('sends to native', () => {
157171
uut.checkPermissions();

0 commit comments

Comments
 (0)