From 9856f121ca91f5ad0b2bd40d0af49c80a24a9635 Mon Sep 17 00:00:00 2001 From: Omri Bruchim Date: Mon, 24 Apr 2017 15:34:22 +0300 Subject: [PATCH] Add setBadgesCount function (for iOS) --- README.md | 10 ++++++++++ RNNotifications/RNNotifications.m | 5 +++++ index.ios.js | 4 ++++ test/index.ios.spec.js | 17 +++++++++++++++-- 4 files changed, 34 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 60bb517..620d551 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 8c7959d..d9abdc7 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 6dd788f..cce7a58 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 2ba23fb..eb043d8 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 () { -- 2.26.2