From 2aaaad99c3f7aa0f60f426b52b0a643f20f4ba10 Mon Sep 17 00:00:00 2001 From: Lucas Gladding Date: Thu, 15 Feb 2018 22:16:35 -0500 Subject: [PATCH] Added method for getting the current badge count. --- RNNotifications/RNNotifications.m | 6 ++++++ docs/advancedIos.md | 7 ++++++- index.ios.js | 4 ++++ test/index.ios.spec.js | 12 ++++++++++++ 4 files changed, 28 insertions(+), 1 deletion(-) diff --git a/RNNotifications/RNNotifications.m b/RNNotifications/RNNotifications.m index dd281e6..3464d22 100644 --- a/RNNotifications/RNNotifications.m +++ b/RNNotifications/RNNotifications.m @@ -533,6 +533,12 @@ RCT_EXPORT_METHOD(registerPushKit) [RNNotifications registerPushKit]; } +RCT_EXPORT_METHOD(getBadgesCount:(RCTResponseSenderBlock)callback) +{ + NSInteger count = [UIApplication sharedApplication].applicationIconBadgeNumber; + callback(@[ [NSNumber numberWithInteger:count] ]); +} + RCT_EXPORT_METHOD(setBadgesCount:(int)count) { [[UIApplication sharedApplication] setApplicationIconBadgeNumber:count]; diff --git a/docs/advancedIos.md b/docs/advancedIos.md index 8675dd3..9e44bde 100644 --- a/docs/advancedIos.md +++ b/docs/advancedIos.md @@ -260,7 +260,12 @@ 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) +#### Get and set application icon badges count (iOS only) + +Get the current number: +```javascript +NotificationsIOS.getBadgesCount((count) => console.log(count)); +``` Set to specific number: ```javascript diff --git a/index.ios.js b/index.ios.js index 8c295a4..623d7cb 100644 --- a/index.ios.js +++ b/index.ios.js @@ -160,6 +160,10 @@ export default class NotificationsIOS { _actionHandlers.clear(); } + static getBadgesCount(callback: Function) { + NativeRNNotifications.getBadgesCount(callback); + } + static setBadgesCount(count: number) { NativeRNNotifications.setBadgesCount(count); } diff --git a/test/index.ios.spec.js b/test/index.ios.spec.js index c7ccc17..295a3b6 100644 --- a/test/index.ios.spec.js +++ b/test/index.ios.spec.js @@ -28,6 +28,7 @@ describe("NotificationsIOS", () => { nativeLocalNotification, nativeCancelLocalNotification, nativeCancelAllLocalNotifications, + nativeGetBadgesCount, nativeSetBadgesCount, nativeIsRegisteredForRemoteNotifications, nativeCheckPermissions, @@ -54,6 +55,7 @@ describe("NotificationsIOS", () => { nativeLocalNotification = sinon.spy(); nativeCancelLocalNotification = sinon.spy(); nativeCancelAllLocalNotifications = sinon.spy(); + nativeGetBadgesCount = sinon.spy(); nativeSetBadgesCount = sinon.spy(); nativeIsRegisteredForRemoteNotifications = sinon.spy(); nativeCheckPermissions = sinon.spy(); @@ -76,6 +78,7 @@ describe("NotificationsIOS", () => { localNotification: nativeLocalNotification, cancelLocalNotification: nativeCancelLocalNotification, cancelAllLocalNotifications: nativeCancelAllLocalNotifications, + getBadgesCount: nativeGetBadgesCount, setBadgesCount: nativeSetBadgesCount, isRegisteredForRemoteNotifications: nativeIsRegisteredForRemoteNotifications, checkPermissions: nativeCheckPermissions, @@ -239,6 +242,15 @@ describe("NotificationsIOS", () => { }); }); + describe("get badges count", () => { + it("should call native getBadgesCount", () => { + const callback = (count) => console.log(count); + NotificationsIOS.getBadgesCount(callback); + + expect(nativeGetBadgesCount).to.have.been.calledWith(callback); + }); + }); + describe("set badges count", () => { it("should call native setBadgesCount", () => { NotificationsIOS.setBadgesCount(44); -- 2.26.2