From bad7b85d40c7bea209b05297a29a42d9f64457e0 Mon Sep 17 00:00:00 2001 From: Reinaldo Neto <47038980+reinaldonetof@users.noreply.github.com> Date: Wed, 21 Dec 2022 13:07:17 -0300 Subject: [PATCH] [FIX] Disappear push notifications when open the app (#4718) * [FIX] Disappear push notifications when open the app * minor tweak * test badgeCount * refactor remove notification to remove notification and badge --- app/lib/notifications/index.ts | 6 +++++- app/lib/notifications/push.ts | 4 ++++ app/lib/store/appStateMiddleware.ts | 2 ++ app/sagas/state.js | 2 -- 4 files changed, 11 insertions(+), 3 deletions(-) diff --git a/app/lib/notifications/index.ts b/app/lib/notifications/index.ts index 3d4888987..167add557 100644 --- a/app/lib/notifications/index.ts +++ b/app/lib/notifications/index.ts @@ -3,7 +3,7 @@ import EJSON from 'ejson'; import { store } from '../store/auxStore'; import { deepLinkingOpen } from '../../actions/deepLinking'; import { isFDroidBuild } from '../constants'; -import { deviceToken, pushNotificationConfigure, setNotificationsBadgeCount } from './push'; +import { deviceToken, pushNotificationConfigure, setNotificationsBadgeCount, removeAllNotifications } from './push'; import { INotification, SubscriptionType } from '../../definitions'; interface IEjson { @@ -49,6 +49,10 @@ export const onNotification = (push: INotification): void => { export const getDeviceToken = (): string => deviceToken; export const setBadgeCount = (count?: number): void => setNotificationsBadgeCount(count); +export const removeNotificationsAndBadge = () => { + removeAllNotifications(); + setBadgeCount(); +}; export const initializePushNotifications = (): Promise | undefined => { if (!isFDroidBuild) { setBadgeCount(); diff --git a/app/lib/notifications/push.ts b/app/lib/notifications/push.ts index 4da3c1a62..4515da941 100644 --- a/app/lib/notifications/push.ts +++ b/app/lib/notifications/push.ts @@ -21,6 +21,10 @@ export const setNotificationsBadgeCount = (count = 0): void => { } }; +export const removeAllNotifications = (): void => { + Notifications.removeAllDeliveredNotifications(); +}; + export const pushNotificationConfigure = (onNotification: (notification: INotification) => void): Promise => { if (isIOS) { // init diff --git a/app/lib/store/appStateMiddleware.ts b/app/lib/store/appStateMiddleware.ts index 7025a8191..61da57a6c 100644 --- a/app/lib/store/appStateMiddleware.ts +++ b/app/lib/store/appStateMiddleware.ts @@ -1,6 +1,7 @@ // https://github.com/bamlab/redux-enhancer-react-native-appstate import { AppState } from 'react-native'; +import { removeNotificationsAndBadge } from '../notifications'; import { APP_STATE } from '../../actions/actionsTypes'; export default () => @@ -16,6 +17,7 @@ export default () => let type; if (nextAppState === 'active') { type = APP_STATE.FOREGROUND; + removeNotificationsAndBadge(); } else if (nextAppState === 'background') { type = APP_STATE.BACKGROUND; } diff --git a/app/sagas/state.js b/app/sagas/state.js index b29b2645a..1e6bcddab 100644 --- a/app/sagas/state.js +++ b/app/sagas/state.js @@ -5,7 +5,6 @@ import { localAuthenticate, saveLastLocalAuthenticationSession } from '../lib/me import { APP_STATE } from '../actions/actionsTypes'; import { RootEnum } from '../definitions'; import { Services } from '../lib/services'; -import { setBadgeCount } from '../lib/notifications'; const appHasComeBackToForeground = function* appHasComeBackToForeground() { const appRoot = yield select(state => state.app.root); @@ -20,7 +19,6 @@ const appHasComeBackToForeground = function* appHasComeBackToForeground() { try { yield localAuthenticate(server.server); Services.checkAndReopen(); - setBadgeCount(); return yield Services.setUserPresenceOnline(); } catch (e) { log(e);