Skip to content

Commit

Permalink
Merge pull request Expensify#32180 from fabioh8010/ts/lib/PushNotific…
Browse files Browse the repository at this point in the history
…ation

[TS migration] Migrate 'PushNotification' lib to TypeScript
  • Loading branch information
Julesssss authored Dec 6, 2023
2 parents efcd932 + 9976663 commit e1fcb42
Show file tree
Hide file tree
Showing 18 changed files with 188 additions and 103 deletions.
2 changes: 1 addition & 1 deletion __mocks__/@ua/react-native-airship.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const Airship = {
},
contact: {
identify: jest.fn(),
getNamedUserId: jest.fn(),
getNamedUserId: () => Promise.resolve(undefined),
reset: jest.fn(),
},
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Airship from '@ua/react-native-airship';
import shouldShowPushNotification from '@libs/Notification/PushNotification/shouldShowPushNotification';
import ForegroundNotificationsModule from './types';

function configureForegroundNotifications() {
Airship.push.android.setForegroundDisplayPredicate((pushPayload) => Promise.resolve(shouldShowPushNotification(pushPayload)));
Expand All @@ -9,7 +10,9 @@ function disableForegroundNotifications() {
Airship.push.android.setForegroundDisplayPredicate(() => Promise.resolve(false));
}

export default {
const ForegroundNotifications: ForegroundNotificationsModule = {
configureForegroundNotifications,
disableForegroundNotifications,
};

export default ForegroundNotifications;
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Airship, {iOS} from '@ua/react-native-airship';
import shouldShowPushNotification from '@libs/Notification/PushNotification/shouldShowPushNotification';
import ForegroundNotificationsModule from './types';

function configureForegroundNotifications() {
// Set our default iOS foreground presentation to be loud with a banner
Expand All @@ -20,7 +21,9 @@ function disableForegroundNotifications() {
Airship.push.iOS.setForegroundPresentationOptionsCallback(() => Promise.resolve([]));
}

export default {
const ForegroundNotifications: ForegroundNotificationsModule = {
configureForegroundNotifications,
disableForegroundNotifications,
};

export default ForegroundNotifications;
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import ForegroundNotificationsModule from './types';

/**
* Configures notification handling while in the foreground on iOS and Android. This is a no-op on other platforms.
*/
export default {
const ForegroundNotifications: ForegroundNotificationsModule = {
configureForegroundNotifications: () => {},
disableForegroundNotifications: () => {},
};

export default ForegroundNotifications;
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type ForegroundNotificationsModule = {
configureForegroundNotifications: () => void;
disableForegroundNotifications: () => void;
};

export default ForegroundNotificationsModule;
7 changes: 0 additions & 7 deletions src/libs/Notification/PushNotification/NotificationType.js

This file was deleted.

28 changes: 28 additions & 0 deletions src/libs/Notification/PushNotification/NotificationType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import {OnyxServerUpdate} from '@src/types/onyx/OnyxUpdatesFromServer';

const NotificationType = {
REPORT_COMMENT: 'reportComment',
} as const;

type NotificationDataMap = {
[NotificationType.REPORT_COMMENT]: ReportCommentNotificationData;
};

type NotificationData = ReportCommentNotificationData;

type ReportCommentNotificationData = {
title: string;
type: typeof NotificationType.REPORT_COMMENT;
reportID: number;
reportActionID: string;
shouldScrollToLastUnread?: boolean;
roomName?: string;
onyxData?: OnyxServerUpdate[];
};

/**
* See https://github.com/Expensify/Web-Expensify/blob/main/lib/MobilePushNotifications.php for the various
* types of push notifications sent by our API.
*/
export default NotificationType;
export type {NotificationDataMap, NotificationData, ReportCommentNotificationData};
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import Visibility from '@libs/Visibility';
import * as App from '@userActions/App';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import BackgroundRefresh from './types';

function getLastOnyxUpdateID() {
function getLastOnyxUpdateID(): Promise<number | null> {
return new Promise((resolve) => {
const connectionID = Onyx.connect({
key: ONYXKEYS.ONYX_UPDATES_LAST_UPDATE_ID_APPLIED_TO_CLIENT,
Expand All @@ -23,7 +24,7 @@ function getLastOnyxUpdateID() {
* We use this to refresh the app in the background after receiving a push notification (native only). Since the full app
* wakes on iOS and by extension runs reconnectApp already, this is a no-op on everything but Android.
*/
export default function backgroundRefresh() {
const backgroundRefresh: BackgroundRefresh = () => {
if (Visibility.isVisible()) {
return;
}
Expand All @@ -38,9 +39,11 @@ export default function backgroundRefresh() {
* See more here: https://reactnative.dev/docs/headless-js-android
*/
App.confirmReadyToOpenApp();
App.reconnectApp(lastUpdateIDAppliedToClient);
App.reconnectApp(lastUpdateIDAppliedToClient ?? undefined);
})
.catch((error) => {
Log.alert(`${CONST.ERROR.ENSURE_BUGBOT} [PushNotification] backgroundRefresh failed. This should never happen.`, {error});
});
}
};

export default backgroundRefresh;
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import BackgroundRefresh from './types';

/**
* Runs our reconnectApp action if the app is in the background.
*
* We use this to refresh the app in the background after receiving a push notification (native only). Since the full app
* wakes on iOS and by extension runs reconnectApp already, this is a no-op on everything but Android.
*/
export default function backgroundRefresh() {}
const backgroundRefresh: BackgroundRefresh = () => {};

export default backgroundRefresh;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
type BackgroundRefresh = () => void;

export default BackgroundRefresh;
Loading

0 comments on commit e1fcb42

Please sign in to comment.