Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
shiguo2021 opened this issue Dec 31, 2019 · 6 comments
Open

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

shiguo2021 opened this issue Dec 31, 2019 · 6 comments

Comments

@shiguo2021
Copy link

@maxzhang
Copy link

maxzhang commented Jan 9, 2020

react-native-aliyun-push@1.0.21 android环境,相同问题。iOS正常。

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

@maxzhang
Copy link

maxzhang commented Jan 9, 2020

我翻看了下阿里云推送官方文档,官方建议 自建通知
参考:https://help.aliyun.com/document_detail/53546.html

react-native-aliyun-push@1.0.21 没有处理自建通知的逻辑,所以当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原生扩展,参考阿里官方的文档。

@charmtiger
Copy link

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

@maxzhang
Copy link

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

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

@chenweigh
Copy link
Contributor

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

@hengkx
Copy link

hengkx commented Nov 3, 2020

有更好的解决方案吗?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants