react-native-aliyun-push icon indicating copy to clipboard operation
react-native-aliyun-push copied to clipboard

可以收到通知 , 但是通知栏不出来

Open shiguo2021 opened this issue 5 years ago • 6 comments

shiguo2021 avatar Dec 31 '19 14:12 shiguo2021

[email protected] android环境,相同问题。iOS正常。

JS事件监听能收到push通知,但是不弹出通知栏。

maxzhang avatar Jan 09 '20 08:01 maxzhang

我翻看了下阿里云推送官方文档,官方建议 自建通知。 参考: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原生扩展,参考阿里官方的文档。

maxzhang avatar Jan 09 '20 14:01 maxzhang

请问您说的这种情况是不是app是前台打开的,或者在后台运行时发生的,并不是app被关闭的时候。我想确认一下app在后台运行,以及app被杀死后是否能弹出通知。这也是最基础的能力

charmtiger avatar Feb 16 '20 00:02 charmtiger

@charmtiger 以上描述的是,android环境,app处于后台运行中 也就是 appState=background

iOS 无论是进程被杀死,还是后台运行,都能正确接收推送,并弹出通知。

maxzhang avatar Feb 18 '20 16:02 maxzhang

The same as me. only method for me: try react-native-notification. because i don‘t know java at all.

chenweigh avatar Aug 03 '20 08:08 chenweigh

有更好的解决方案吗?

hengkx avatar Nov 03 '20 03:11 hengkx