反应本机推送通知 onNotification 事件不起作用
React native Push Notification onNotification event not working
在这个问题上卡了好几天。所以,我使用这个包来实现本地推送通知:
https://github.com/zo0r/react-native-push-notification
我可以像这样获得本地预定通知:
PushNotification.localNotificationSchedule({
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"
smallIcon: "ic_notification", // (optional) default: "ic_notification" with fallback for "ic_launcher"
bigText: "My big text that will be shown when notification is expanded", // (optional) default: "message" prop
subText: "This is a subText", // (optional) default: none
color: "blue", // (optional) default: system default
vibrate: true, // (optional) default: true
vibration: 300, // vibration length in milliseconds, ignored if vibrate=false, default: 1000
tag: "some_tag", // (optional) add tag to message
group: "group", // (optional) add group to message
ongoing: false, // (optional) set whether this is an "ongoing" notification
/* iOS only properties */
alertAction: "view", // (optional) default: view
category: null, // (optional) default: null
userInfo: null, // (optional) default: null (object containing additional notification data)
/* iOS and Android properties */
title: "Scheduled Notification", // (optional)
message: "My Notification Message", // (required)
playSound: true, // (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)
actions: '["Yes", "No"]', // (Android only) See the doc for notification actions to know more
foreground: false, // BOOLEAN: If the notification was received in foreground or not
userInteraction: true, // BOOLEAN: If the notification was opened by the user from the notification area or not
data: { type: "test" } // OBJECT: The push data
});
我想在用户点击通知和打开应用程序时调用一个函数。为了实现以下目标,我在 App.js:
中这样做了
async componentDidMount() {
PushNotification.configure({
// (required) Called when a remote or local notification is opened or received
onNotification: function(notification) {
console.log("NOTIFICATION:", notification);
},
// IOS ONLY (optional): default: all - Permissions to register.
permissions: {
alert: true,
badge: true,
sound: true
},
// Should the initial notification be popped automatically
// default: true
popInitialNotification: true,
/**
* (optional) default: true
* - Specified if permissions (ios) and token (android and ios) will requested or not,
* - if not, you must call PushNotificationsHandler.requestPermissions() later
*/
requestPermissions: true
});
}
但是 onNotification
函数没有被调用。
我已经完成了以下关于 SO 的问题,但没有成功。
我还在 Github 上提出了 issue。
可能的解决方案是什么?
对于那些寻求解决方案的人,我在 app.js 的 componentDidMount
方法中做了类似的事情:
PushNotification.configure({
// (required) Called when a remote or local notification is opened or received
onNotification: notification => {
console.log(notification);
if (notification.action === "Take") {
//do something here
} else if (notification.action === "Skip") {
//do something here
} else if (notification.action === "Snooze") {
//do something here
}
},
// IOS ONLY (optional): default: all - Permissions to register.
permissions: {
alert: true,
badge: true,
sound: true
},
// Should the initial notification be popped automatically
// default: true
popInitialNotification: true,
/**
* (optional) default: true
* - Specified if permissions (ios) and token (android and ios) will requested or not,
* - if not, you must call PushNotificationsHandler.requestPermissions() later
*/
requestPermissions: true
});
在这个问题上卡了好几天。所以,我使用这个包来实现本地推送通知:
https://github.com/zo0r/react-native-push-notification
我可以像这样获得本地预定通知:
PushNotification.localNotificationSchedule({
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"
smallIcon: "ic_notification", // (optional) default: "ic_notification" with fallback for "ic_launcher"
bigText: "My big text that will be shown when notification is expanded", // (optional) default: "message" prop
subText: "This is a subText", // (optional) default: none
color: "blue", // (optional) default: system default
vibrate: true, // (optional) default: true
vibration: 300, // vibration length in milliseconds, ignored if vibrate=false, default: 1000
tag: "some_tag", // (optional) add tag to message
group: "group", // (optional) add group to message
ongoing: false, // (optional) set whether this is an "ongoing" notification
/* iOS only properties */
alertAction: "view", // (optional) default: view
category: null, // (optional) default: null
userInfo: null, // (optional) default: null (object containing additional notification data)
/* iOS and Android properties */
title: "Scheduled Notification", // (optional)
message: "My Notification Message", // (required)
playSound: true, // (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)
actions: '["Yes", "No"]', // (Android only) See the doc for notification actions to know more
foreground: false, // BOOLEAN: If the notification was received in foreground or not
userInteraction: true, // BOOLEAN: If the notification was opened by the user from the notification area or not
data: { type: "test" } // OBJECT: The push data
});
我想在用户点击通知和打开应用程序时调用一个函数。为了实现以下目标,我在 App.js:
中这样做了async componentDidMount() {
PushNotification.configure({
// (required) Called when a remote or local notification is opened or received
onNotification: function(notification) {
console.log("NOTIFICATION:", notification);
},
// IOS ONLY (optional): default: all - Permissions to register.
permissions: {
alert: true,
badge: true,
sound: true
},
// Should the initial notification be popped automatically
// default: true
popInitialNotification: true,
/**
* (optional) default: true
* - Specified if permissions (ios) and token (android and ios) will requested or not,
* - if not, you must call PushNotificationsHandler.requestPermissions() later
*/
requestPermissions: true
});
}
但是 onNotification
函数没有被调用。
我已经完成了以下关于 SO 的问题,但没有成功。
我还在 Github 上提出了 issue。
可能的解决方案是什么?
对于那些寻求解决方案的人,我在 app.js 的 componentDidMount
方法中做了类似的事情:
PushNotification.configure({
// (required) Called when a remote or local notification is opened or received
onNotification: notification => {
console.log(notification);
if (notification.action === "Take") {
//do something here
} else if (notification.action === "Skip") {
//do something here
} else if (notification.action === "Snooze") {
//do something here
}
},
// IOS ONLY (optional): default: all - Permissions to register.
permissions: {
alert: true,
badge: true,
sound: true
},
// Should the initial notification be popped automatically
// default: true
popInitialNotification: true,
/**
* (optional) default: true
* - Specified if permissions (ios) and token (android and ios) will requested or not,
* - if not, you must call PushNotificationsHandler.requestPermissions() later
*/
requestPermissions: true
});