[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:
Miguel Alatzar
2020-12-10 12:29:47 -07:00
committed by GitHub
parent 6e2936e2e9
commit 77bc6257ac
2 changed files with 22 additions and 5 deletions

View File

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

View File

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