Skip to content

Commit

Permalink
WEB-2097 getUnseenNotificationsBadge/setUnseenNotificationsBadge meth…
Browse files Browse the repository at this point in the history
…ods (#163)
  • Loading branch information
atabel authored Nov 14, 2024
1 parent a86d004 commit ebd5d88
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 0 deletions.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1346,6 +1346,32 @@ that represents one installation of the native app.
getInstallationId: () => Promise<{installationId: string}>;
```
### getUnseenNotificationsBadge / setUnseenNotificationsBadge
<kbd>App version >=24.12</kbd>
get/set the number of unseen notifications in the inbox and the last time the
counter was updated (timestamp in milliseconds).
```ts
getUnseenNotificationsBadge: () => Promise<{unseenNotificationCounter: number; lastUpdated: number}>;
```
```ts
setUnseenNotificationsBadge: ({unseenNotificationCounter: number; lastUpdated: number}) => Promise<void>;
```
When the webview is opened, it will ask to the native app for the unseen
notifications badge (`getUnseenNotificationsBadge`). This allows the webview to
know if the native app has received any push while the webview was closed. The
webview will check the `lastUpdated` timestamp receibed from the native app with
the one persisted in the webview `localStorage`, if it's different, the webview
will fetch the inbox from server. When the webview updates their state, it will
persist the lastUpdated timestamp in the localStorage and send it to the native
app using the `setUnseenNotificationsBadge`. This way, the next time the webview
use the getter, it will know if the `lastUpdated` matches with the one persisted
in `localStorage`.
## Error handling
If an uncontrolled error occurs, promise will be rejected with an error object:
Expand Down
4 changes: 4 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,7 @@ export {openOnboarding} from './src/open-onboarding';

export {getProfileImage, startProfileImageFlow} from './src/profile';
export {readTextFromClipboard, writeTextToClipboard} from './src/clipboard';
export {
getUnseenNotificationsBadge,
setUnseenNotificationsBadge,
} from './src/inbox-notifications';
51 changes: 51 additions & 0 deletions src/__tests__/inbox-notifications-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import {
getUnseenNotificationsBadge,
setUnseenNotificationsBadge,
} from '../inbox-notifications';
import {createFakeWebKitPostMessage} from './fake-post-message';

test('getUnseenNotificationsBadge', async () => {
const anyTimestamp = new Date().getTime();
createFakeWebKitPostMessage({
checkMessage: (msg) => {
expect(msg.type).toBe('GET_UNSEEN_NOTIFICATIONS_BADGE');
},
getResponse: (msg) => ({
type: 'GET_UNSEEN_NOTIFICATIONS_BADGE',
id: msg.id,
payload: {
unseenNotificationCounter: 2,
lastUpdated: anyTimestamp,
},
}),
});

const res = await getUnseenNotificationsBadge();

expect(res).toEqual({
unseenNotificationCounter: 2,
lastUpdated: anyTimestamp,
});
});

test('setUnseenNotificationsBadge', async () => {
const anyTimestamp = new Date().getTime();
createFakeWebKitPostMessage({
checkMessage: (msg) => {
expect(msg.type).toBe('SET_UNSEEN_NOTIFICATIONS_BADGE');
expect(msg.payload).toEqual({
unseenNotificationCounter: 3,
lastUpdated: anyTimestamp,
});
},
getResponse: (msg) => ({
type: 'SET_UNSEEN_NOTIFICATIONS_BADGE',
id: msg.id,
}),
});

await setUnseenNotificationsBadge({
unseenNotificationCounter: 3,
lastUpdated: anyTimestamp,
});
});
12 changes: 12 additions & 0 deletions src/inbox-notifications.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {postMessageToNativeApp} from './post-message';

export const getUnseenNotificationsBadge = async (): Promise<{
unseenNotificationCounter: number;
lastUpdated: number;
}> => postMessageToNativeApp({type: 'GET_UNSEEN_NOTIFICATIONS_BADGE'});

export const setUnseenNotificationsBadge = async (payload: {
unseenNotificationCounter: number;
lastUpdated: number;
}): Promise<void> =>
postMessageToNativeApp({type: 'SET_UNSEEN_NOTIFICATIONS_BADGE', payload});
10 changes: 10 additions & 0 deletions src/post-message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,16 @@ export type ResponsesFromNativeApp = {
id: string;
payload: {installationId: string};
};
GET_UNSEEN_NOTIFICATIONS_BADGE: {
type: 'GET_UNSEEN_NOTIFICATIONS_BADGE';
id: string;
payload: {unseenNotificationCounter: number; lastUpdated: number};
};
SET_UNSEEN_NOTIFICATIONS_BADGE: {
type: 'SET_UNSEEN_NOTIFICATIONS_BADGE';
id: string;
payload: void;
};
};

export type NativeAppResponsePayload<
Expand Down

0 comments on commit ebd5d88

Please sign in to comment.