diff --git a/RNNotifications/RNNotifications.m b/RNNotifications/RNNotifications.m index 2162283c3c559ce6841e7d22740b7b4ed40e8700..838fb0e62d40e0e27b2a92a6c175e2bbc56cdee0 100644 --- a/RNNotifications/RNNotifications.m +++ b/RNNotifications/RNNotifications.m @@ -545,6 +545,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 8675dd3d37f00821e0e405e2289a67e66056f8b8..9e44bde671158c2b25eea0a66aa3a593d7d2cdca 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 7a167c90f95810faae06d398f128f5d557c6d63d..2108667328a7b8114e30e84a4b0b46dbdc2434bf 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 c7ccc17b915764d2957bdc4858a53053f9d87f07..295a3b672ac841c18484413f61e8e02966eefd26 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);