首先,“热启动”意味着使用系统推送通知从后台状态启动应用程序。“暖”推出推送通知
我很努力地将我的应用程序路由到适当的屏幕以获得热启动通知。
在冷启动时,通知可通过PushNotificationIOS.popInitialNotification()
获得,但在热启动时返回null。回应“通知”事件很困难,因为我无法确定应用程序是从后台启动还是在前台运行时收到通知。
在React Native中处理热启动通知的正确方法是什么?
这里是我的PushNotificationHandler
,它封装应用的核心导航:
export default class PushNotificationHandler extends React.Component {
constructor(props) {
super(props)
this.state = {
modalInitialRoute: null,
modalOpen: false,
}
}
openModalWithRoute(route) {
this.setState({
modalInitialRoute: route,
modalOpen: true,
})
}
closeModal() {
this.setState({
modalOpen: false,
})
}
renderModal() {
const {
modalInitialRoute,
modalOpen,
} = this.state
return (
<Modal
close={() => this.closeModal()}
open={modalOpen}
initialRoute={modalInitialRoute}/>
)
}
componentWillMount() {
PushNotificationIOS.addEventListener('register', this._onPushNotificationRegistration)
PushNotificationIOS.addEventListener('notification', this._onPushNotification)
}
componentDidMount() {
this.requestPushNotificationPermission()
const notification = PushNotificationIOS.popInitialNotification()
if (notification) {
this.displayNotification(notification)
}
}
componentWillUnmount() {
PushNotificationIOS.removeEventListener('register', this._onPushNotificationRegistration)
PushNotificationIOS.removeEventListener('notification', this._onPushNotification)
}
requestPushNotificationPermission() {
PushNotificationIOS.checkPermissions((permissions) => {
// If no permissions are allowed, request permissions.
if (!(permissions.alert || permissions.badge || permissions.sound)) {
PushNotificationIOS.requestPermissions()
}
})
}
_onPushNotificationRegistration(deviceToken) {
const {
registerForPushNotifications,
} = this.props
const userId = this.props.parentId
registerForPushNotifications(userId, deviceToken)
}
_onPushNotification(notification) {
const {
receivePushNotification,
} = this.props
receivePushNotification(notification)
}
displayNotification(notification) {
const {
receivePushNotification
} = this.props
const data = notification.getData()
switch (data.type) {
case WORKSHEET_FEEDBACK:
this.openModalWithRoute({
component: SubmissionDetailsScreenContainer,
title: 'Submission Details',
key: 'submissionDetails',
props: {
submissionId: data.sID,
},
})
default:
break
}
}
render() {
//return this.renderModal()
return (
<View>
<View>
{this.props.children}
</View>
{this.renderModal()}
</View>
)
}
}