【问题标题】:Notification can not display correctly when app in background ( React Native )应用程序在后台时无法正确显示通知(React Native)
【发布时间】:2018-12-23 14:57:35
【问题描述】:

我有一个使用 Firebase 推送远程通知的应用,一个客户端发送如下通知消息:

            body: JSON.stringify({
                'to' : 'token',
                'notification' : {
                    'title' : ' ' + this.currentUser.email + ' send you a message !',      
                    'body' : text
                },
                'data': {
                    'senderName' : this.currentUser.email,
                    'senderUid': this.currentUser.uid 
                }

            })

和一个客户端实现接收通知的方法:

           firebase.notifications().onNotification((notification) => {
                    const showNotification = new firebase.notifications.Notification()
                        .setNotificationId('notificationId')
                        .setTitle(notification.title)
                        .setBody(notification.data.text)
                        .android.setChannelId('channel_id_foreground')
                        .android.setSmallIcon('ic_launcher');
                    firebase.notifications().displayNotification(showNotification)
            });

当我的应用程序在前台运行时,通知正常显示,但在后台时,它可以在通知托盘中接收,但不会将该通知显示到设备屏幕上。




通知显示如下:




我希望通知可以显示如下:


补充:如果我通过滑动关闭应用并发送通知,我的应用将崩溃。


谁能帮帮我?

【问题讨论】:

  • 问题很明显。如果使用通知消息并且应用程序在后台,则无法触发侦听器 onNotification。通知只显示在通知托盘中。
  • 附加:这一行 classpath 'com.google.gms:google-services:4.0.1' 如果在应用关闭时收到通知,可能会使应用崩溃/刷卡。解决这一变化:classpath 'com.google.gms:google-services:3.2.1' 或更低
  • 你找到解决办法了吗?
  • 我也遇到了同样的问题,你们找到解决办法了吗?

标签: firebase react-native push-notification firebase-cloud-messaging


【解决方案1】:

正如RN Firebase docs 中提到的,当您的应用程序在后台或关闭时,通知由操作系统处理和显示,因此永远不会调用您的 onNotification() 方法,因此永远不会设置通知通道。根据the android developer docs,没有频道的通知不会出现。

这对我有用:如果您希望通知在 android 中显示为提示通知,请修改您的消息正文以包含您的频道 ID,如下所示:

body: JSON.stringify({
        'to' : 'token',
        'notification' : {
            'title' : ' ' + this.currentUser.email + ' send you a message !',      
            'body' : text
        },
        'data': {
            'senderName' : this.currentUser.email,
            'senderUid': this.currentUser.uid 
        },
        'android': {
            'priority': 'high',
            'notification': {
                'channel_id': 'channel_id_foreground',
            },
        }
    })

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-03
    • 2012-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-04
    • 2020-05-08
    • 1970-01-01
    相关资源
    最近更新 更多