diff --git a/README.md b/README.md index 07bda846e1f92b745aa48e8b84d3c2a6aaeeb474..275b61da5e380394669b313cd9118cd71c82c406 100644 --- a/README.md +++ b/README.md @@ -58,6 +58,10 @@ And the following methods to support registration and receiving notifications: [RNNotifications didRegisterForRemoteNotificationsWithDeviceToken:deviceToken]; } +- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error { + [RNNotifications didFailToRegisterForRemoteNotificationsWithError:error]; +} + // Required for the notification event. - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)notification { [RNNotifications didReceiveRemoteNotification:notification]; @@ -151,16 +155,29 @@ import NotificationsIOS from 'react-native-notifications'; class App extends Component { constructor() { NotificationsIOS.addEventListener('remoteNotificationsRegistered', this.onPushRegistered.bind(this)); + NotificationsIOS.addEventListener('remoteNotificationsRegistrationFailed', this.onPushRegistrationFaled.bind(this)); NotificationsIOS.requestPermissions(); } onPushRegistered(deviceToken) { console.log("Device Token Received", deviceToken); } + + onPushRegistrationFailed(error) { + // For example: + // + // error={ + // domain: 'NSCocoaErroDomain', + // code: 3010, + // localizedDescription: 'remote notifications are not supported in the simulator' + // } + console.error(error); + } componentWillUnmount() { // prevent memory leaks! NotificationsIOS.removeEventListener('remoteNotificationsRegistered', this.onPushRegistered.bind(this)); + NotificationsIOS.removeEventListener('remoteNotificationsRegistrationFailed', this.onPushRegistrationFailed.bind(this)); } } diff --git a/RNNotifications/RNNotifications.h b/RNNotifications/RNNotifications.h index fb97dc0faad37f5b14e576ffa091433dc540411b..f72b424e9c9e744411efd171f39844153b2bb405 100644 --- a/RNNotifications/RNNotifications.h +++ b/RNNotifications/RNNotifications.h @@ -6,6 +6,7 @@ @interface RNNotifications : NSObject + (void)didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken; ++ (void)didFailToRegisterForRemoteNotificationsWithError:(NSError *)error; + (void)didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings; + (void)didUpdatePushCredentials:(PKPushCredentials *)credentials forType:(NSString *)type; diff --git a/RNNotifications/RNNotifications.m b/RNNotifications/RNNotifications.m index 47427862c45bec5c151b04f07b9e508dac54c533..1f8f90610ac9b38a656aad7cf2ed4cc562c08bf2 100644 --- a/RNNotifications/RNNotifications.m +++ b/RNNotifications/RNNotifications.m @@ -12,6 +12,7 @@ NSString* const RNNotificationCreateAction = @"CREATE"; NSString* const RNNotificationClearAction = @"CLEAR"; NSString* const RNNotificationsRegistered = @"RNNotificationsRegistered"; +NSString* const RNNotificationsRegistrationFailed = @"RNNotificationsRegistrationFailed"; NSString* const RNPushKitRegistered = @"RNPushKitRegistered"; NSString* const RNNotificationReceivedForeground = @"RNNotificationReceivedForeground"; NSString* const RNNotificationReceivedBackground = @"RNNotificationReceivedBackground"; @@ -118,6 +119,11 @@ RCT_EXPORT_MODULE() name:RNNotificationsRegistered object:nil]; + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(handleNotificationsRegistrationFailed:) + name:RNNotificationsRegistrationFailed + object:nil]; + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handlePushKitRegistered:) name:RNPushKitRegistered @@ -164,6 +170,12 @@ RCT_EXPORT_MODULE() userInfo:@{@"deviceToken": [self deviceTokenToString:deviceToken]}]; } ++ (void)didFailToRegisterForRemoteNotificationsWithError:(NSError *)error { + [[NSNotificationCenter defaultCenter] postNotificationName:RNNotificationsRegistrationFailed + object:self + userInfo:@{@"code": [NSNumber numberWithInteger:error.code], @"domain": error.domain, @"localizedDescription": error.localizedDescription}]; +} + + (void)didReceiveRemoteNotification:(NSDictionary *)notification { UIApplicationState state = [UIApplication sharedApplication].applicationState; @@ -389,6 +401,11 @@ RCT_EXPORT_MODULE() [_bridge.eventDispatcher sendDeviceEventWithName:@"remoteNotificationsRegistered" body:notification.userInfo]; } +- (void)handleNotificationsRegistrationFailed:(NSNotification *)notification +{ + [_bridge.eventDispatcher sendDeviceEventWithName:@"remoteNotificationsRegistrationFailed" body:notification.userInfo]; +} + - (void)handlePushKitRegistered:(NSNotification *)notification { [_bridge.eventDispatcher sendDeviceEventWithName:@"pushKitRegistered" body:notification.userInfo]; diff --git a/index.ios.js b/index.ios.js index 3bbf07298a385601d6234b0e7bc26d81d93dc23c..6dd788fd31723d7c892dbe9b0fef711e4064795f 100644 --- a/index.ios.js +++ b/index.ios.js @@ -10,6 +10,7 @@ const NativeRNNotifications = NativeModules.RNNotifications; // eslint-disable-l import IOSNotification from "./notification.ios"; export const DEVICE_REMOTE_NOTIFICATIONS_REGISTERED_EVENT = "remoteNotificationsRegistered"; +export const DEVICE_REMOTE_NOTIFICATIONS_REGISTRATION_FAILED_EVENT = "remoteNotificationsRegistrationFailed"; export const DEVICE_PUSH_KIT_REGISTERED_EVENT = "pushKitRegistered"; export const DEVICE_NOTIFICATION_RECEIVED_FOREGROUND_EVENT = "notificationReceivedForeground"; export const DEVICE_NOTIFICATION_RECEIVED_BACKGROUND_EVENT = "notificationReceivedBackground"; @@ -19,6 +20,7 @@ const DEVICE_NOTIFICATION_ACTION_RECEIVED = "notificationActionReceived"; const _exportedEvents = [ DEVICE_REMOTE_NOTIFICATIONS_REGISTERED_EVENT, + DEVICE_REMOTE_NOTIFICATIONS_REGISTRATION_FAILED_EVENT, DEVICE_PUSH_KIT_REGISTERED_EVENT, DEVICE_NOTIFICATION_RECEIVED_FOREGROUND_EVENT, DEVICE_NOTIFICATION_RECEIVED_BACKGROUND_EVENT, @@ -62,6 +64,11 @@ export default class NotificationsIOS { DEVICE_REMOTE_NOTIFICATIONS_REGISTERED_EVENT, registration => handler(registration.deviceToken) ); + } else if (type === DEVICE_REMOTE_NOTIFICATIONS_REGISTRATION_FAILED_EVENT) { + listener = DeviceEventEmitter.addListener( + DEVICE_REMOTE_NOTIFICATIONS_REGISTRATION_FAILED_EVENT, + error => handler(error) + ); } else if (type === DEVICE_PUSH_KIT_REGISTERED_EVENT) { listener = DeviceEventEmitter.addListener( DEVICE_PUSH_KIT_REGISTERED_EVENT, diff --git a/test/index.ios.spec.js b/test/index.ios.spec.js index c9cdd8206bd4dea739e6531c9bda8f129eb46fe3..2ba23fb41131fd4c6d9ace387b2134fd4440633a 100644 --- a/test/index.ios.spec.js +++ b/test/index.ios.spec.js @@ -9,6 +9,7 @@ describe("NotificationsIOS", () => { let deviceEvents = [ "pushKitRegistered", "remoteNotificationsRegistered", + "remoteNotificationsRegistrationFailed", "notificationReceivedForeground", "notificationReceivedBackground", "notificationOpened"