react-native-aliyun-push
react-native-aliyun-push copied to clipboard
可以收到通知 , 但是通知栏不出来
我翻看了下阿里云推送官方文档,官方建议 自建通知。 参考:https://help.aliyun.com/document_detail/53546.html
[email protected]
没有处理自建通知的逻辑,所以当APP处于 background
状态时,当然也就不弹通知栏,下面是源码:
https://github.com/wonday/react-native-aliyun-push/blob/master/android/src/main/java/org/wonday/aliyun/push/AliyunPushMessageReceiver.java#L56
给一段我的代码参考:
handleAliyunPushMessage = (msg) => {
console.debug('AliyunPush message received:', JSON.stringify(msg))
/*
* `msg`结构说明:
* msg.type: "notification":通知 或者 "message":消息
* msg.title: 推送通知/消息标题
* msg.body: 推送通知/消息具体内容
* msg.actionIdentifier: "opened":用户点击了通知, "removed"用户删除了通知, 其他非空值:用户点击了自定义action(仅限ios)
* msg.extras: 用户附加的{key:value}的对象
*/
try {
const { appState } = this.state
if (msg.type === 'notification') { // 处理通知
if (appState !== 'active' && msg) { // APP不处于前台时,才处理通知。前台则忽略通知
if (Platform.OS === 'android') {
// 阿里云推送官方文档,建议`自建通知`
// 参考: https://help.aliyun.com/document_detail/53546.html
Notifications.presentLocalNotificationAsync({
title: msg.title,
body: msg.body,
android: {
channelId: 'your app notification channel_id', // 与Android `MainApplication.createNotificationChannel()` 中配置相同
icon: 'your app icon url',
color: 'your app icon color',
},
}).catch((err) => {
console.debug('Android presentLocalNotificationAsync() failed:', err)
})
} else if (Platform.OS === 'ios') {
// todo: 处理 iOS 通知
}
}
} else if (msg.type === 'message') { // 处理消息
}
} catch (err) {
console.error('An error occurred while processing the AliyunPush message.', err)
}
}
Notifications.presentLocalNotificationAsync()
是Expo框架的接口,参考: https://docs.expo.io/versions/latest/sdk/notifications/
react-native原生文档我没找到Notification
的类似接口,只有PushNotificationIOS
,处理不了android
可以使用 https://github.com/wix/react-native-notifications 或者 直接Java原生扩展,参考阿里官方的文档。
请问您说的这种情况是不是app是前台打开的,或者在后台运行时发生的,并不是app被关闭的时候。我想确认一下app在后台运行,以及app被杀死后是否能弹出通知。这也是最基础的能力
@charmtiger 以上描述的是,android环境,app处于后台运行
中 也就是 appState=background
。
iOS 无论是进程被杀死,还是后台运行,都能正确接收推送,并弹出通知。
The same as me.
only method for me: try react-native-notification
.
because i don‘t know java at all.
有更好的解决方案吗?