question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feature proposal : get permission status

See original GitHub issue

Hello,

this isn’t really about an issue, this is more an extra feature I needed and developed (as an Objective-C category 😃

Here is my need:

  • My application needs the user to accept several permissions (GPS, backgroundRefresh on iOS, Push Notifications on iOS, …).
  • If the user doesn’t accept all the permissions, then he cannot access application and is blocked by a specific screen.

In order to develop that, I need to be able to:

  • check a permission
  • activate a permission (using native popups…)

I’m using react-native-permissions to check / activate permissions.

The problem came when I installed react-native-fcm : both modules don’t activate Notifications the same way (because of UserNotifications new framework introduced in iOS 10+).

If you find this feature interesting, here its the code :

#import "RNFIRMessaging.h" 

@interface RNFIRMessaging (Permission)

@end

#import "RNFIRMessaging+Permission.h"

static NSString* LCDidAskForNotification = @"LCDidAskForNotification";

// authorization status (we use same status names as react-native-permissions)
static NSString* LCStatusUndetermined = @"undetermined";
static NSString* LCStatusDenied = @"denied";
static NSString* LCStatusAuthorized = @"authorized";

@import UserNotifications;

@implementation RNFIRMessaging (Permission)

// we use NSUserDefault to know if user already asked for permissions
// (same logic as react-native-permissions)
//
// This doesn't really need to be exposed to Javascript
// it could be called on native side in 'react-native-fcm' 'requestPermissions' method
RCT_EXPORT_METHOD(setPushNotificationAsked:(BOOL)asked)
{
  [[NSUserDefaults standardUserDefaults] setBool:asked forKey:LCDidAskForNotification];
  [[NSUserDefaults standardUserDefaults] synchronize];
}

//
// 
//
RCT_EXPORT_METHOD(checkPushNotificationAuthorization:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
{
  BOOL didAskForPermission = [[NSUserDefaults standardUserDefaults] boolForKey:LCDidAskForNotification];
  
  // we use different API for notifications odépending on iOS version
  if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_9_x_Max) {
    BOOL isEnabled = [[[UIApplication sharedApplication] currentUserNotificationSettings] types] != UIUserNotificationTypeNone;
    NSString *permissionStatus = LCStatusUndetermined;
    if (isEnabled) {
      permissionStatus = LCStatusAuthorized;
    } else if(didAskForPermission){
      permissionStatus = LCStatusDenied;
    }
    resolve(permissionStatus);
  } else {
    // iOS 10 or later
#if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
    [[UNUserNotificationCenter currentNotificationCenter] getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
      
      // translate API status to our own NSString
      NSString *permissionStatus = LCStatusUndetermined; // default
      if(settings.authorizationStatus == UNAuthorizationStatusAuthorized){
        permissionStatus = LCStatusAuthorized;
      } else if (didAskForPermission){
        permissionStatus = LCStatusDenied;
      }
      resolve(permissionStatus);
    }];
#endif
  }
}

@end

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
aatreyacommented, Mar 15, 2018

I agree that this feature would be useful. Storing a flag in AsyncStorage seems like an inelegant workaround vs. simply being able to check whether the permission is already enabled.

0reactions
evollucommented, Mar 15, 2018

simply being able to check whether the permission is already enabled. @aatreya react-native-push-notification has a method to check permission. PR is welcomed for porting it over

Read more comments on GitHub >

github_iconTop Results From Across the Web

Feature Proposal: Handling file permissions #16548 - GitHub
It's pointless if we have a security system and end up adding too many permissions because a single file (or library) wants a...
Read more >
Feature Proposal: Omnibus Configuration to Adjust ... - GitLab
To solve this problem, we propose adding functionality allowing Omnibus to be configured to adjust permissions on log folders and files to ...
Read more >
A Writer's Guide to Fair Use and Permissions + Sample ...
Permissions is all about seeking permission to quote or excerpt other people's copyrighted work within your own. Here's when you need to ...
Read more >
[New Feature Proposal] Doctype Variant: more user friendly ...
Filter field defines the SQL where condition which will be applied to the get_list call to this doctype, this is to be acted...
Read more >
ProposalCentral FAQs
I THINK I HAVE MULTIPLE ACCOUNTS ON PROPOSALCENTRAL. ... I WAS NOTIFIED THAT MY PROPOSAL STATUS IS NOW PRE-AWARD. ... You do not...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found