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

[Pushnotifications] Add error event #2336

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

var DEVICE_NOTIF_EVENT = 'remoteNotificationReceived';
var NOTIF_REGISTER_EVENT = 'remoteNotificationsRegistered';
var NOTIF_REGISTER_ERROR_EVENT = 'remoteNotificationsRegisteredError';

/**
* Handle push notifications for your app, including permission handling and
Expand Down Expand Up @@ -88,8 +89,8 @@ class PushNotificationIOS {
*/
static addEventListener(type: string, handler: Function) {
invariant(
type === 'notification' || type === 'register',
'PushNotificationIOS only supports `notification` and `register` events'
type === 'notification' || type === 'register' || type === 'error',
'PushNotificationIOS only supports `notification`, `register` and `error` events'
);
var listener;
if (type === 'notification') {
Expand All @@ -106,6 +107,17 @@ class PushNotificationIOS {
handler(registrationInfo.deviceToken);
}
);
} else if(type === 'error'){
listener = RCTDeviceEventEmitter.addListener(
NOTIF_REGISTER_ERROR_EVENT,
(registrationError) => {
var key = Object.keys(registrationError)[0] || null;
if(!key){
return;
}
handler(registrationError[key], key);
}
);
}
_notifHandlers.set(handler, listener);
}
Expand Down Expand Up @@ -180,8 +192,8 @@ class PushNotificationIOS {
*/
static removeEventListener(type: string, handler: Function) {
invariant(
type === 'notification' || type === 'register',
'PushNotificationIOS only supports `notification` and `register` events'
type === 'notification' || type === 'register' || type === 'error',
'PushNotificationIOS only supports `notification`, `register` and `error` 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 @@ -16,5 +16,6 @@
+ (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings;
+ (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken;
+ (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)notification;
+ (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error;

@end
23 changes: 20 additions & 3 deletions Libraries/PushNotificationIOS/RCTPushNotificationManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

NSString *const RCTRemoteNotificationReceived = @"RemoteNotificationReceived";
NSString *const RCTRemoteNotificationsRegistered = @"RemoteNotificationsRegistered";
NSString *const RCTRemoteNotificationRegisteredError = @"RemoteNotificationRegisteredError";

@implementation RCTConvert (UILocalNotification)

Expand Down Expand Up @@ -60,6 +61,10 @@ - (instancetype)init
selector:@selector(handleRemoteNotificationsRegistered:)
name:RCTRemoteNotificationsRegistered
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleRemoteNotificationRegisteredError:)
name:RCTRemoteNotificationRegisteredError
object:nil];
}
return self;
}
Expand Down Expand Up @@ -105,6 +110,13 @@ + (void)application:(__unused UIApplication *)application didReceiveRemoteNotifi
userInfo:notification];
}

+ (void)application:(__unused UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
[[NSNotificationCenter defaultCenter] postNotificationName:RCTRemoteNotificationRegisteredError
object:self
userInfo:error.userInfo];
}

- (void)handleRemoteNotificationReceived:(NSNotification *)notification
{
[_bridge.eventDispatcher sendDeviceEventWithName:@"remoteNotificationReceived"
Expand All @@ -116,6 +128,11 @@ - (void)handleRemoteNotificationsRegistered:(NSNotification *)notification
[_bridge.eventDispatcher sendDeviceEventWithName:@"remoteNotificationsRegistered"
body:notification.userInfo];
}
- (void)handleRemoteNotificationRegisteredError:(NSNotification *)notification
{
[_bridge.eventDispatcher sendDeviceEventWithName:@"remoteNotificationsRegisteredError"
body:[notification userInfo]];
}

/**
* Update the application icon badge number on the home screen
Expand Down Expand Up @@ -152,17 +169,17 @@ - (void)handleRemoteNotificationsRegistered:(NSNotification *)notification
types = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;
}

#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_8_0
if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {

id notificationSettings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];
[[UIApplication sharedApplication] registerForRemoteNotifications];

#else
}else{

[[UIApplication sharedApplication] registerForRemoteNotificationTypes:types];

#endif
}

}

Expand Down