我在用着react-navigation
and react-native-push-notification
。我怎样才能打开某个StackNavigator's
屏幕输入onNotification
打回来?应该在以下情况下工作:
- 应用程序已关闭
- 应用程序在前台
- 应用程序在后台运行
我现在只需要它在 Android 中工作。
我尝试将回调函数传递给组件中的通知:
_handleClick() {
PushNotification.localNotification({
foreground: false
userInteraction: false
message: 'My Notification Message'
onOpen: () => { this.props.navigation.navigate("OtherScreen") },
})
}
并开火onOpen
in PushNotification
config:
onNotification: function(notification) {
notification.onOpen()
}
但似乎函数无法传递给通知,除非值是一个被忽略的字符串,导致onOpen
未定义。
好吧,看来我得发布我自己的解决方案了:)
// src/services/push-notification.js
const PushNotification = require('react-native-push-notification')
export function setupPushNotification(handleNotification) {
PushNotification.configure({
onNotification: function(notification) {
handleNotification(notification)
},
popInitialNotification: true,
requestPermissions: true,
})
return PushNotification
}
// Some notification-scheduling component
import {setupPushNotification} from "src/services/push-notification"
class SomeComponent extends PureComponent {
componentDidMount() {
this.pushNotification = setupPushNotification(this._handleNotificationOpen)
}
_handleNotificationOpen = () => {
const {navigate} = this.props.navigation
navigate("SomeOtherScreen")
}
_handlePress = () => {
this.pushNotification.localNotificationSchedule({
message: 'Some message',
date: new Date(Date.now() + (10 * 1000)), // to schedule it in 10 secs in my case
})
}
render() {
// use _handlePress function somewhere to schedule notification
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)