forked from Ivasoft/mattermost-mobile
[MM-31216] iOS - Only set badge to 0 if there are no delivered notifications or it is forced (#5015)
* Only set badge to 0 if there are no delivered notifications * Missing semicolon
This commit is contained in:
@@ -58,12 +58,12 @@ describe('PushNotification', () => {
|
||||
});
|
||||
});
|
||||
|
||||
it('should clear all notifications', () => {
|
||||
it('should clear all notifications', async () => {
|
||||
const setApplicationIconBadgeNumber = jest.spyOn(PushNotification, 'setApplicationIconBadgeNumber');
|
||||
const cancelAllLocalNotifications = jest.spyOn(PushNotification, 'cancelAllLocalNotifications');
|
||||
|
||||
PushNotification.clearNotifications();
|
||||
expect(setApplicationIconBadgeNumber).toHaveBeenCalledWith(0);
|
||||
await expect(setApplicationIconBadgeNumber).toHaveBeenCalledWith(0, true);
|
||||
expect(Notifications.ios.setBadgeCount).toHaveBeenCalledWith(0);
|
||||
expect(cancelAllLocalNotifications).toHaveBeenCalled();
|
||||
expect(Notifications.cancelAllLocalNotifications).toHaveBeenCalled();
|
||||
@@ -93,4 +93,18 @@ describe('PushNotification', () => {
|
||||
await PushNotification.clearChannelNotifications();
|
||||
expect(setApplicationIconBadgeNumber).toHaveBeenCalledWith(stateBadgeCount);
|
||||
});
|
||||
|
||||
test('setApplicationIconBadgeNumber should only set badge to 0 if there are no delivered notifications', async () => {
|
||||
const setBadgeCount = jest.spyOn(Notifications.ios, 'setBadgeCount');
|
||||
|
||||
let deliveredNotifications = [{identifier: 1}];
|
||||
Notifications.setDeliveredNotifications(deliveredNotifications);
|
||||
await PushNotification.setApplicationIconBadgeNumber(0);
|
||||
expect(setBadgeCount).not.toHaveBeenCalled();
|
||||
|
||||
deliveredNotifications = [];
|
||||
Notifications.setDeliveredNotifications(deliveredNotifications);
|
||||
await PushNotification.setApplicationIconBadgeNumber(0);
|
||||
expect(setBadgeCount).toHaveBeenCalledWith(0);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -65,7 +65,7 @@ class PushNotifications {
|
||||
}
|
||||
|
||||
clearNotifications = () => {
|
||||
this.setApplicationIconBadgeNumber(0);
|
||||
this.setApplicationIconBadgeNumber(0, true);
|
||||
|
||||
// TODO: Only cancel the local notifications that belong to this server
|
||||
this.cancelAllLocalNotifications();
|
||||
@@ -233,10 +233,13 @@ class PushNotifications {
|
||||
}
|
||||
};
|
||||
|
||||
setApplicationIconBadgeNumber = (value: number) => {
|
||||
setApplicationIconBadgeNumber = async (value: number, force = false) => {
|
||||
if (Platform.OS === 'ios') {
|
||||
const count = value < 0 ? 0 : value;
|
||||
Notifications.ios.setBadgeCount(count);
|
||||
const notifications = await Notifications.ios.getDeliveredNotifications();
|
||||
if (count === 0 && (force || notifications.length === 0)) {
|
||||
Notifications.ios.setBadgeCount(count);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user