[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:
Miguel Alatzar 2019-07-26 03:21:11 -07:00 committed by Saturnino Abril
parent 894ba58acc
commit 4ee4ff10dd
5 changed files with 63 additions and 4 deletions

View file

@ -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();

View 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();
});
});

View file

@ -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();

View file

@ -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();

View file

@ -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();
});
});