Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow listen to foreground or background notification separately #1524

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 22 additions & 9 deletions Libraries/PushNotificationIOS/PushNotificationIOS.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ var _notifHandlers = {};
var _initialNotification = RCTPushNotificationManager &&
RCTPushNotificationManager.initialNotification;

var DEVICE_NOTIF_EVENT = 'remoteNotificationReceived';
var DEVICE_NOTIF_EVENT_FOREGROUND = 'remoteNotificationReceivedForeground';
var DEVICE_NOTIF_EVENT_BACKGROUND = 'remoteNotificationReceivedBackground';
var NOTIF_REGISTER_EVENT = 'remoteNotificationsRegistered';

/**
Expand Down Expand Up @@ -55,19 +56,30 @@ class PushNotificationIOS {
*
* Valid events are:
*
* - `notification` : Fired when a remote notification is received. The
* handler will be invoked with an instance of `PushNotificationIOS`.
* - `notificationForeground` : Fired when a remote notification is received
* and the app is running in foreground. The handler will be invoked with an
* instance of `PushNotificationIOS`.
* - `notificationBackground` : Same as `notificationForeground`, only it will
* only be fired when app is opening from background.
* - `register`: Fired when the user registers for remote notifications. The
* handler will be invoked with a hex string representing the deviceToken.
*/
static addEventListener(type: string, handler: Function) {
invariant(
type === 'notification' || type === 'register',
'PushNotificationIOS only supports `notification` and `register` events'
type === 'notificationForeground' || type === 'notificationBackground' || type === 'register',
'PushNotificationIOS only supports `notificationForeground`, `notificationBackground`, and `register` events'
);
if (type === 'notification') {

if (type === 'notificationForeground') {
_notifHandlers[handler] = RCTDeviceEventEmitter.addListener(
DEVICE_NOTIF_EVENT,
DEVICE_NOTIF_EVENT_FOREGROUND,
(notifData) => {
handler(new PushNotificationIOS(notifData));
}
);
} else if (type === 'notificationBackground') {
_notifHandlers[handler] = RCTDeviceEventEmitter.addListener(
DEVICE_NOTIF_EVENT_BACKGROUND,
(notifData) => {
handler(new PushNotificationIOS(notifData));
}
Expand All @@ -80,6 +92,7 @@ class PushNotificationIOS {
}
);
}

}

/**
Expand Down Expand Up @@ -140,8 +153,8 @@ class PushNotificationIOS {
*/
static removeEventListener(type: string, handler: Function) {
invariant(
type === 'notification' || type === 'register',
'PushNotificationIOS only supports `notification` and `register` events'
type === 'notificationForeground' || type === 'notificationBackground' || type === 'register',
'PushNotificationIOS only supports `notificationForeground`, `notificationBackground`, and `register` events'
);
if (!_notifHandlers[handler]) {
return;
Expand Down
39 changes: 31 additions & 8 deletions Libraries/PushNotificationIOS/RCTPushNotificationManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@

#endif

NSString *const RCTRemoteNotificationReceived = @"RemoteNotificationReceived";
NSString *const RCTRemoteNotificationReceivedForeground = @"RemoteNotificationReceivedForeground";
NSString *const RCTRemoteNotificationReceivedBackground = @"RemoteNotificationReceivedBackground";
NSString *const RCTRemoteNotificationsRegistered = @"RemoteNotificationsRegistered";

@implementation RCTPushNotificationManager
Expand All @@ -38,9 +39,15 @@ - (instancetype)init
{
if ((self = [super init])) {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleRemoteNotificationReceived:)
name:RCTRemoteNotificationReceived
selector:@selector(handleRemoteNotificationReceivedForeground:)
name:RCTRemoteNotificationReceivedForeground
object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleRemoteNotificationReceivedBackground:)
name:RCTRemoteNotificationReceivedBackground
object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleRemoteNotificationsRegistered:)
name:RCTRemoteNotificationsRegistered
Expand Down Expand Up @@ -84,14 +91,30 @@ + (void)application:(UIApplication *)application didRegisterForRemoteNotificatio

+ (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)notification
{
[[NSNotificationCenter defaultCenter] postNotificationName:RCTRemoteNotificationReceived
object:self
userInfo:notification];
UIApplicationState state = [application applicationState];

if (state == UIApplicationStateActive) {
//app is in foreground
[[NSNotificationCenter defaultCenter] postNotificationName:RCTRemoteNotificationReceivedForeground
object:self
userInfo:notification];
} else {
//app is in background
[[NSNotificationCenter defaultCenter] postNotificationName:RCTRemoteNotificationReceivedBackground
object:self
userInfo:notification];
}
}

- (void)handleRemoteNotificationReceivedForeground:(NSNotification *)notification
{
[_bridge.eventDispatcher sendDeviceEventWithName:@"remoteNotificationReceivedForeground"
body:[notification userInfo]];
}

- (void)handleRemoteNotificationReceived:(NSNotification *)notification
- (void)handleRemoteNotificationReceivedBackground:(NSNotification *)notification
{
[_bridge.eventDispatcher sendDeviceEventWithName:@"remoteNotificationReceived"
[_bridge.eventDispatcher sendDeviceEventWithName:@"remoteNotificationReceivedBackground"
body:[notification userInfo]];
}

Expand Down