diff --git a/README.md b/README.md index 1c222836d18eda0d3223264598415d44dd8880b5..d85e05681883c4b34fc34641ee0ddf169767b1ec 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 e10edd11a914688b37dd32447f7db21d27737561..0ce06e32c3671a7536f58ae3ea2ee35081a248bc 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 f30931d79a3c20d914ef53aa9e537894ccff0245..f189676483d8a09477a88b5afaebb30201b77231 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 fd911bf2cca9c468cb7403200e2fe8faea832733..aa1f72009595ca49494f44990d4b36b1854f33a0 100644 --- a/test/index.ios.spec.js +++ b/test/index.ios.spec.js @@ -29,7 +29,8 @@ describe("NotificationsIOS", () => { nativeCancelLocalNotification, nativeCancelAllLocalNotifications, nativeSetBadgesCount, - nativeIsRegisteredForRemoteNotifications; + nativeIsRegisteredForRemoteNotifications, + nativeCheckPermissions; let NotificationsIOS, NotificationAction, NotificationCategory; let someHandler = () => {}; @@ -51,6 +52,7 @@ describe("NotificationsIOS", () => { nativeCancelAllLocalNotifications = sinon.spy(); nativeSetBadgesCount = sinon.spy(); nativeIsRegisteredForRemoteNotifications = sinon.spy(); + nativeCheckPermissions = sinon.spy(); let libUnderTest = proxyquire("../index.ios", { "uuid": { @@ -68,7 +70,8 @@ describe("NotificationsIOS", () => { cancelLocalNotification: nativeCancelLocalNotification, cancelAllLocalNotifications: nativeCancelAllLocalNotifications, setBadgesCount: nativeSetBadgesCount, - isRegisteredForRemoteNotifications: nativeIsRegisteredForRemoteNotifications + isRegisteredForRemoteNotifications: nativeIsRegisteredForRemoteNotifications, + checkPermissions: nativeCheckPermissions } }, NativeAppEventEmitter: { @@ -108,6 +111,7 @@ describe("NotificationsIOS", () => { nativeCancelLocalNotification.reset(); nativeCancelAllLocalNotifications.reset(); nativeIsRegisteredForRemoteNotifications.reset(); + nativeCheckPermissions.reset(); }); after(() => { @@ -124,6 +128,7 @@ describe("NotificationsIOS", () => { nativeCancelLocalNotification = null; nativeCancelAllLocalNotifications = null; nativeIsRegisteredForRemoteNotifications = null; + nativeCheckPermissions = null; NotificationsIOS = null; NotificationAction = null; @@ -309,4 +314,12 @@ describe("NotificationsIOS", () => { }); }); + + describe("Check permissions ", () => { + it("should call native check permissions", () => { + NotificationsIOS.checkPermissions(); + expect(nativeCheckPermissions).to.have.been.calledWith(); + + }); + }); });