Skip to content
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

[Push notifications] Add error event #7694

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
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
16 changes: 12 additions & 4 deletions Libraries/PushNotificationIOS/PushNotificationIOS.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var _initialNotification = RCTPushNotificationManager &&

var DEVICE_NOTIF_EVENT = 'remoteNotificationReceived';
var NOTIF_REGISTER_EVENT = 'remoteNotificationsRegistered';
var NOTIF_REGISTER_ERROR_EVENT = 'remoteNotificationsRegisteredError';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This name doesn’t really read nicely. What about a name like remoteNotificationsRegistrationError (and in all other places in the code) ?

var DEVICE_LOCAL_NOTIF_EVENT = 'localNotificationReceived';

/**
Expand Down Expand Up @@ -153,8 +154,8 @@ class PushNotificationIOS {
*/
static addEventListener(type: string, handler: Function) {
invariant(
type === 'notification' || type === 'register' || type === 'localNotification',
'PushNotificationIOS only supports `notification`, `register` and `localNotification` events'
type === 'notification' || type === 'register' || type === 'localNotification' || type === 'error',
'PushNotificationIOS only supports `notification`, `register`, `error` and `localNotification` events'
);
var listener;
if (type === 'notification') {
Expand All @@ -178,6 +179,13 @@ class PushNotificationIOS {
handler(registrationInfo.deviceToken);
}
);
} else if(type === 'error'){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a space between if(.

listener = RCTDeviceEventEmitter.addListener(
NOTIF_REGISTER_ERROR_EVENT,
(registrationError) => {
handler(registrationError);
}
);
}
_notifHandlers.set(handler, listener);
}
Expand Down Expand Up @@ -252,8 +260,8 @@ class PushNotificationIOS {
*/
static removeEventListener(type: string, handler: Function) {
invariant(
type === 'notification' || type === 'register' || type === 'localNotification',
'PushNotificationIOS only supports `notification`, `register` and `localNotification` events'
type === 'notification' || type === 'register' || type === 'localNotification' || type === 'error',
'PushNotificationIOS only supports `notification`, `register`, `error` and `localNotification` events'
);
var listener = _notifHandlers.get(handler);
if (!listener) {
Expand Down
1 change: 1 addition & 0 deletions Libraries/PushNotificationIOS/RCTPushNotificationManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@
+ (void)didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken;
+ (void)didReceiveRemoteNotification:(NSDictionary *)notification;
+ (void)didReceiveLocalNotification:(UILocalNotification *)notification;
+ (void)didFailToRegisterForRemoteNotificationsWithError:(NSError *)error;

@end
20 changes: 20 additions & 0 deletions Libraries/PushNotificationIOS/RCTPushNotificationManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
NSString *const RCTLocalNotificationReceived = @"LocalNotificationReceived";
NSString *const RCTRemoteNotificationReceived = @"RemoteNotificationReceived";
NSString *const RCTRemoteNotificationsRegistered = @"RemoteNotificationsRegistered";
NSString *const RCTRemoteNotificationRegisteredError = @"RemoteNotificationRegisteredError";

@implementation RCTConvert (UILocalNotification)

Expand Down Expand Up @@ -85,6 +86,10 @@ - (void)setBridge:(RCTBridge *)bridge
selector:@selector(handleRemoteNotificationsRegistered:)
name:RCTRemoteNotificationsRegistered
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleRemoteNotificationRegisteredError:)
name:RCTRemoteNotificationRegisteredError
object:nil];
}

- (NSDictionary<NSString *, id> *)constantsToExport
Expand Down Expand Up @@ -135,6 +140,15 @@ + (void)didReceiveLocalNotification:(UILocalNotification *)notification
userInfo:details];
}



+ (void)didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you the update the documentation in

* - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
to mention this method.

{
[[NSNotificationCenter defaultCenter] postNotificationName:RCTRemoteNotificationRegisteredError
object:self
userInfo:error.userInfo];
}

- (void)handleLocalNotificationReceived:(NSNotification *)notification
{
[_bridge.eventDispatcher sendDeviceEventWithName:@"localNotificationReceived"
Expand All @@ -153,6 +167,12 @@ - (void)handleRemoteNotificationsRegistered:(NSNotification *)notification
body:notification.userInfo];
}

- (void)handleRemoteNotificationRegisteredError:(NSNotification *)notification
{
[_bridge.eventDispatcher sendDeviceEventWithName:@"remoteNotificationsRegisteredError"
body:[notification userInfo]];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Judging by the preceding notification handlers, dot-notation is preferred: notification.userInfo.

}

/**
* Update the application icon badge number on the home screen
*/
Expand Down