Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WEB-2097 getUnseenNotificationsBadge/setUnseenNotificationsBadge methods #163

Merged
merged 3 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1339,6 +1339,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
Loading