From 0dca865c35dae490f75607fbe3647ddb3aa8d1a9 Mon Sep 17 00:00:00 2001 From: Mike Holloway Date: Thu, 17 Mar 2022 12:05:59 +0000 Subject: [PATCH 1/3] Adds an event handler to undertand 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 | 3 +++ lib/src/adapters/NativeEventsReceiver.ts | 4 ++++ lib/src/events/EventsRegistry.ts | 4 ++++ website/docs/api/general-events.md | 9 +++++++++ 6 files changed, 22 insertions(+) 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..a93659de0 100644 --- a/lib/ios/RNNotificationCenter.m +++ b/lib/ios/RNNotificationCenter.m @@ -40,6 +40,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..87118be79 100644 --- a/lib/src/adapters/NativeEventsReceiver.ts +++ b/lib/src/adapters/NativeEventsReceiver.ts @@ -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..23a0beb03 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 From f2bec0226e489626a68c03496875ef6bb1183162 Mon Sep 17 00:00:00 2001 From: Mike Holloway Date: Thu, 17 Mar 2022 13:49:11 +0000 Subject: [PATCH 2/3] Tweak --- lib/src/adapters/NativeEventsReceiver.ts | 4 ++-- lib/src/events/EventsRegistry.ts | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/src/adapters/NativeEventsReceiver.ts b/lib/src/adapters/NativeEventsReceiver.ts index 87118be79..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)); }); } diff --git a/lib/src/events/EventsRegistry.ts b/lib/src/events/EventsRegistry.ts index 23a0beb03..6281a36af 100644 --- a/lib/src/events/EventsRegistry.ts +++ b/lib/src/events/EventsRegistry.ts @@ -37,5 +37,5 @@ export class EventsRegistry { public registerRemoteNotificationsRegistrationDenied(callback: () => void): EmitterSubscription { return this.nativeEventsReceiver.registerRemoteNotificationsRegistrationDenied(callback); -} + } } From 6587adeed7cab91de4eae45397b752bfc2540a3c Mon Sep 17 00:00:00 2001 From: Mike Holloway Date: Thu, 17 Mar 2022 14:03:38 +0000 Subject: [PATCH 3/3] Adding missing import statement --- lib/ios/RNNotificationCenter.m | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/ios/RNNotificationCenter.m b/lib/ios/RNNotificationCenter.m index a93659de0..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"