mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-04-24 04:16:00 +08:00
registration error event
Summary: This is an updated version of #2336 and #7694. --- This adds a `registrationError` event that is emitted by `PushNotificationIOS` whenever an application receives a registration error from APNS (APNS service failure, running on simulator, etc). This event fires to the exclusion of the `register` event (and vice versa). **How to use** Add the following to your `AppDelegate.m`: ```obj-c - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error { [RCTPushNotificationManager didFailToRegisterForRemoteNotificationsWithError:error]; } ``` And register an event handler for the event: ```js PushNotificationIOS.addEventListener('registrationError', function({ message, code }) { // Complete your registration process in error. }); ``` **Test plan** Added support for this event (and `register`) to UIExplorer as a proof of concept. Navigating to the push notifications example on a simulator is an easy way to reproduce this e Closes https://github.com/facebook/react-native/pull/9650 Differential Revision: D3822142 Pulled By: javache fbshipit-source-id: a15ed8941b74dc3eed2c44c658deccbcaf39ce3d
This commit is contained in:
committed by
Facebook Github Bot
parent
dea6b0e24c
commit
4f89fa9cf3
@@ -20,6 +20,7 @@ NSString *const RCTRemoteNotificationsRegistered = @"RemoteNotificationsRegister
|
||||
NSString *const RCTRegisterUserNotificationSettings = @"RegisterUserNotificationSettings";
|
||||
|
||||
NSString *const RCTErrorUnableToRequestPermissions = @"E_UNABLE_TO_REQUEST_PERMISSIONS";
|
||||
NSString *const RCTErrorRemoteNotificationRegistrationFailed = @"E_FAILED_TO_REGISTER_FOR_REMOTE_NOTIFICATIONS";
|
||||
|
||||
@implementation RCTConvert (UILocalNotification)
|
||||
|
||||
@@ -86,6 +87,10 @@ RCT_EXPORT_MODULE()
|
||||
selector:@selector(handleRemoteNotificationsRegistered:)
|
||||
name:RCTRemoteNotificationsRegistered
|
||||
object:nil];
|
||||
[[NSNotificationCenter defaultCenter] addObserver:self
|
||||
selector:@selector(handleRemoteNotificationRegistrationError:)
|
||||
name:RCTErrorRemoteNotificationRegistrationFailed
|
||||
object:nil];
|
||||
[[NSNotificationCenter defaultCenter] addObserver:self
|
||||
selector:@selector(handleRegisterUserNotificationSettings:)
|
||||
name:RCTRegisterUserNotificationSettings
|
||||
@@ -101,7 +106,8 @@ RCT_EXPORT_MODULE()
|
||||
{
|
||||
return @[@"localNotificationReceived",
|
||||
@"remoteNotificationReceived",
|
||||
@"remoteNotificationsRegistered"];
|
||||
@"remoteNotificationsRegistered",
|
||||
@"remoteNotificationRegistrationError"];
|
||||
}
|
||||
|
||||
+ (void)didRegisterUserNotificationSettings:(__unused UIUserNotificationSettings *)notificationSettings
|
||||
@@ -127,6 +133,13 @@ RCT_EXPORT_MODULE()
|
||||
userInfo:@{@"deviceToken" : [hexString copy]}];
|
||||
}
|
||||
|
||||
+ (void)didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
|
||||
{
|
||||
[[NSNotificationCenter defaultCenter] postNotificationName:RCTErrorRemoteNotificationRegistrationFailed
|
||||
object:self
|
||||
userInfo:@{@"error": error}];
|
||||
}
|
||||
|
||||
+ (void)didReceiveRemoteNotification:(NSDictionary *)notification
|
||||
{
|
||||
[[NSNotificationCenter defaultCenter] postNotificationName:RCTRemoteNotificationReceived
|
||||
@@ -158,6 +171,17 @@ RCT_EXPORT_MODULE()
|
||||
[self sendEventWithName:@"remoteNotificationsRegistered" body:notification.userInfo];
|
||||
}
|
||||
|
||||
- (void)handleRemoteNotificationRegistrationError:(NSNotification *)notification
|
||||
{
|
||||
NSError *error = notification.userInfo[@"error"];
|
||||
NSDictionary *errorDetails = @{
|
||||
@"message": error.localizedDescription,
|
||||
@"code": @(error.code),
|
||||
@"details": error.userInfo,
|
||||
};
|
||||
[self sendEventWithName:@"remoteNotificationRegistrationError" body:errorDetails];
|
||||
}
|
||||
|
||||
- (void)handleRegisterUserNotificationSettings:(NSNotification *)notification
|
||||
{
|
||||
if (_requestPermissionsResolveBlock == nil) {
|
||||
|
||||
Reference in New Issue
Block a user