Skip to content

Commit d727df5

Browse files
authored
Exposes new event: remoteNotificationsRegistrationDenied (#845)
* Adds an event handler to understand when the user has pressed "Don't Allow" in permissions overlay
1 parent 9ec4916 commit d727df5

File tree

6 files changed

+25
-2
lines changed

6 files changed

+25
-2
lines changed

lib/ios/RNEventEmitter.h

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#import <React/RCTEventEmitter.h>
22

33
static NSString* const RNRegistered = @"remoteNotificationsRegistered";
4+
static NSString* const RNRegistrationDenied = @"remoteNotificationsRegistrationDenied";
45
static NSString* const RNRegistrationFailed = @"remoteNotificationsRegistrationFailed";
56
static NSString* const RNPushKitRegistered = @"pushKitRegistered";
67
static NSString* const RNNotificationReceived = @"notificationReceived";

lib/ios/RNEventEmitter.m

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ @implementation RNEventEmitter
66

77
-(NSArray<NSString *> *)supportedEvents {
88
return @[RNRegistered,
9+
RNRegistrationDenied,
910
RNRegistrationFailed,
1011
RNPushKitRegistered,
1112
RNNotificationReceived,

lib/ios/RNNotificationCenter.m

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#import "RNEventEmitter.h"
12
#import "RNNotificationCenter.h"
23
#import "RCTConvert+RNNotifications.h"
34

@@ -40,6 +41,9 @@ - (void)requestPermissions:(NSDictionary *)options {
4041
}
4142
}];
4243
}
44+
if (!error && !granted) {
45+
[RNEventEmitter sendEvent:RNRegistrationDenied body:nil];
46+
}
4347
}];
4448
}
4549

lib/src/adapters/NativeEventsReceiver.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ export class NativeEventsReceiver {
2525
}
2626

2727
public registerNotificationReceived(callback: (notification: Notification) => void): EmitterSubscription {
28-
return this.emitter.addListener('notificationReceived', (payload) => {
28+
return this.emitter.addListener('notificationReceived', (payload: unknown) => {
2929
callback(this.notificationFactory.fromPayload(payload));
3030
});
3131
}
3232

3333
public registerNotificationReceivedBackground(callback: (notification: Notification) => void): EmitterSubscription {
34-
return this.emitter.addListener('notificationReceivedBackground', (payload) => {
34+
return this.emitter.addListener('notificationReceivedBackground', (payload: unknown) => {
3535
callback(this.notificationFactory.fromPayload(payload));
3636
});
3737
}
@@ -50,4 +50,8 @@ export class NativeEventsReceiver {
5050
public registerRemoteNotificationsRegistrationFailed(callback: (event: RegistrationError) => void): EmitterSubscription {
5151
return this.emitter.addListener('remoteNotificationsRegistrationFailed', callback);
5252
}
53+
54+
public registerRemoteNotificationsRegistrationDenied(callback: () => void): EmitterSubscription {
55+
return this.emitter.addListener('remoteNotificationsRegistrationDenied', callback);
56+
}
5357
}

lib/src/events/EventsRegistry.ts

+4
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,8 @@ export class EventsRegistry {
3434
public registerRemoteNotificationsRegistrationFailed(callback: (event: RegistrationError) => void): EmitterSubscription {
3535
return this.nativeEventsReceiver.registerRemoteNotificationsRegistrationFailed(callback);
3636
}
37+
38+
public registerRemoteNotificationsRegistrationDenied(callback: () => void): EmitterSubscription {
39+
return this.nativeEventsReceiver.registerRemoteNotificationsRegistrationDenied(callback);
40+
}
3741
}

website/docs/api/general-events.md

+9
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,13 @@ Fired when the user fails to register for remote notifications. Typically occurs
5959
Notifications.events().registerRemoteNotificationsRegistrationFailed((event: RegistrationError) => {
6060
console.log(event.code, event.localizedDescription, event.domain);
6161
});
62+
```
63+
64+
## registerRemoteNotificationsDenied()
65+
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.
66+
67+
```js
68+
Notifications.events().registerRemoteNotificationRegistrationDenied(() => {
69+
console.log('Notification permissions not granted')
70+
})
6271
```

0 commit comments

Comments
 (0)