Skip to content

Commit 4699811

Browse files
committed
Split foreground and background notification type.
1 parent 8b93b99 commit 4699811

2 files changed

Lines changed: 55 additions & 20 deletions

File tree

‎Libraries/PushNotificationIOS/PushNotificationIOS.js‎

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ var _notifHandlers = {};
1919
var _initialNotification = RCTPushNotificationManager &&
2020
RCTPushNotificationManager.initialNotification;
2121

22-
var DEVICE_NOTIF_EVENT = 'remoteNotificationReceived';
22+
var DEVICE_NOTIF_EVENT_FOREGROUND = 'remoteNotificationReceivedForeground';
23+
var DEVICE_NOTIF_EVENT_BACKGROUND = 'remoteNotificationReceivedBackground';
2324

2425
/**
2526
* Handle push notifications for your app, including permission handling and
@@ -52,19 +53,31 @@ class PushNotificationIOS {
5253
* Attaches a listener to remote notifications while the app is running in the
5354
* foreground or the background.
5455
*
55-
* The handler will get be invoked with an instance of `PushNotificationIOS`
56+
* The handler will get be invoked with an instance of `PushNotificationIOS`.
57+
*
5658
*/
5759
static addEventListener(type: string, handler: Function) {
5860
invariant(
59-
type === 'notification',
60-
'PushNotificationIOS only supports `notification` events'
61-
);
62-
_notifHandlers[handler] = RCTDeviceEventEmitter.addListener(
63-
DEVICE_NOTIF_EVENT,
64-
(notifData) => {
65-
handler(new PushNotificationIOS(notifData));
66-
}
61+
type === 'notificationForeground' || type === 'notificationBackground',
62+
'PushNotificationIOS only supports `notificationForeground` and `notificationBackground` events'
6763
);
64+
65+
if (type === 'notificationForeground') {
66+
_notifHandlers[handler] = RCTDeviceEventEmitter.addListener(
67+
DEVICE_NOTIF_EVENT_FOREGROUND,
68+
(notifData) => {
69+
handler(new PushNotificationIOS(notifData));
70+
}
71+
);
72+
} else {
73+
_notifHandlers[handler] = RCTDeviceEventEmitter.addListener(
74+
DEVICE_NOTIF_EVENT_BACKGROUND,
75+
(notifData) => {
76+
handler(new PushNotificationIOS(notifData));
77+
}
78+
);
79+
}
80+
6881
}
6982

7083
/**
@@ -97,8 +110,8 @@ class PushNotificationIOS {
97110
*/
98111
static removeEventListener(type: string, handler: Function) {
99112
invariant(
100-
type === 'notification',
101-
'PushNotificationIOS only supports `notification` events'
113+
type === 'notificationForeground' || type === 'notificationBackground',
114+
'PushNotificationIOS only supports `notificationForeground` and `notificationBackground` events'
102115
);
103116
if (!_notifHandlers[handler]) {
104117
return;

‎Libraries/PushNotificationIOS/RCTPushNotificationManager.m‎

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
#import "RCTBridge.h"
1313
#import "RCTEventDispatcher.h"
1414

15-
NSString *const RCTRemoteNotificationReceived = @"RemoteNotificationReceived";
15+
NSString *const RCTRemoteNotificationReceivedForeground = @"RemoteNotificationReceivedForeground";
16+
NSString *const RCTRemoteNotificationReceivedBackground = @"RemoteNotificationReceivedBackground";
1617

1718
@implementation RCTPushNotificationManager
1819
{
@@ -27,8 +28,13 @@ - (instancetype)init
2728
{
2829
if ((self = [super init])) {
2930
[[NSNotificationCenter defaultCenter] addObserver:self
30-
selector:@selector(handleRemoteNotificationReceived:)
31-
name:RCTRemoteNotificationReceived
31+
selector:@selector(handleRemoteNotificationReceivedForeground:)
32+
name:RCTRemoteNotificationReceivedForeground
33+
object:nil];
34+
35+
[[NSNotificationCenter defaultCenter] addObserver:self
36+
selector:@selector(handleRemoteNotificationReceivedBackground:)
37+
name:RCTRemoteNotificationReceivedBackground
3238
object:nil];
3339
}
3440
return self;
@@ -54,14 +60,30 @@ + (void)application:(UIApplication *)application didRegisterUserNotificationSett
5460

5561
+ (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)notification
5662
{
57-
[[NSNotificationCenter defaultCenter] postNotificationName:RCTRemoteNotificationReceived
58-
object:self
59-
userInfo:notification];
63+
UIApplicationState state = [application applicationState];
64+
65+
if (state == UIApplicationStateActive) {
66+
//app is in foreground
67+
[[NSNotificationCenter defaultCenter] postNotificationName:RCTRemoteNotificationReceivedForeground
68+
object:self
69+
userInfo:notification];
70+
} else {
71+
//app is in background
72+
[[NSNotificationCenter defaultCenter] postNotificationName:RCTRemoteNotificationReceivedBackground
73+
object:self
74+
userInfo:notification];
75+
}
76+
}
77+
78+
- (void)handleRemoteNotificationReceivedForeground:(NSNotification *)notification
79+
{
80+
[_bridge.eventDispatcher sendDeviceEventWithName:@"remoteNotificationReceivedForeground"
81+
body:[notification userInfo]];
6082
}
6183

62-
- (void)handleRemoteNotificationReceived:(NSNotification *)notification
84+
- (void)handleRemoteNotificationReceivedBackground:(NSNotification *)notification
6385
{
64-
[_bridge.eventDispatcher sendDeviceEventWithName:@"remoteNotificationReceived"
86+
[_bridge.eventDispatcher sendDeviceEventWithName:@"remoteNotificationReceivedBackground"
6587
body:[notification userInfo]];
6688
}
6789

0 commit comments

Comments
 (0)