diff --git a/app/init/push_notifications.test.js b/app/init/push_notifications.test.js index 34c68b5dfd..7ef3245ae1 100644 --- a/app/init/push_notifications.test.js +++ b/app/init/push_notifications.test.js @@ -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); + }); }); diff --git a/app/init/push_notifications.ts b/app/init/push_notifications.ts index 57a971128b..0d8fa27b56 100644 --- a/app/init/push_notifications.ts +++ b/app/init/push_notifications.ts @@ -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); + } } };