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

Populate userInfo on android, update RNCPushNotificationIOS #1516

Merged
merged 22 commits into from
Aug 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
c90fc6e
return a Promise in getScheduledLocalNotifications
lukebars Jun 10, 2020
a5cdcda
updated example/App.js
lukebars Jun 10, 2020
b76a4e4
Merge pull request #1 from lukebars/patch-2
lukebars Jun 10, 2020
9aa88d6
Create FUNDING.yml
Dallas62 Jul 1, 2020
da34ac1
Fix custom link
Dallas62 Jul 1, 2020
e37552d
Merge branch 'dev'
Dallas62 Jul 6, 2020
86af98d
Bump package.json to 4.0.0
Dallas62 Jul 6, 2020
2816253
Update CHANGELOG.md
Brianop Jul 7, 2020
1b73593
Merge pull request #1508 from Brianop/patch-1
Dallas62 Jul 7, 2020
74e9b8b
add data to android notification when scheduling
lukebars Jul 8, 2020
454af8c
"@react-native-community/push-notification-ios": "^1.3.0"
lukebars Jul 8, 2020
579ffb8
reflecting userInfo working on both platforms in documentation and ex…
lukebars Jul 8, 2020
0c828e9
Merge branch 'master' of https://github.com/lukebars/react-native-pus…
lukebars Jul 8, 2020
3e19105
revert changes
lukebars Jul 8, 2020
85952df
populate userInfo with id on both platforms
lukebars Jul 8, 2020
22d8c75
return notification id in onNotification method on iOS
lukebars Jul 8, 2020
9e84803
align onNotification on both platforms
lukebars Jul 21, 2020
9cc4167
keep notification data from fcm while populating data userInfo
lukebars Jul 27, 2020
d547687
make sure data/userInfo is an object before spreading
lukebars Jul 27, 2020
7589c4a
Apply changes to popInitialNotification.
Dallas62 Jul 27, 2020
271461e
fix popInitialNotification and null pointer
Dallas62 Jul 27, 2020
762f4c0
Fix behaviour of popInitialNotification and onNotification.
Dallas62 Jul 31, 2020
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
2 changes: 2 additions & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github: [Dallas62]
custom: ["https://www.buymeacoffee.com/Dallas62"]
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
### Fixed


## [4.4.0] 2020-07-06
## [4.0.0] 2020-07-06

### Breaking changes

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,6 @@ EXAMPLE:
```javascript
PushNotification.localNotification({
/* Android Only Properties */
id: 0, // (optional) Valid unique 32 bit integer specified as string. default: Autogenerated Unique ID
ticker: "My Notification Ticker", // (optional)
showWhen: true, // (optional) default: true
autoCancel: true, // (optional) default: true
Expand Down Expand Up @@ -330,11 +329,12 @@ PushNotification.localNotification({
/* iOS only properties */
alertAction: "view", // (optional) default: view
category: "", // (optional) default: empty string
userInfo: {}, // (optional) default: {} (using null throws a JSON value '<null>' error)

/* iOS and Android properties */
id: 0, // (optional) Valid unique 32 bit integer specified as string. default: Autogenerated Unique ID
title: "My Notification Title", // (optional)
message: "My Notification Message", // (required)
userInfo: {}, // (optional) default: {} (using null throws a JSON value '<null>' error)
playSound: false, // (optional) default: true
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)
number: 10, // (optional) Valid 32 bit integer specified as string. default: none (Cannot be zero)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,6 @@ private Bundle getBundleFromIntent(Intent intent) {
public void onNewIntent(Intent intent) {
Bundle bundle = this.getBundleFromIntent(intent);
if (bundle != null) {
bundle.putBoolean("foreground", false);
intent.putExtra("notification", bundle);
mJsDelivery.notifyNotification(bundle);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,7 @@ public void sendToNotificationCentreWithPicture(Bundle bundle, Bitmap largeIconB

Intent intent = new Intent(context, intentClass);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
bundle.putBoolean("foreground", this.isApplicationInForeground());
bundle.putBoolean("userInteraction", true);
intent.putExtra("notification", bundle);

Expand Down Expand Up @@ -583,7 +584,7 @@ public void sendToNotificationCentreWithPicture(Bundle bundle, Bitmap largeIconB
editor.apply();
}

if (!(this.isApplicationInForeground(context) && bundle.getBoolean("ignoreInForeground"))) {
if (!(this.isApplicationInForeground() && bundle.getBoolean("ignoreInForeground"))) {
Notification info = notification.build();
info.defaults |= Notification.DEFAULT_LIGHTS;

Expand Down Expand Up @@ -938,7 +939,7 @@ private void checkOrCreateChannel(NotificationManager manager, String channel_id
}
}

public boolean isApplicationInForeground(Context context) {
public boolean isApplicationInForeground() {
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningAppProcessInfo> processInfos = activityManager.getRunningAppProcesses();
if (processInfos != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ private void handleLocalNotification(Context context, Bundle bundle) {
Application applicationContext = (Application) context.getApplicationContext();
RNPushNotificationHelper pushNotificationHelper = new RNPushNotificationHelper(applicationContext);

boolean isForeground = pushNotificationHelper.isApplicationInForeground(applicationContext);
boolean isForeground = pushNotificationHelper.isApplicationInForeground();

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

Expand Down
14 changes: 7 additions & 7 deletions example/NotifService.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ export default class NotifService {
this.lastId++;
PushNotification.localNotification({
/* Android Only Properties */
id: this.lastId, // (optional) Valid unique 32 bit integer specified as string. default: Autogenerated Unique ID
ticker: 'My Notification Ticker', // (optional)
autoCancel: true, // (optional) default: true
largeIcon: 'ic_launcher', // (optional) default: "ic_launcher"
Expand All @@ -39,15 +38,16 @@ export default class NotifService {
ongoing: false, // (optional) set whether this is an "ongoing" notification
actions: ['Yes', 'No'], // (Android only) See the doc for notification actions to know more
invokeApp: true, // (optional) This enable click on actions to bring back the application to foreground or stay in background, default: true

/* iOS only properties */
alertAction: 'view', // (optional) default: view
category: '', // (optional) default: empty string
userInfo: {}, // (optional) default: {} (using null throws a JSON value '<null>' error)


/* iOS and Android properties */
id: this.lastId, // (optional) Valid unique 32 bit integer specified as string. default: Autogenerated Unique ID
title: 'Local Notification', // (optional)
message: 'My Notification Message', // (required)
userInfo: { screen: 'home' }, // (optional) default: {} (using null throws a JSON value '<null>' error)
playSound: !!soundName, // (optional) default: true
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)
number: 10, // (optional) Valid 32 bit integer specified as string. default: none (Cannot be zero)
Expand All @@ -60,7 +60,6 @@ export default class NotifService {
date: new Date(Date.now() + 30 * 1000), // in 30 secs

/* Android Only Properties */
id: this.lastId, // (optional) Valid unique 32 bit integer specified as string. default: Autogenerated Unique ID
ticker: 'My Notification Ticker', // (optional)
autoCancel: true, // (optional) default: true
largeIcon: 'ic_launcher', // (optional) default: "ic_launcher"
Expand All @@ -79,11 +78,12 @@ export default class NotifService {
/* iOS only properties */
alertAction: 'view', // (optional) default: view
category: '', // (optional) default: empty string
userInfo: {}, // (optional) default: {} (using null throws a JSON value '<null>' error)


/* iOS and Android properties */
id: this.lastId, // (optional) Valid unique 32 bit integer specified as string. default: Autogenerated Unique ID
title: 'Scheduled Notification', // (optional)
message: 'My Notification Message', // (required)
userInfo: { sceen: "home" }, // (optional) default: {} (using null throws a JSON value '<null>' error)
playSound: !!soundName, // (optional) default: true
number: 10, // (optional) Valid 32 bit integer specified as string. default: none (Cannot be zero)
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)
Expand Down
6 changes: 3 additions & 3 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ PODS:
- React-cxxreact (= 0.62.2)
- React-jsi (= 0.62.2)
- ReactCommon/callinvoker (= 0.62.2)
- RNCPushNotificationIOS (1.2.2):
- RNCPushNotificationIOS (1.4.0):
- React
- Yoga (1.14.0)
- YogaKit (1.18.1):
Expand Down Expand Up @@ -453,10 +453,10 @@ SPEC CHECKSUMS:
React-RCTText: fae545b10cfdb3d247c36c56f61a94cfd6dba41d
React-RCTVibration: 4356114dbcba4ce66991096e51a66e61eda51256
ReactCommon: ed4e11d27609d571e7eee8b65548efc191116eb3
RNCPushNotificationIOS: 4c97a36dbec42dba411cc35e6dac25e34a805fde
RNCPushNotificationIOS: dc1c0c6aa18a128df123598149f42e848d26a4ac
Yoga: 3ebccbdd559724312790e7742142d062476b698e
YogaKit: f782866e155069a2cca2517aafea43200b01fd5a

PODFILE CHECKSUM: 56c2537f71f3f02200d6918c542a8e89a0b422fa

COCOAPODS: 1.9.1
COCOAPODS: 1.9.3
2 changes: 1 addition & 1 deletion example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"pod-install": "cd ios && pod install"
},
"dependencies": {
"@react-native-community/push-notification-ios": "^1.2.2",
"@react-native-community/push-notification-ios": "^1.4.0",
"react": "16.11.0",
"react-native": "0.62.2",
"react-native-push-notification": "git+https://[email protected]/zo0r/react-native-push-notification.git"
Expand Down
140 changes: 86 additions & 54 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var Notifications = {
onAction: false,
onRemoteFetch: false,
isLoaded: false,
idInitialNotification: null,
isPopInitialNotification: false,

isPermissionsRequestPending: false,

Expand Down Expand Up @@ -95,27 +95,31 @@ Notifications.configure = function(options) {
this.isLoaded = true;
}

const handlePopInitialNotification = function(state) {
const handlePopInitialNotification = (state) => {
if('active' !== state) {
return;
}

if (options.popInitialNotification === undefined || options.popInitialNotification === true) {
this.popInitialNotification(function(firstNotification) {
if ( firstNotification !== null ) {
if(false === firstNotification.userInteraction || this.idInitialNotification === firstNotification.id) {
return;
}

this.idInitialNotification = firstNotification.id;
this._onNotification(firstNotification, true);
if(this.isPopInitialNotification) {
return;
}

this.isPopInitialNotification = true;

if (!firstNotification || false === firstNotification.userInteraction) {
return;
}

this._onNotification(firstNotification, true);
}.bind(this));
}
}

AppState.addEventListener('change', handlePopInitialNotification.bind(this));
handlePopInitialNotification();

handlePopInitialNotification(AppState.currentState);

if ( options.requestPermissions !== false ) {
this._requestPermissions();
Expand Down Expand Up @@ -152,6 +156,12 @@ Notifications.localNotification = function(details) {
}
}

if (details.userInfo) {
details.userInfo.id = details.userInfo.id || details.id;
} else {
details.userInfo = {id: details.id};
}

if (Platform.OS === 'ios') {
// https://developer.apple.com/reference/uikit/uilocalnotification

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

if (details.userInfo) {
details.userInfo.id = details.userInfo.id || details.id;
} else {
details.userInfo = {id: details.id};
}

// for valid fields see: https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/PayloadKeyReference.html
// alertTitle only valid for apple watch: https://developer.apple.com/library/ios/documentation/iPhone/Reference/UILocalNotification_Class/#//apple_ref/occ/instp/UILocalNotification/alertTitle

Expand Down Expand Up @@ -224,19 +228,19 @@ Notifications.localNotificationSchedule = function(details) {
}
}

if (details.userInfo) {
details.userInfo.id = details.userInfo.id || details.id;
} else {
details.userInfo = {id: details.id};
}

if (Platform.OS === 'ios') {
let soundName = details.soundName ? details.soundName : 'default'; // play sound (and vibrate) as default behaviour

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

if (details.userInfo) {
details.userInfo.id = details.userInfo.id || details.id;
} else {
details.userInfo = {id: details.id};
}

const iosDetails = {
fireDate: details.date.toISOString(),
alertTitle: details.title,
Expand Down Expand Up @@ -326,45 +330,71 @@ Notifications._onAction = function(notification) {
this.onAction(notification);
}

Notifications._onNotification = function(data, isFromBackground = null) {
Notifications._transformNotificationObject = function(data, isFromBackground = null) {
if(!data) {
return;
}

if ( isFromBackground === null ) {
isFromBackground = (
data.foreground === false ||
AppState.currentState === 'background'
);
}

if ( this.onNotification !== false ) {
if ( Platform.OS === 'ios' ) {
this.onNotification({
id: data?.userInfo?.id,
foreground: ! isFromBackground,
userInteraction: isFromBackground,
message: data.getMessage(),
data: data.getData(),
badge: data.getBadgeCount(),
alert: data.getAlert(),
sound: data.getSound(),
fireDate: data._fireDate,
finish: (res) => data.finish(res)
});
} else {
var notificationData = {
foreground: ! isFromBackground,
finish: () => {},
...data
};

if ( typeof notificationData.data === 'string' ) {
try {
notificationData.data = JSON.parse(notificationData.data);
} catch(e) {
/* void */
}
let _notification;

if ( Platform.OS === 'ios' ) {
const notifData = data.getData();

_notification = {
id: notifData?.id,
foreground: !isFromBackground,
userInteraction: isFromBackground,
message: data.getMessage(),
data: notifData,
badge: data.getBadgeCount(),
title: data.getTitle(),
soundName: data.getSound(),
fireDate: Date.parse(data._fireDate),
finish: (res) => data.finish(res)
};
} else {
_notification = {
foreground: ! isFromBackground,
finish: () => {},
...data,
};

if ( typeof _notification.data === 'string' ) {
try {
_notification.data = JSON.parse(_notification.data);
} catch(e) {
/* void */
}
}

_notification.data = {
...(typeof _notification.userInfo === 'object' ? _notification.userInfo : {}),
...(typeof _notification.data === 'object' ? _notification.data : {}),
};

this.onNotification(notificationData);
delete _notification.userInfo;
delete _notification.notificationId;
}

return _notification;
}

Notifications._onNotification = function(data, initialNotification = false) {
if ( this.onNotification !== false ) {
let notification = data;

if(!initialNotification) {
notification = this._transformNotificationObject(data);
}

this.onNotification(notification);
}
};

Expand Down Expand Up @@ -434,8 +464,10 @@ Notifications.getApplicationIconBadgeNumber = function() {
};

Notifications.popInitialNotification = function(handler) {
this.callNative('getInitialNotification').then(function(result){
handler(result);
this.callNative('getInitialNotification').then((result) => {
handler(
this._transformNotificationObject(result, true)
);
});
};

Expand Down Expand Up @@ -471,7 +503,7 @@ Notifications.getScheduledLocalNotifications = function(callback) {
soundName: notif.soundName,
repeatInterval: notif.repeatInterval,
id: notif.userInfo?.id,
date: new Date(notif.fireDate),
date: new Date(notif.fireDate),
number: notif?.applicationIconBadgeNumber,
message: notif?.alertBody,
title: notif?.alertTitle,
Expand Down
Loading