[MM-14926] Clear foreground notifications from AsyncStorage on logout and server upgrade (#3033)
* Clear foreground notifications from AsyncStorage * Remove unnecessary call to clearNotifications
This commit is contained in:
parent
894ba58acc
commit
4ee4ff10dd
5 changed files with 63 additions and 4 deletions
|
|
@ -143,8 +143,7 @@ class GlobalEventHandler {
|
|||
deleteFileCache();
|
||||
removeAppCredentials();
|
||||
|
||||
PushNotifications.setApplicationIconBadgeNumber(0);
|
||||
PushNotifications.cancelAllLocalNotifications(); // TODO: Only cancel the notification that belongs to this server
|
||||
PushNotifications.clearNotifications();
|
||||
|
||||
if (this.launchApp) {
|
||||
this.launchApp();
|
||||
|
|
@ -225,8 +224,6 @@ class GlobalEventHandler {
|
|||
|
||||
dispatch(setServerVersion(''));
|
||||
Client4.serverVersion = '';
|
||||
PushNotifications.setApplicationIconBadgeNumber(0);
|
||||
PushNotifications.cancelAllLocalNotifications(); // TODO: Only cancel the notification that belongs to this server
|
||||
|
||||
const credentials = await getAppCredentials();
|
||||
|
||||
|
|
|
|||
28
app/init/global_event_handler.test.js
Normal file
28
app/init/global_event_handler.test.js
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
||||
// See LICENSE.txt for license information.
|
||||
|
||||
import configureMockStore from 'redux-mock-store';
|
||||
import thunk from 'redux-thunk';
|
||||
|
||||
import PushNotification from 'app/push_notifications';
|
||||
|
||||
import GlobalEventHandler from './global_event_handler';
|
||||
|
||||
jest.mock('app/init/credentials', () => ({
|
||||
getAppCredentials: jest.fn(),
|
||||
removeAppCredentials: jest.fn(),
|
||||
}));
|
||||
|
||||
const mockStore = configureMockStore([thunk]);
|
||||
const store = mockStore({});
|
||||
GlobalEventHandler.store = store;
|
||||
|
||||
// TODO: Add Android test as part of https://mattermost.atlassian.net/browse/MM-17110
|
||||
describe('GlobalEventHandler', () => {
|
||||
it('should clear notifications on logout', () => {
|
||||
const clearNotifications = jest.spyOn(PushNotification, 'clearNotifications');
|
||||
|
||||
GlobalEventHandler.onLogout();
|
||||
expect(clearNotifications).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
|
@ -113,6 +113,16 @@ class PushNotification {
|
|||
NotificationPreferences.removeDeliveredNotifications(notificationForChannel.identifier, channelId);
|
||||
}
|
||||
}
|
||||
|
||||
clearForegroundNotifications = () => {
|
||||
// TODO: Implement as part of https://mattermost.atlassian.net/browse/MM-17110
|
||||
};
|
||||
|
||||
clearNotifications = () => {
|
||||
this.setApplicationIconBadgeNumber(0);
|
||||
this.cancelAllLocalNotifications(); // TODO: Only cancel the local notifications that belong to this server
|
||||
this.clearForegroundNotifications(); // TODO: Only clear the foreground notifications that belong to this server
|
||||
}
|
||||
}
|
||||
|
||||
export default new PushNotification();
|
||||
|
|
|
|||
|
|
@ -210,6 +210,16 @@ class PushNotification {
|
|||
foregroundNotifications[channelId] += 1;
|
||||
await AsyncStorage.setItem(FOREGROUND_NOTIFICATIONS_KEY, JSON.stringify(foregroundNotifications));
|
||||
}
|
||||
|
||||
clearForegroundNotifications = () => {
|
||||
AsyncStorage.removeItem(FOREGROUND_NOTIFICATIONS_KEY);
|
||||
};
|
||||
|
||||
clearNotifications = () => {
|
||||
this.setApplicationIconBadgeNumber(0);
|
||||
this.cancelAllLocalNotifications(); // TODO: Only cancel the local notifications that belong to this server
|
||||
this.clearForegroundNotifications(); // TODO: Only clear the foreground notifications that belong to this server
|
||||
}
|
||||
}
|
||||
|
||||
export default new PushNotification();
|
||||
|
|
|
|||
|
|
@ -24,6 +24,7 @@ jest.mock('react-native-notifications', () => {
|
|||
removeDeliveredNotifications: jest.fn((ids) => {
|
||||
deliveredNotifications = deliveredNotifications.filter((n) => !ids.includes(n.identifier));
|
||||
}),
|
||||
cancelAllLocalNotifications: jest.fn(),
|
||||
NotificationAction: jest.fn(),
|
||||
NotificationCategory: jest.fn(),
|
||||
};
|
||||
|
|
@ -133,4 +134,17 @@ describe('PushNotification', () => {
|
|||
expect(setApplicationIconBadgeNumber).toHaveBeenCalledWith(badgeNumber);
|
||||
});
|
||||
});
|
||||
|
||||
it('should clear all notifications', () => {
|
||||
const setApplicationIconBadgeNumber = jest.spyOn(PushNotification, 'setApplicationIconBadgeNumber');
|
||||
const cancelAllLocalNotifications = jest.spyOn(PushNotification, 'cancelAllLocalNotifications');
|
||||
const clearForegroundNotifications = jest.spyOn(PushNotification, 'clearForegroundNotifications');
|
||||
|
||||
PushNotification.clearNotifications();
|
||||
expect(setApplicationIconBadgeNumber).toHaveBeenCalledWith(0);
|
||||
expect(NotificationsIOS.setBadgesCount).toHaveBeenCalledWith(0);
|
||||
expect(cancelAllLocalNotifications).toHaveBeenCalled();
|
||||
expect(NotificationsIOS.cancelAllLocalNotifications).toHaveBeenCalled();
|
||||
expect(clearForegroundNotifications).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
Loading…
Reference in a new issue