在React Native中无法从FCM获取推送通知

时间:2018-12-13 06:02:39

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

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

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

下面是我的代码

bgMessaging.js

import firebase from 'react-native-firebase';
// Optional flow type
import type { RemoteMessage } from 'react-native-firebase';

export default async (message: RemoteMessage) => {
    // handle your message
    console.log("message")
    console.log(message)

    // senName = message.data.senderName;
    // senUid = message.data.senderUid;
    // const notification = new 
    // firebase.notifications.Notification()
    //     .setNotificationId('notificationId')
    //     .setTitle(message.data.title)
    //     .setBody(message.data.body)
    //     .android.setChannelId('channel_id_foreground')
    //     .android.setSmallIcon('ic_launcher');
    // firebase.notifications().displayNotification(notification);

    return Promise.resolve();
}

index.js(末尾添加几行)

AppRegistry.registerHeadlessTask('RNFirebaseBackgroundMessage', () => bgMessaging); // <-- Add this line

App.js

componentDidMount() {
    this.messageListener = firebase.messaging().onMessage((message: RemoteMessage) => {
      //process data message
      console.log(message);
    });
 }

AndroidManifest.xml

<service android:name="io.invertase.firebase.messaging.RNFirebaseBackgroundMessagingService" />
      <service android:name="io.invertase.firebase.messaging.RNFirebaseMessagingService">
        <intent-filter>
          <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
      </service>  
      <service android:name="io.invertase.firebase.messaging.RNFirebaseInstanceIdService">
        <intent-filter>
          <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
        </intent-filter>
      </service>
      <service android:name=".MyTaskService" />

3 个答案:

答案 0 :(得分:1)

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

我发现,要显示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));

答案 1 :(得分:0)

显示通知时,您还可以执行以下操作:

firebase.notifications().onNotificationDisplayed(notification => { ... })

或者通过电话接收时:

firebase.notifications().onNotification(notification => { ... })

如果要获取触发应用程序打开的通知,请使用以下命令:

firebase.notifications().getInitialNotification().then(notification => { ... })

请在此处https://rnfirebase.io/docs/v4.3.x/notifications/receiving-notifications

引用该文档

答案 2 :(得分:0)

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);
        });
}
相关问题