From d727df5ef8adb600c162d7584847dc858027e95f Mon Sep 17 00:00:00 2001 From: Mike Holloway Date: Thu, 24 Mar 2022 11:00:24 +0000 Subject: [PATCH] Exposes new event: remoteNotificationsRegistrationDenied (#845) * Adds an event handler to understand when the user has pressed "Don't Allow" in permissions overlay --- lib/ios/RNEventEmitter.h | 1 + lib/ios/RNEventEmitter.m | 1 + lib/ios/RNNotificationCenter.m | 4 ++++ lib/src/adapters/NativeEventsReceiver.ts | 8 ++++++-- lib/src/events/EventsRegistry.ts | 4 ++++ website/docs/api/general-events.md | 9 +++++++++ 6 files changed, 25 insertions(+), 2 deletions(-) diff --git a/lib/ios/RNEventEmitter.h b/lib/ios/RNEventEmitter.h index 3ca3e6adc..b09d90fcd 100644 --- a/lib/ios/RNEventEmitter.h +++ b/lib/ios/RNEventEmitter.h @@ -1,6 +1,7 @@ #import static NSString* const RNRegistered = @"remoteNotificationsRegistered"; +static NSString* const RNRegistrationDenied = @"remoteNotificationsRegistrationDenied"; static NSString* const RNRegistrationFailed = @"remoteNotificationsRegistrationFailed"; static NSString* const RNPushKitRegistered = @"pushKitRegistered"; static NSString* const RNNotificationReceived = @"notificationReceived"; diff --git a/lib/ios/RNEventEmitter.m b/lib/ios/RNEventEmitter.m index 594badf2c..35bca6250 100644 --- a/lib/ios/RNEventEmitter.m +++ b/lib/ios/RNEventEmitter.m @@ -6,6 +6,7 @@ @implementation RNEventEmitter -(NSArray *)supportedEvents { return @[RNRegistered, + RNRegistrationDenied, RNRegistrationFailed, RNPushKitRegistered, RNNotificationReceived, diff --git a/lib/ios/RNNotificationCenter.m b/lib/ios/RNNotificationCenter.m index 4b3365625..afd5c7368 100644 --- a/lib/ios/RNNotificationCenter.m +++ b/lib/ios/RNNotificationCenter.m @@ -1,3 +1,4 @@ +#import "RNEventEmitter.h" #import "RNNotificationCenter.h" #import "RCTConvert+RNNotifications.h" @@ -40,6 +41,9 @@ - (void)requestPermissions:(NSDictionary *)options { } }]; } + if (!error && !granted) { + [RNEventEmitter sendEvent:RNRegistrationDenied body:nil]; + } }]; } diff --git a/lib/src/adapters/NativeEventsReceiver.ts b/lib/src/adapters/NativeEventsReceiver.ts index f71b490ea..18536ead0 100644 --- a/lib/src/adapters/NativeEventsReceiver.ts +++ b/lib/src/adapters/NativeEventsReceiver.ts @@ -25,13 +25,13 @@ export class NativeEventsReceiver { } public registerNotificationReceived(callback: (notification: Notification) => void): EmitterSubscription { - return this.emitter.addListener('notificationReceived', (payload) => { + return this.emitter.addListener('notificationReceived', (payload: unknown) => { callback(this.notificationFactory.fromPayload(payload)); }); } public registerNotificationReceivedBackground(callback: (notification: Notification) => void): EmitterSubscription { - return this.emitter.addListener('notificationReceivedBackground', (payload) => { + return this.emitter.addListener('notificationReceivedBackground', (payload: unknown) => { callback(this.notificationFactory.fromPayload(payload)); }); } @@ -50,4 +50,8 @@ export class NativeEventsReceiver { public registerRemoteNotificationsRegistrationFailed(callback: (event: RegistrationError) => void): EmitterSubscription { return this.emitter.addListener('remoteNotificationsRegistrationFailed', callback); } + + public registerRemoteNotificationsRegistrationDenied(callback: () => void): EmitterSubscription { + return this.emitter.addListener('remoteNotificationsRegistrationDenied', callback); + } } diff --git a/lib/src/events/EventsRegistry.ts b/lib/src/events/EventsRegistry.ts index a62af182c..6281a36af 100644 --- a/lib/src/events/EventsRegistry.ts +++ b/lib/src/events/EventsRegistry.ts @@ -34,4 +34,8 @@ export class EventsRegistry { public registerRemoteNotificationsRegistrationFailed(callback: (event: RegistrationError) => void): EmitterSubscription { return this.nativeEventsReceiver.registerRemoteNotificationsRegistrationFailed(callback); } + + public registerRemoteNotificationsRegistrationDenied(callback: () => void): EmitterSubscription { + return this.nativeEventsReceiver.registerRemoteNotificationsRegistrationDenied(callback); + } } diff --git a/website/docs/api/general-events.md b/website/docs/api/general-events.md index e0be0924c..e7ce7cefd 100755 --- a/website/docs/api/general-events.md +++ b/website/docs/api/general-events.md @@ -59,4 +59,13 @@ Fired when the user fails to register for remote notifications. Typically occurs Notifications.events().registerRemoteNotificationsRegistrationFailed((event: RegistrationError) => { console.log(event.code, event.localizedDescription, event.domain); }); +``` + +## registerRemoteNotificationsDenied() +Fired when the user does not grant permission to receive push notifications. Typically occurs when pressing the "Don't Allow" button in iOS permissions overlay. + +```js +Notifications.events().registerRemoteNotificationRegistrationDenied(() => { + console.log('Notification permissions not granted') +}) ``` \ No newline at end of file