React native 无法在react native中从fcm获取推送通知

React native 无法在react native中从fcm获取推送通知,react-native,push-notification,react-native-firebase,React Native,Push Notification,React Native Firebase,我从firebase收到推送通知,但当我使用react-native firebase库中的react-native在android上发送推送通知时,我在android上没有收到任何通知。但是,我能够使用onMessage方法在控制台上显示消息。但是如何在通知栏中获取通知。我的消息只是数据,因此我还创建了bgMessaging.js来处理后台消息,在这里我还可以在控制台上显示消息,但不能在通知上显示 如何解决此问题并在通知栏上显示仅限数据的消息 下面是我的代码 bgMessaging.js 从

我从firebase收到推送通知,但当我使用react-native firebase库中的react-native在android上发送推送通知时,我在android上没有收到任何通知。但是,我能够使用onMessage方法在控制台上显示消息。但是如何在通知栏中获取通知。我的消息只是数据,因此我还创建了bgMessaging.js来处理后台消息,在这里我还可以在控制台上显示消息,但不能在通知上显示

如何解决此问题并在通知栏上显示仅限数据的消息

下面是我的代码

bgMessaging.js

从“react native firebase”导入firebase; //可选流量类型 从“react native firebase”导入类型{RemoteMessage}; 导出默认异步消息:RemoteMessage=>{ //处理好你的信息 console.logmessage console.logmessage //senName=message.data.senderName; //senUid=message.data.senderUid; //const通知=新建 //firebase.notifications.Notification //.setNotificationId'notificationId' //.setTitlemessage.data.title //.setBodymessage.data.body //.android.setChannelId'channel\u id\u front' //.android.setSmallIcon'ic_launcher'; //firebase.notifications.displayNotificationnotification; 回报承诺。决心;
} 您还可以在显示通知时执行以下操作:

firebase.notifications().onNotificationDisplayed(notification => { ... })
或者当手机接收到该信息时:

firebase.notifications().onNotification(notification => { ... })
如果要获取触发应用程序打开的通知,请使用以下命令:

firebase.notifications().getInitialNotification().then(notification => { ... })
请参阅此处的文档

App.js

async componentDidMount() {
    this.getToken();
    this.createNotificationListeners();
}

async createNotificationListeners() {
this.notificationListener = firebase.notifications().onNotification((notification) => {
            const { title, body, data } = notification;
            notification
                .android.setChannelId('channel')
                .android.setSmallIcon('icon')
                .android.setAutoCancel(true)
                .android.setPriority(firebase.notifications.Android.Priority.Max)
                .setSound('default');

            firebase.notifications().displayNotification(notification);
        });
}

我遇到了完全相同的问题,我收到了仅限数据的消息,但无法显示通知

我发现,为了显示8+Android版本的通知,您需要先创建一个Android频道,代码:

// Create Channel first.
  const channel = new firebase.notifications.Android.Channel(
    "general-channel",
    "General Notifications",
    firebase.notifications.Android.Importance.Default
  ).setDescription("General Notifications");
  firebase.notifications().android.createChannel(channel);

  // Build your notification
  const notification = new firebase.notifications.Notification()
    .setTitle(...)
    .setBody(...)
    .setNotificationId("notification-action")
    .setSound("default")
    .setData(message.data)
    .android.setChannelId("general-channel")
    .android.setPriority(firebase.notifications.Android.Priority.Max);

  // Display the notification (with debug)
  firebase
    .notifications()
    .displayNotification(notification)
    .catch(err => console.error(err));