Skip to content
This repository was archived by the owner on Jan 14, 2025. It is now read-only.

Commit ab8e29f

Browse files
authored
Merge pull request #1516 from lukebars/patch-3
Populate userInfo on android, update RNCPushNotificationIOS
2 parents 9ae2ce5 + 762f4c0 commit ab8e29f

File tree

11 files changed

+108
-75
lines changed

11 files changed

+108
-75
lines changed

.github/FUNDING.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
github: [Dallas62]
2+
custom: ["https://www.buymeacoffee.com/Dallas62"]

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1919
### Fixed
2020

2121

22-
## [4.4.0] 2020-07-06
22+
## [4.0.0] 2020-07-06
2323

2424
### Breaking changes
2525

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,6 @@ EXAMPLE:
299299
```javascript
300300
PushNotification.localNotification({
301301
/* Android Only Properties */
302-
id: 0, // (optional) Valid unique 32 bit integer specified as string. default: Autogenerated Unique ID
303302
ticker: "My Notification Ticker", // (optional)
304303
showWhen: true, // (optional) default: true
305304
autoCancel: true, // (optional) default: true
@@ -331,11 +330,12 @@ PushNotification.localNotification({
331330
/* iOS only properties */
332331
alertAction: "view", // (optional) default: view
333332
category: "", // (optional) default: empty string
334-
userInfo: {}, // (optional) default: {} (using null throws a JSON value '<null>' error)
335333

336334
/* iOS and Android properties */
335+
id: 0, // (optional) Valid unique 32 bit integer specified as string. default: Autogenerated Unique ID
337336
title: "My Notification Title", // (optional)
338337
message: "My Notification Message", // (required)
338+
userInfo: {}, // (optional) default: {} (using null throws a JSON value '<null>' error)
339339
playSound: false, // (optional) default: true
340340
soundName: "default", // (optional) Sound to play when the notification is shown. Value of 'default' plays the default sound. It can be set to a custom sound such as 'android.resource://com.xyz/raw/my_sound'. It will look for the 'my_sound' audio file in 'res/raw' directory and play it. default: 'default' (default sound is played)
341341
number: 10, // (optional) Valid 32 bit integer specified as string. default: none (Cannot be zero)

android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotification.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,6 @@ private Bundle getBundleFromIntent(Intent intent) {
8989
public void onNewIntent(Intent intent) {
9090
Bundle bundle = this.getBundleFromIntent(intent);
9191
if (bundle != null) {
92-
bundle.putBoolean("foreground", false);
93-
intent.putExtra("notification", bundle);
9492
mJsDelivery.notifyNotification(bundle);
9593
}
9694
}

android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationHelper.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,7 @@ public void sendToNotificationCentreWithPicture(Bundle bundle, Bitmap largeIconB
422422

423423
Intent intent = new Intent(context, intentClass);
424424
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
425+
bundle.putBoolean("foreground", this.isApplicationInForeground());
425426
bundle.putBoolean("userInteraction", true);
426427
intent.putExtra("notification", bundle);
427428

@@ -574,7 +575,7 @@ public void sendToNotificationCentreWithPicture(Bundle bundle, Bitmap largeIconB
574575
editor.apply();
575576
}
576577

577-
if (!(this.isApplicationInForeground(context) && bundle.getBoolean("ignoreInForeground"))) {
578+
if (!(this.isApplicationInForeground() && bundle.getBoolean("ignoreInForeground"))) {
578579
Notification info = notification.build();
579580
info.defaults |= Notification.DEFAULT_LIGHTS;
580581

@@ -973,7 +974,7 @@ public boolean createChannel(ReadableMap channelInfo) {
973974
return checkOrCreateChannel(manager, channelId, channelName, channelDescription, soundUri, importance, vibratePattern);
974975
}
975976

976-
public boolean isApplicationInForeground(Context context) {
977+
public boolean isApplicationInForeground() {
977978
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
978979
List<RunningAppProcessInfo> processInfos = activityManager.getRunningAppProcesses();
979980
if (processInfos != null) {

android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationPublisher.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ private void handleLocalNotification(Context context, Bundle bundle) {
4040
Application applicationContext = (Application) context.getApplicationContext();
4141
RNPushNotificationHelper pushNotificationHelper = new RNPushNotificationHelper(applicationContext);
4242

43-
boolean isForeground = pushNotificationHelper.isApplicationInForeground(applicationContext);
43+
boolean isForeground = pushNotificationHelper.isApplicationInForeground();
4444

4545
Log.v(LOG_TAG, "sendNotification: " + bundle);
4646

example/NotifService.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ export default class NotifService {
4040
this.lastId++;
4141
PushNotification.localNotification({
4242
/* Android Only Properties */
43-
id: this.lastId, // (optional) Valid unique 32 bit integer specified as string. default: Autogenerated Unique ID
4443
ticker: 'My Notification Ticker', // (optional)
4544
autoCancel: true, // (optional) default: true
4645
largeIcon: 'ic_launcher', // (optional) default: "ic_launcher"
@@ -55,15 +54,16 @@ export default class NotifService {
5554
ongoing: false, // (optional) set whether this is an "ongoing" notification
5655
actions: ['Yes', 'No'], // (Android only) See the doc for notification actions to know more
5756
invokeApp: true, // (optional) This enable click on actions to bring back the application to foreground or stay in background, default: true
58-
57+
5958
/* iOS only properties */
6059
alertAction: 'view', // (optional) default: view
6160
category: '', // (optional) default: empty string
62-
userInfo: {}, // (optional) default: {} (using null throws a JSON value '<null>' error)
63-
61+
6462
/* iOS and Android properties */
63+
id: this.lastId, // (optional) Valid unique 32 bit integer specified as string. default: Autogenerated Unique ID
6564
title: 'Local Notification', // (optional)
6665
message: 'My Notification Message', // (required)
66+
userInfo: { screen: 'home' }, // (optional) default: {} (using null throws a JSON value '<null>' error)
6767
playSound: !!soundName, // (optional) default: true
6868
soundName: soundName ? soundName : 'default', // (optional) Sound to play when the notification is shown. Value of 'default' plays the default sound. It can be set to a custom sound such as 'android.resource://com.xyz/raw/my_sound'. It will look for the 'my_sound' audio file in 'res/raw' directory and play it. default: 'default' (default sound is played)
6969
number: 10, // (optional) Valid 32 bit integer specified as string. default: none (Cannot be zero)
@@ -76,7 +76,6 @@ export default class NotifService {
7676
date: new Date(Date.now() + 30 * 1000), // in 30 secs
7777

7878
/* Android Only Properties */
79-
id: this.lastId, // (optional) Valid unique 32 bit integer specified as string. default: Autogenerated Unique ID
8079
ticker: 'My Notification Ticker', // (optional)
8180
autoCancel: true, // (optional) default: true
8281
largeIcon: 'ic_launcher', // (optional) default: "ic_launcher"
@@ -95,11 +94,12 @@ export default class NotifService {
9594
/* iOS only properties */
9695
alertAction: 'view', // (optional) default: view
9796
category: '', // (optional) default: empty string
98-
userInfo: {}, // (optional) default: {} (using null throws a JSON value '<null>' error)
99-
97+
10098
/* iOS and Android properties */
99+
id: this.lastId, // (optional) Valid unique 32 bit integer specified as string. default: Autogenerated Unique ID
101100
title: 'Scheduled Notification', // (optional)
102101
message: 'My Notification Message', // (required)
102+
userInfo: { sceen: "home" }, // (optional) default: {} (using null throws a JSON value '<null>' error)
103103
playSound: !!soundName, // (optional) default: true
104104
number: 10, // (optional) Valid 32 bit integer specified as string. default: none (Cannot be zero)
105105
soundName: soundName ? soundName : 'default', // (optional) Sound to play when the notification is shown. Value of 'default' plays the default sound. It can be set to a custom sound such as 'android.resource://com.xyz/raw/my_sound'. It will look for the 'my_sound' audio file in 'res/raw' directory and play it. default: 'default' (default sound is played)

example/ios/Podfile.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ PODS:
292292
- React-cxxreact (= 0.62.2)
293293
- React-jsi (= 0.62.2)
294294
- ReactCommon/callinvoker (= 0.62.2)
295-
- RNCPushNotificationIOS (1.2.2):
295+
- RNCPushNotificationIOS (1.4.0):
296296
- React
297297
- Yoga (1.14.0)
298298
- YogaKit (1.18.1):
@@ -453,10 +453,10 @@ SPEC CHECKSUMS:
453453
React-RCTText: fae545b10cfdb3d247c36c56f61a94cfd6dba41d
454454
React-RCTVibration: 4356114dbcba4ce66991096e51a66e61eda51256
455455
ReactCommon: ed4e11d27609d571e7eee8b65548efc191116eb3
456-
RNCPushNotificationIOS: 4c97a36dbec42dba411cc35e6dac25e34a805fde
456+
RNCPushNotificationIOS: dc1c0c6aa18a128df123598149f42e848d26a4ac
457457
Yoga: 3ebccbdd559724312790e7742142d062476b698e
458458
YogaKit: f782866e155069a2cca2517aafea43200b01fd5a
459459

460460
PODFILE CHECKSUM: 56c2537f71f3f02200d6918c542a8e89a0b422fa
461461

462-
COCOAPODS: 1.9.1
462+
COCOAPODS: 1.9.3

example/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"pod-install": "cd ios && pod install"
1212
},
1313
"dependencies": {
14-
"@react-native-community/push-notification-ios": "^1.2.2",
14+
"@react-native-community/push-notification-ios": "^1.4.0",
1515
"react": "16.11.0",
1616
"react-native": "0.62.2",
1717
"react-native-push-notification": "zo0r/react-native-push-notification#dev"

index.js

Lines changed: 86 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ var Notifications = {
2020
onAction: false,
2121
onRemoteFetch: false,
2222
isLoaded: false,
23-
idInitialNotification: null,
23+
isPopInitialNotification: false,
2424

2525
isPermissionsRequestPending: false,
2626

@@ -95,27 +95,31 @@ Notifications.configure = function(options) {
9595
this.isLoaded = true;
9696
}
9797

98-
const handlePopInitialNotification = function(state) {
98+
const handlePopInitialNotification = (state) => {
9999
if('active' !== state) {
100100
return;
101101
}
102102

103103
if (options.popInitialNotification === undefined || options.popInitialNotification === true) {
104104
this.popInitialNotification(function(firstNotification) {
105-
if ( firstNotification !== null ) {
106-
if(false === firstNotification.userInteraction || this.idInitialNotification === firstNotification.id) {
107-
return;
108-
}
109-
110-
this.idInitialNotification = firstNotification.id;
111-
this._onNotification(firstNotification, true);
105+
if(this.isPopInitialNotification) {
106+
return;
112107
}
108+
109+
this.isPopInitialNotification = true;
110+
111+
if (!firstNotification || false === firstNotification.userInteraction) {
112+
return;
113+
}
114+
115+
this._onNotification(firstNotification, true);
113116
}.bind(this));
114117
}
115118
}
116119

117120
AppState.addEventListener('change', handlePopInitialNotification.bind(this));
118-
handlePopInitialNotification();
121+
122+
handlePopInitialNotification(AppState.currentState);
119123

120124
if ( options.requestPermissions !== false ) {
121125
this._requestPermissions();
@@ -152,6 +156,12 @@ Notifications.localNotification = function(details) {
152156
}
153157
}
154158

159+
if (details.userInfo) {
160+
details.userInfo.id = details.userInfo.id || details.id;
161+
} else {
162+
details.userInfo = {id: details.id};
163+
}
164+
155165
if (Platform.OS === 'ios') {
156166
// https://developer.apple.com/reference/uikit/uilocalnotification
157167

@@ -161,12 +171,6 @@ Notifications.localNotification = function(details) {
161171
soundName = ''; // empty string results in no sound (and no vibration)
162172
}
163173

164-
if (details.userInfo) {
165-
details.userInfo.id = details.userInfo.id || details.id;
166-
} else {
167-
details.userInfo = {id: details.id};
168-
}
169-
170174
// for valid fields see: https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/PayloadKeyReference.html
171175
// alertTitle only valid for apple watch: https://developer.apple.com/library/ios/documentation/iPhone/Reference/UILocalNotification_Class/#//apple_ref/occ/instp/UILocalNotification/alertTitle
172176

@@ -224,19 +228,19 @@ Notifications.localNotificationSchedule = function(details) {
224228
}
225229
}
226230

231+
if (details.userInfo) {
232+
details.userInfo.id = details.userInfo.id || details.id;
233+
} else {
234+
details.userInfo = {id: details.id};
235+
}
236+
227237
if (Platform.OS === 'ios') {
228238
let soundName = details.soundName ? details.soundName : 'default'; // play sound (and vibrate) as default behaviour
229239

230240
if (details.hasOwnProperty('playSound') && !details.playSound) {
231241
soundName = ''; // empty string results in no sound (and no vibration)
232242
}
233243

234-
if (details.userInfo) {
235-
details.userInfo.id = details.userInfo.id || details.id;
236-
} else {
237-
details.userInfo = {id: details.id};
238-
}
239-
240244
const iosDetails = {
241245
fireDate: details.date.toISOString(),
242246
alertTitle: details.title,
@@ -326,45 +330,71 @@ Notifications._onAction = function(notification) {
326330
this.onAction(notification);
327331
}
328332

329-
Notifications._onNotification = function(data, isFromBackground = null) {
333+
Notifications._transformNotificationObject = function(data, isFromBackground = null) {
334+
if(!data) {
335+
return;
336+
}
337+
330338
if ( isFromBackground === null ) {
331339
isFromBackground = (
332340
data.foreground === false ||
333341
AppState.currentState === 'background'
334342
);
335343
}
336344

337-
if ( this.onNotification !== false ) {
338-
if ( Platform.OS === 'ios' ) {
339-
this.onNotification({
340-
id: data?.userInfo?.id,
341-
foreground: ! isFromBackground,
342-
userInteraction: isFromBackground,
343-
message: data.getMessage(),
344-
data: data.getData(),
345-
badge: data.getBadgeCount(),
346-
alert: data.getAlert(),
347-
sound: data.getSound(),
348-
fireDate: data._fireDate,
349-
finish: (res) => data.finish(res)
350-
});
351-
} else {
352-
var notificationData = {
353-
foreground: ! isFromBackground,
354-
finish: () => {},
355-
...data
356-
};
357-
358-
if ( typeof notificationData.data === 'string' ) {
359-
try {
360-
notificationData.data = JSON.parse(notificationData.data);
361-
} catch(e) {
362-
/* void */
363-
}
345+
let _notification;
346+
347+
if ( Platform.OS === 'ios' ) {
348+
const notifData = data.getData();
349+
350+
_notification = {
351+
id: notifData?.id,
352+
foreground: !isFromBackground,
353+
userInteraction: isFromBackground,
354+
message: data.getMessage(),
355+
data: notifData,
356+
badge: data.getBadgeCount(),
357+
title: data.getTitle(),
358+
soundName: data.getSound(),
359+
fireDate: Date.parse(data._fireDate),
360+
finish: (res) => data.finish(res)
361+
};
362+
} else {
363+
_notification = {
364+
foreground: ! isFromBackground,
365+
finish: () => {},
366+
...data,
367+
};
368+
369+
if ( typeof _notification.data === 'string' ) {
370+
try {
371+
_notification.data = JSON.parse(_notification.data);
372+
} catch(e) {
373+
/* void */
364374
}
375+
}
376+
377+
_notification.data = {
378+
...(typeof _notification.userInfo === 'object' ? _notification.userInfo : {}),
379+
...(typeof _notification.data === 'object' ? _notification.data : {}),
380+
};
365381

366-
this.onNotification(notificationData);
382+
delete _notification.userInfo;
383+
delete _notification.notificationId;
384+
}
385+
386+
return _notification;
387+
}
388+
389+
Notifications._onNotification = function(data, initialNotification = false) {
390+
if ( this.onNotification !== false ) {
391+
let notification = data;
392+
393+
if(!initialNotification) {
394+
notification = this._transformNotificationObject(data);
367395
}
396+
397+
this.onNotification(notification);
368398
}
369399
};
370400

@@ -434,8 +464,10 @@ Notifications.getApplicationIconBadgeNumber = function() {
434464
};
435465

436466
Notifications.popInitialNotification = function(handler) {
437-
this.callNative('getInitialNotification').then(function(result){
438-
handler(result);
467+
this.callNative('getInitialNotification').then((result) => {
468+
handler(
469+
this._transformNotificationObject(result, true)
470+
);
439471
});
440472
};
441473

@@ -471,7 +503,7 @@ Notifications.getScheduledLocalNotifications = function(callback) {
471503
soundName: notif.soundName,
472504
repeatInterval: notif.repeatInterval,
473505
id: notif.userInfo?.id,
474-
date: new Date(notif.fireDate),
506+
date: new Date(notif.fireDate),
475507
number: notif?.applicationIconBadgeNumber,
476508
message: notif?.alertBody,
477509
title: notif?.alertTitle,

0 commit comments

Comments
 (0)