Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix: propagate error when user denies permission
  • Loading branch information
mvayngrib committed Apr 4, 2019
commit 8066938e8ca3d9960ccd46ac09228ca685b79ecc
41 changes: 30 additions & 11 deletions RNNotifications/RNNotifications.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
NSString* const RNNotificationActionReceived = @"notificationActionReceived";
//NSString* const RNNotificationActionDismissed = @"RNNotificationActionDismissed";

NSString* const RNNErrorDomain = @"RNNNotificationsError";
static const int RNNErrorCode = -1;

////////////////////////////////////////////////////////////////
#pragma mark conversions
Expand Down Expand Up @@ -189,25 +191,36 @@ - (void)setBridge:(RCTBridge *)bridge
#pragma mark private functions
////////////////////////////////////////////////////////////////

+ (void)requestPermissionsWithCategories:(NSMutableSet *)categories
- (void)requestPermissionsWithCategoriesInternal:(NSMutableSet *)categories
{
dispatch_async(dispatch_get_main_queue(), ^{
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error){
if(!error)
{
if (granted)
{
dispatch_async(dispatch_get_main_queue(), ^{
[[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:categories];
[[UIApplication sharedApplication] registerForRemoteNotifications];
});
}
if (error) {
[self sendFailedToRegisterEvent:error];
return;
}

if (!granted) {
[self sendFailedToRegisterEvent:[RNNotifications errorWithMessage:@"UserDenied"]];
return;
}

dispatch_async(dispatch_get_main_queue(), ^{
[[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:categories];
[[UIApplication sharedApplication] registerForRemoteNotifications];
});
}];
});
}

+ (NSError *)errorWithMessage:(NSString *)errorMessage
{
return [NSError errorWithDomain:RNNErrorDomain
code:RNNErrorCode
userInfo:@{ NSLocalizedDescriptionKey: NSLocalizedString(errorMessage, nil) }];
}

////////////////////////////////////////////////////////////////
#pragma mark NRNNManagerDelegate
////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -295,11 +308,17 @@ - (void)application:(UIApplication *)application didRegisterForRemoteNotificatio
}

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
[self sendFailedToRegisterEvent:error];
}

- (void)sendFailedToRegisterEvent:(NSError *)error
{
[self checkAndSendEvent:RNNotificationsRegistrationFailed
body:@{@"code": [NSNumber numberWithInteger:error.code], @"domain": error.domain, @"localizedDescription": error.localizedDescription}];
}


//the system calls this method when your app is running in the foreground or background
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
Expand Down Expand Up @@ -384,7 +403,7 @@ - (void)startObserving
[categories addObject:[RCTConvert UNNotificationCategory:dic]];
}
}
[RNNotifications requestPermissionsWithCategories:categories];
[self requestPermissionsWithCategoriesInternal:categories];
}

RCT_EXPORT_METHOD(localNotification:(NSDictionary *)notification withId:(NSString *)notificationId)
Expand Down