diff --git a/README.md b/README.md index 60bb5171aa51466472c24d367d1ed62956843afe..620d5510bb7c1516c68df3ed0444318b66473aff 100644 --- a/README.md +++ b/README.md @@ -612,6 +612,16 @@ The [example app](https://github.com/wix/react-native-notifications/tree/master/ - `minimal` - Displays up tp 2 actions (minimal UI). +#### Set application icon badges count (iOS only) + +Set to specific number: +```javascript +NotificationsIOS.setBadgesCount(2); +``` +Clear badges icon: +```javascript +NotificationsIOS.setBadgesCount(0); +``` ## License The MIT License. diff --git a/RNNotifications/RNNotifications.m b/RNNotifications/RNNotifications.m index 8c7959d6b453b3c4b1d9c5bd422c013226ae179f..d9abdc7f29b0063da38e484256eef74531f74f3c 100644 --- a/RNNotifications/RNNotifications.m +++ b/RNNotifications/RNNotifications.m @@ -476,6 +476,11 @@ RCT_EXPORT_METHOD(registerPushKit) [RNNotifications registerPushKit]; } +RCT_EXPORT_METHOD(setBadgesCount:(int)count) +{ + [[UIApplication sharedApplication] setApplicationIconBadgeNumber:count]; +} + RCT_EXPORT_METHOD(backgroundTimeRemaining:(RCTResponseSenderBlock)callback) { NSTimeInterval remainingTime = [UIApplication sharedApplication].backgroundTimeRemaining; diff --git a/index.ios.js b/index.ios.js index 6dd788fd31723d7c892dbe9b0fef711e4064795f..cce7a58f3dd724b303b2064d5e354cffeeadc216 100644 --- a/index.ios.js +++ b/index.ios.js @@ -156,6 +156,10 @@ export default class NotificationsIOS { _actionHandlers.clear(); } + static setBadgesCount(count: number) { + NativeRNNotifications.setBadgesCount(count); + } + static registerPushKit() { NativeRNNotifications.registerPushKit(); } diff --git a/test/index.ios.spec.js b/test/index.ios.spec.js index 2ba23fb41131fd4c6d9ace387b2134fd4440633a..eb043d80c27bc9f05c5840f9c8ba1fb20b49888f 100644 --- a/test/index.ios.spec.js +++ b/test/index.ios.spec.js @@ -27,7 +27,9 @@ describe("NotificationsIOS", () => { nativeConsumeBackgroundQueue, nativeLocalNotification, nativeCancelLocalNotification, - nativeCancelAllLocalNotifications; + nativeCancelAllLocalNotifications, + nativeSetBadgesCount; + let NotificationsIOS, NotificationAction, NotificationCategory; let someHandler = () => {}; let constantGuid = "some-random-uuid"; @@ -46,6 +48,7 @@ describe("NotificationsIOS", () => { nativeLocalNotification = sinon.spy(); nativeCancelLocalNotification = sinon.spy(); nativeCancelAllLocalNotifications = sinon.spy(); + nativeSetBadgesCount = sinon.spy(); let libUnderTest = proxyquire("../index.ios", { "uuid": { @@ -61,7 +64,8 @@ describe("NotificationsIOS", () => { consumeBackgroundQueue: nativeConsumeBackgroundQueue, localNotification: nativeLocalNotification, cancelLocalNotification: nativeCancelLocalNotification, - cancelAllLocalNotifications: nativeCancelAllLocalNotifications + cancelAllLocalNotifications: nativeCancelAllLocalNotifications, + setBadgesCount: nativeSetBadgesCount } }, NativeAppEventEmitter: { @@ -208,6 +212,15 @@ describe("NotificationsIOS", () => { expect(nativeAppRemoveEventListener).to.have.been.calledOnce; }); }); + + describe("set badges count", () => { + it("should call native setBadgesCount", () => { + NotificationsIOS.setBadgesCount(44); + + expect(nativeSetBadgesCount).to.have.been.calledWith(44); + }); + }); + }); describe("register push kit for background notifications", function () {