2
0
mirror of synced 2025-02-23 03:28:25 +00:00

[fcm] iOS 8/9 grant permission feedback

This commit is contained in:
Chris Bianca 2018-02-05 17:45:08 +00:00
parent 121a4d4b91
commit 93805062f7
2 changed files with 53 additions and 12 deletions

View File

@ -11,9 +11,13 @@
+ (_Nonnull instancetype)instance; + (_Nonnull instancetype)instance;
@property _Nullable RCTPromiseRejectBlock permissionRejecter;
@property _Nullable RCTPromiseResolveBlock permissionResolver;
#if !TARGET_OS_TV #if !TARGET_OS_TV
- (void)didReceiveRemoteNotification:(nonnull NSDictionary *)userInfo; - (void)didReceiveRemoteNotification:(nonnull NSDictionary *)userInfo;
- (void)didReceiveRemoteNotification:(nonnull NSDictionary *)userInfo fetchCompletionHandler:(void (^_Nonnull)(UIBackgroundFetchResult))completionHandler; - (void)didReceiveRemoteNotification:(nonnull NSDictionary *)userInfo fetchCompletionHandler:(void (^_Nonnull)(UIBackgroundFetchResult))completionHandler;
- (void)didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings;
#endif #endif
@end @end
@ -24,3 +28,4 @@
#endif #endif
#endif #endif

View File

@ -55,7 +55,10 @@ RCT_EXPORT_MODULE()
theRNFirebaseMessaging = self; theRNFirebaseMessaging = self;
} }
// ** AppDelegate methods ** // *******************************************************
// ** Start AppDelegate methods
// ** iOS 8/9 Only
// *******************************************************
// Listen for background messages // Listen for background messages
- (void)didReceiveRemoteNotification:(nonnull NSDictionary *)userInfo { - (void)didReceiveRemoteNotification:(nonnull NSDictionary *)userInfo {
@ -76,7 +79,29 @@ RCT_EXPORT_MODULE()
// TODO: FetchCompletionHandler? // TODO: FetchCompletionHandler?
} }
// ** UNUserNotificationCenterDelegate methods ** // Listen for permission response
- (void) didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
if (notificationSettings.types == UIUserNotificationTypeNone) {
if (_permissionRejecter) {
_permissionRejecter(@"messaging/permission_error", @"Failed to grant permission", nil);
}
} else if (_permissionResolver) {
_permissionResolver(nil);
}
_permissionRejecter = nil;
_permissionResolver = nil;
}
// *******************************************************
// ** Finish AppDelegate methods
// *******************************************************
// *******************************************************
// ** Start UNUserNotificationCenterDelegate methods
// ** iOS 10+
// *******************************************************
#if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0 #if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
// Handle incoming notification messages while app is in the foreground. // Handle incoming notification messages while app is in the foreground.
- (void)userNotificationCenter:(UNUserNotificationCenter *)center - (void)userNotificationCenter:(UNUserNotificationCenter *)center
@ -107,8 +132,16 @@ didReceiveNotificationResponse:(UNNotificationResponse *)response
} }
#endif #endif
// *******************************************************
// ** Finish UNUserNotificationCenterDelegate methods
// *******************************************************
// ** FIRMessagingDelegate methods **
// *******************************************************
// ** Start FIRMessagingDelegate methods
// ** iOS 8+
// *******************************************************
// Listen for FCM tokens // Listen for FCM tokens
- (void)messaging:(FIRMessaging *)messaging didReceiveRegistrationToken:(NSString *)fcmToken { - (void)messaging:(FIRMessaging *)messaging didReceiveRegistrationToken:(NSString *)fcmToken {
@ -122,6 +155,10 @@ didReceiveNotificationResponse:(UNNotificationResponse *)response
[RNFirebaseUtil sendJSEvent:self name:MESSAGING_MESSAGE_RECEIVED body:message]; [RNFirebaseUtil sendJSEvent:self name:MESSAGING_MESSAGE_RECEIVED body:message];
} }
// *******************************************************
// ** Finish FIRMessagingDelegate methods
// *******************************************************
// ** Start React Module methods ** // ** Start React Module methods **
RCT_EXPORT_METHOD(getToken:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) { RCT_EXPORT_METHOD(getToken:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
@ -130,26 +167,26 @@ RCT_EXPORT_METHOD(getToken:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseR
RCT_EXPORT_METHOD(requestPermission:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) { RCT_EXPORT_METHOD(requestPermission:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
if (RCTRunningInAppExtension()) { if (RCTRunningInAppExtension()) {
reject(@"request_permission_unavailable", @"requestPermission is not supported in App Extensions", nil); reject(@"messaging/request-permission-unavailable", @"requestPermission is not supported in App Extensions", nil);
return; return;
} }
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_9_x_Max) { if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_9_x_Max) {
UIUserNotificationType types = (UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge); UIUserNotificationType types = (UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge);
[RCTSharedApplication() registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:types categories:nil]]; [RCTSharedApplication() registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:types categories:nil]];
// Unfortunately on iOS 9 or below, there's no way to tell whether the user accepted or // We set the promise for usage by the AppDelegate callback which listens
// rejected the permissions popup // for the result of the permission request
// TODO: Is there something we can listen for? _permissionRejecter = reject;
resolve(@{@"status":@"unknown"}); _permissionResolver = resolve;
} else { } else {
#if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0 #if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
// For iOS 10 display notification (sent via APNS) // For iOS 10 display notification (sent via APNS)
UNAuthorizationOptions authOptions = UNAuthorizationOptionAlert | UNAuthorizationOptionSound | UNAuthorizationOptionBadge; UNAuthorizationOptions authOptions = UNAuthorizationOptionAlert | UNAuthorizationOptionSound | UNAuthorizationOptionBadge;
[[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:authOptions completionHandler:^(BOOL granted, NSError * _Nullable error) { [[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:authOptions completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (granted) { if (granted) {
resolve(@{@"status": @"granted"}); resolve(nil);
} else { } else {
reject(@"permission_error", @"Failed to grant permission", error); reject(@"messaging/permission_error", @"Failed to grant permission", error);
} }
}]; }];
#endif #endif
@ -175,7 +212,7 @@ RCT_EXPORT_METHOD(getInitialMessage:(RCTPromiseResolveBlock)resolve rejecter:(RC
resolve(nil); resolve(nil);
} }
} }
RCT_EXPORT_METHOD(sendMessage: (NSDictionary *) message RCT_EXPORT_METHOD(sendMessage: (NSDictionary *) message
resolve:(RCTPromiseResolveBlock) resolve resolve:(RCTPromiseResolveBlock) resolve
reject:(RCTPromiseRejectBlock) reject) { reject:(RCTPromiseRejectBlock) reject) {
@ -350,4 +387,3 @@ RCT_EXPORT_METHOD(unsubscribeFromTopic: (NSString*) topic) {
@implementation RNFirebaseMessaging @implementation RNFirebaseMessaging
@end @end
#endif #endif