From 1ece02816812aac22c3f049df69a2dd7f7d8c668 Mon Sep 17 00:00:00 2001 From: Ryan Eberhardt Date: Tue, 1 Aug 2017 16:53:36 -0700 Subject: [PATCH] Add iOS checkPermissions Add a checkPermissions function allowing the app to determine what notification permissions the user has granted. The function returns a promise resolving to an object with permission info. --- README.md | 10 ++++++++++ RNNotifications/RNNotifications.m | 10 ++++++++++ index.ios.js | 4 ++++ test/index.ios.spec.js | 11 ++++++++++- 4 files changed, 34 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 1c22283..d85e056 100644 --- a/README.md +++ b/README.md @@ -185,6 +185,16 @@ class App extends Component { When you have the device token, POST it to your server and register the device in your notifications provider (Amazon SNS, Azure, etc.). +You can check if the user granted permissions by calling `checkPermissions()`: + +```javascript +NotificationsIOS.checkPermissions().then((currentPermissions) => { + console.log('Badges enabled: ' + !!currentPermissions.badge); + console.log('Sounds enabled: ' + !!currentPermissions.sound); + console.log('Alerts enabled: ' + !!currentPermissions.alert); +}); +``` + ### Android The React-Native code equivalent on Android is: diff --git a/RNNotifications/RNNotifications.m b/RNNotifications/RNNotifications.m index e10edd1..0ce06e3 100644 --- a/RNNotifications/RNNotifications.m +++ b/RNNotifications/RNNotifications.m @@ -562,4 +562,14 @@ RCT_EXPORT_METHOD(isRegisteredForRemoteNotifications:(RCTPromiseResolveBlock)res resolve(@(ans)); } +RCT_EXPORT_METHOD(checkPermissions:(RCTPromiseResolveBlock) resolve + reject:(RCTPromiseRejectBlock) reject) { + UIUserNotificationSettings *currentSettings = [[UIApplication sharedApplication] currentUserNotificationSettings]; + resolve(@{ + @"badge": @((currentSettings.types & UIUserNotificationTypeBadge) > 0), + @"sound": @((currentSettings.types & UIUserNotificationTypeSound) > 0), + @"alert": @((currentSettings.types & UIUserNotificationTypeAlert) > 0), + }); +} + @end diff --git a/index.ios.js b/index.ios.js index f30931d..f189676 100644 --- a/index.ios.js +++ b/index.ios.js @@ -212,4 +212,8 @@ export default class NotificationsIOS { static isRegisteredForRemoteNotifications() { return NativeRNNotifications.isRegisteredForRemoteNotifications(); } + + static checkPermissions() { + return NativeRNNotifications.checkPermissions(); + } } diff --git a/test/index.ios.spec.js b/test/index.ios.spec.js index fd911bf..beff611 100644 --- a/test/index.ios.spec.js +++ b/test/index.ios.spec.js @@ -68,7 +68,8 @@ describe("NotificationsIOS", () => { cancelLocalNotification: nativeCancelLocalNotification, cancelAllLocalNotifications: nativeCancelAllLocalNotifications, setBadgesCount: nativeSetBadgesCount, - isRegisteredForRemoteNotifications: nativeIsRegisteredForRemoteNotifications + isRegisteredForRemoteNotifications: nativeIsRegisteredForRemoteNotifications, + checkPermissions: nativeCheckPermissions, } }, NativeAppEventEmitter: { @@ -309,4 +310,12 @@ describe("NotificationsIOS", () => { }); }); + + describe("Check permissions ", () => { + it("should call native check permissions", () => { + NotificationsIOS.checkPermissions(); + expect(nativeCheckPermissions).to.have.been.calledWith(); + + }); + }); }); -- 2.26.2