Skip to content

Commit 9d09931

Browse files
authored
feat: subscribeTo and subscribeFrom push notifications (#276)
Implementation of #275
1 parent bf91235 commit 9d09931

6 files changed

Lines changed: 157 additions & 1 deletion

File tree

.prettierignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/dist
2-
/coverage
2+
/coverage
3+
CHANGELOG.md

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2029,6 +2029,38 @@ verifyIdentity({orderId: '123456789AAA'})
20292029
- `505`: Identity verification flow is not supported on this device (Android
20302030
< 9)
20312031
2032+
### subscribeToPushNotifications
2033+
2034+
<kbd>App version >= TBD</kbd>
2035+
2036+
Tells the native app to start the push notification subscription flow. The app
2037+
first verifies that notifications permission is granted; if so, it starts the
2038+
corresponding SDK and executes the push subscription through it.
2039+
2040+
```ts
2041+
subscribeToPushNotifications: () => Promise<void>;
2042+
```
2043+
2044+
#### Error cases
2045+
2046+
- `401`: Push notifications permission is not granted
2047+
- `500`: Any other error that prevented the operation from completing
2048+
2049+
### unsubscribeFromPushNotifications
2050+
2051+
<kbd>App version >= TBD</kbd>
2052+
2053+
Tells the native app to unsubscribe from pushes through the corresponding SDK.
2054+
No permission is required.
2055+
2056+
```ts
2057+
unsubscribeFromPushNotifications: () => Promise<void>;
2058+
```
2059+
2060+
#### Error cases
2061+
2062+
- `500`: Any other error that prevented the operation from completing
2063+
20322064
## Error handling
20332065
20342066
If an uncontrolled error occurs, promise will be rejected with an error object:

index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,11 @@ export {
147147

148148
export {verifyIdentity} from './src/identity-verification';
149149

150+
export {
151+
subscribeToPushNotifications,
152+
unsubscribeFromPushNotifications,
153+
} from './src/push-notifications';
154+
150155
export {
151156
type LocatorSdkMode,
152157
setupLocatorSdkConfig,
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import {
2+
subscribeToPushNotifications,
3+
unsubscribeFromPushNotifications,
4+
} from '../push-notifications';
5+
import {createFakeAndroidPostMessage} from './fake-post-message';
6+
7+
test('subscribeToPushNotifications - success', async () => {
8+
createFakeAndroidPostMessage({
9+
checkMessage: (msg) => {
10+
expect(msg.type).toBe('SUBSCRIBE_TO_PUSH_NOTIFICATIONS');
11+
expect(msg.payload).toBeUndefined();
12+
},
13+
getResponse: (msg) => ({
14+
type: 'SUBSCRIBE_TO_PUSH_NOTIFICATIONS',
15+
id: msg.id,
16+
}),
17+
});
18+
19+
const res = await subscribeToPushNotifications();
20+
expect(res).toBeUndefined();
21+
});
22+
23+
test('subscribeToPushNotifications - missing permission error', async () => {
24+
createFakeAndroidPostMessage({
25+
getResponse: (msg) => ({
26+
type: 'ERROR',
27+
id: msg.id,
28+
payload: {
29+
code: 401,
30+
reason: 'Missing Push notifications permission',
31+
},
32+
}),
33+
});
34+
35+
await expect(subscribeToPushNotifications()).rejects.toEqual({
36+
code: 401,
37+
reason: 'Missing Push notifications permission',
38+
});
39+
});
40+
41+
test('subscribeToPushNotifications - generic error', async () => {
42+
createFakeAndroidPostMessage({
43+
getResponse: (msg) => ({
44+
type: 'ERROR',
45+
id: msg.id,
46+
payload: {code: 500, reason: 'Internal error'},
47+
}),
48+
});
49+
50+
await expect(subscribeToPushNotifications()).rejects.toEqual({
51+
code: 500,
52+
reason: 'Internal error',
53+
});
54+
});
55+
56+
test('unsubscribeFromPushNotifications - success', async () => {
57+
createFakeAndroidPostMessage({
58+
checkMessage: (msg) => {
59+
expect(msg.type).toBe('UNSUBSCRIBE_FROM_PUSH_NOTIFICATIONS');
60+
expect(msg.payload).toBeUndefined();
61+
},
62+
getResponse: (msg) => ({
63+
type: 'UNSUBSCRIBE_FROM_PUSH_NOTIFICATIONS',
64+
id: msg.id,
65+
}),
66+
});
67+
68+
const res = await unsubscribeFromPushNotifications();
69+
expect(res).toBeUndefined();
70+
});
71+
72+
test('unsubscribeFromPushNotifications - generic error', async () => {
73+
createFakeAndroidPostMessage({
74+
getResponse: (msg) => ({
75+
type: 'ERROR',
76+
id: msg.id,
77+
payload: {code: 500, reason: 'Internal error'},
78+
}),
79+
});
80+
81+
await expect(unsubscribeFromPushNotifications()).rejects.toEqual({
82+
code: 500,
83+
reason: 'Internal error',
84+
});
85+
});

src/post-message.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,16 @@ export type ResponsesFromNativeApp = {
597597
id: string;
598598
payload: void;
599599
};
600+
SUBSCRIBE_TO_PUSH_NOTIFICATIONS: {
601+
type: 'SUBSCRIBE_TO_PUSH_NOTIFICATIONS';
602+
id: string;
603+
payload: void;
604+
};
605+
UNSUBSCRIBE_FROM_PUSH_NOTIFICATIONS: {
606+
type: 'UNSUBSCRIBE_FROM_PUSH_NOTIFICATIONS';
607+
id: string;
608+
payload: void;
609+
};
600610
};
601611

602612
export type NativeAppResponsePayload<

src/push-notifications.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import {postMessageToNativeApp} from './post-message';
2+
3+
/**
4+
* Tells the native app to start the push notification subscription flow.
5+
* The native app verifies the notifications permission first; if granted, it
6+
* starts the corresponding SDK and executes the push subscription.
7+
*
8+
* Error cases:
9+
* - 401: Push notifications permission is not granted
10+
* - 500: Any other error that prevented the operation from completing
11+
*/
12+
export const subscribeToPushNotifications = (): Promise<void> =>
13+
postMessageToNativeApp({type: 'SUBSCRIBE_TO_PUSH_NOTIFICATIONS'});
14+
15+
/**
16+
* Tells the native app to unsubscribe from pushes through the corresponding SDK.
17+
* No permission is required.
18+
*
19+
* Error cases:
20+
* - 500: Any other error that prevented the operation from completing
21+
*/
22+
export const unsubscribeFromPushNotifications = (): Promise<void> =>
23+
postMessageToNativeApp({type: 'UNSUBSCRIBE_FROM_PUSH_NOTIFICATIONS'});

0 commit comments

Comments
 (0)