Skip to content

Commit

Permalink
Exposes new event: remoteNotificationsRegistrationDenied (#845)
Browse files Browse the repository at this point in the history
* Adds an event handler to understand when the user has pressed "Don't Allow" in permissions overlay
  • Loading branch information
artdevgame authored Mar 24, 2022
1 parent 9ec4916 commit d727df5
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/ios/RNEventEmitter.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#import <React/RCTEventEmitter.h>

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";
Expand Down
1 change: 1 addition & 0 deletions lib/ios/RNEventEmitter.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ @implementation RNEventEmitter

-(NSArray<NSString *> *)supportedEvents {
return @[RNRegistered,
RNRegistrationDenied,
RNRegistrationFailed,
RNPushKitRegistered,
RNNotificationReceived,
Expand Down
4 changes: 4 additions & 0 deletions lib/ios/RNNotificationCenter.m
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#import "RNEventEmitter.h"
#import "RNNotificationCenter.h"
#import "RCTConvert+RNNotifications.h"

Expand Down Expand Up @@ -40,6 +41,9 @@ - (void)requestPermissions:(NSDictionary *)options {
}
}];
}
if (!error && !granted) {
[RNEventEmitter sendEvent:RNRegistrationDenied body:nil];
}
}];
}

Expand Down
8 changes: 6 additions & 2 deletions lib/src/adapters/NativeEventsReceiver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
});
}
Expand All @@ -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);
}
}
4 changes: 4 additions & 0 deletions lib/src/events/EventsRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
9 changes: 9 additions & 0 deletions website/docs/api/general-events.md
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})
```

0 comments on commit d727df5

Please sign in to comment.