-
Notifications
You must be signed in to change notification settings - Fork 24.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PushNotificationIOS requestPermission promisified #7900
Changes from 5 commits
b8180c9
1e2f592
6365175
ef8c066
8344774
afed56b
dbabb78
45c9e15
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,13 @@ | |
NSString *const RCTLocalNotificationReceived = @"LocalNotificationReceived"; | ||
NSString *const RCTRemoteNotificationReceived = @"RemoteNotificationReceived"; | ||
NSString *const RCTRemoteNotificationsRegistered = @"RemoteNotificationsRegistered"; | ||
NSString *const RCTRegisterUserNotificationSettings = @"RegisterUserNotificationSettings"; | ||
|
||
NSString *const RCTErrorUnableToRequestPermissions = @"E_UNABLE_TO_REQUEST_PERMISSIONS"; | ||
|
||
@interface RCTPushNotificationManager () | ||
@property (nonatomic, copy) RCTPromiseResolveBlock requestPermissionsResolveBlock; | ||
@end | ||
|
||
@implementation RCTConvert (UILocalNotification) | ||
|
||
|
@@ -66,6 +73,10 @@ - (void)startObserving | |
selector:@selector(handleRemoteNotificationsRegistered:) | ||
name:RCTRemoteNotificationsRegistered | ||
object:nil]; | ||
[[NSNotificationCenter defaultCenter] addObserver:self | ||
selector:@selector(handleRegisterUserNotificationSettings:) | ||
name:RCTRegisterUserNotificationSettings | ||
object:nil]; | ||
} | ||
|
||
- (void)stopObserving | ||
|
@@ -93,6 +104,9 @@ + (void)didRegisterUserNotificationSettings:(__unused UIUserNotificationSettings | |
{ | ||
if ([UIApplication instancesRespondToSelector:@selector(registerForRemoteNotifications)]) { | ||
[[UIApplication sharedApplication] registerForRemoteNotifications]; | ||
[[NSNotificationCenter defaultCenter] postNotificationName:RCTRegisterUserNotificationSettings | ||
object:self | ||
userInfo:notificationSettings]; | ||
} | ||
} | ||
|
||
|
@@ -145,6 +159,22 @@ - (void)handleRemoteNotificationsRegistered:(NSNotification *)notification | |
[self sendEventWithName:@"remoteNotificationsRegistered" body:notification.userInfo]; | ||
} | ||
|
||
- (void)handleRegisterUserNotificationSettings:(NSNotification *)notification | ||
{ | ||
if (self.requestPermissionsResolveBlock == nil) { | ||
return; | ||
} | ||
UIUserNotificationSettings *notificationSettings = notification.userInfo; | ||
NSDictionary *notificationTypes = @{ | ||
@"alert": @((BOOL)notificationSettings.types & UIUserNotificationTypeAlert), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this won't actually work, for the reason I mentioned - casting to BOOL is basically just casting to char/int8, it won't actually coerce the value to be true or false There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this needs to be written as
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Gotcha, alright I played with it on my device and the only change I made was wrapping the bitwise-and in parenthesis before comparing it. Otherwise it works fine 👍 |
||
@"sound": @((BOOL)notificationSettings.types & UIUserNotificationTypeSound), | ||
@"badge": @((BOOL)notificationSettings.types & UIUserNotificationTypeBadge), | ||
}; | ||
|
||
self.requestPermissionsResolveBlock(notificationTypes); | ||
self.requestPermissionsResolveBlock = nil; | ||
} | ||
|
||
/** | ||
* Update the application icon badge number on the home screen | ||
*/ | ||
|
@@ -161,12 +191,22 @@ - (void)handleRemoteNotificationsRegistered:(NSNotification *)notification | |
callback(@[@(RCTSharedApplication().applicationIconBadgeNumber)]); | ||
} | ||
|
||
RCT_EXPORT_METHOD(requestPermissions:(NSDictionary *)permissions) | ||
RCT_EXPORT_METHOD(requestPermissions:(NSDictionary *)permissions | ||
resolver:(RCTPromiseResolveBlock)resolve | ||
rejecter:(RCTPromiseRejectBlock)reject) | ||
{ | ||
if (RCTRunningInAppExtension()) { | ||
reject(RCTErrorUnableToRequestPermissions, nil, RCTErrorWithMessage(@"Requesting push notifications is currently unavailable in an app extension")); | ||
return; | ||
} | ||
|
||
if (self.requestPermissionsResolveBlock != nil) { | ||
RCTLogError(@"Cannot call requestPermissions twice before the first has returned."); | ||
return; | ||
} | ||
|
||
self.requestPermissionsResolveBlock = resolve; | ||
|
||
UIUserNotificationType types = UIUserNotificationTypeNone; | ||
if (permissions) { | ||
if ([RCTConvert BOOL:permissions[@"alert"]]) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JAStanton This failed to land due to a type error detected by our build system. You're assigning notification.userInfo (which is defined as being an NSDictionary) to a property of type UIUserNotificationSettings. I'm not sure why this worked for you locally, but it seems unlikely that it's correct.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh nice, so it works because I was passing in
UIUserNotificationSettings
to theuserInfo
parameter of my NSNotification message instead of setting it a dictionary like how it's supposed to be used. Bummer. Updated, take a look! Thanks.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JAStanton ah, I totally missed that. Should just be a simple case of wrapping notificationSettings in a dictionary and then unwrapping it then: