未收到FCM通知

时间:2019-10-12 08:15:04

标签: android typescript firebase firebase-cloud-messaging react-native-firebase

反应原生版本:0.60.5

React Native Firebase版本:5.5.6

Android Native Firebase版本:17.0.0

dependencies{
    implementation project(':react-native-firebase')
...
    implementation 'com.google.firebase:firebase-core:17.0.0'
    implementation 'com.google.firebase:firebase-messaging:19.0.1'
...
}

MainApplication.java

    protected List<ReactPackage> getPackages() {
      @SuppressWarnings("UnnecessaryLocalVariable")
      List<ReactPackage> packages = new PackageList(this).getPackages();
      packages.add(new RNFirebaseMessagingPackage());
      packages.add(new RNFirebaseNotificationsPackage());
      return packages;
      }

AndroidManifest.xml

  <meta-data android:name="com.dieam.reactnativepushnotification.notification_channel_name" android:value="shwoop-adventure-app"/>
    <meta-data android:name="com.dieam.reactnativepushnotification.notification_channel_description" android:value="Notifications for the shwoop app"/>
    <meta-data android:name="com.dieam.reactnativepushnotification.notification_color" android:resource="@color/appPrimary"/>

    <meta-data android:name="com.google.firebase.messaging.default_notification_icon" android:resource="@drawable/logo1024"/>
    <meta-data android:name="com.google.firebase.messaging.default_notification_color" android:resource="@color/appPrimary"/>
    <meta-data android:name="com.google.firebase.messaging.default_notification_channel_id" android:value="@string/default_channel"/>

    <receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationPublisher" />
    <receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationBootEventReceiver">
      <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
      </intent-filter>
    </receiver>

    <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>

反应本机Firebase处理

import firebase, { Notification, RemoteMessage } from 'react-native-firebase';
import { Platform } from 'react-native';
import { logger } from '@react-native-community/cli-tools';


// add following code to an appropriate place.
// implement notifications as needed
async function setupNotifications(this: any) {
  // Build a channel
  const channel = new firebase.notifications.Android.Channel('test-channel', 'Test Channel', firebase.notifications.Android.Importance.Max)
    .setDescription('My apps test channel');

  // Create the channel
  firebase.notifications().android.createChannel(channel);
  if(Platform.OS === 'android') {
    try {
      const res = await firebase.messaging().requestPermission();
      const fcmToken = await firebase.messaging().getToken();

      if(fcmToken) {
        console.log('FCM Token: ', fcmToken);

        const enabled = await firebase.messaging().hasPermission();
        if(enabled)
          logger.log('FCM messaging has permission:' + enabled);

        else {
          try {
            await firebase.messaging().requestPermission();
            logger.log('FCM permission granted');
          }
          catch(error) {
            logger.log('FCM Permission Error', error);
          }
        }
        firebase.notifications().onNotificationDisplayed((notification: Notification) => {
          // Process your notification as required
          // ANDROID: Remote notifications do not contain the channel ID. You will have to specify this manually if you'd like to re-display the notification.
          console.log('Notification: ', notification);
        });

        this.notificationListener = firebase.notifications().onNotification((notification: Notification) => {
          console.log('Not displayed');
          console.log('Notification: ', notification);
        });

        console.log('Lets send a notification');

        const notification = new firebase.notifications.Notification()
          .setNotificationId('notificationId')
          .setTitle('My notification title')
          .setBody('My notification body')
          .android.setChannelId('test-channel')
          .setSound('default');

        firebase.notifications().displayNotification(notification);
        firebase.messaging().onMessage(() => console.log('Something happened.'));

      }
      else
        logger.log('FCM Token not available');

    }
    catch(e) {
      logger.log('Error initializing FCM', e);
    }
  }
}

setupNotifications();

问题:

没有通知到达设备。我正在使用Firebase Cloud Messaging控制台使用firebase.messaging().getToken();给出的FCM令牌将消息发送到我的设备。我整天待在这里,无法确定为什么没有通知到达设备。但是,此文件中生成的通知确实会显示并通过onNotificationDisplayed()函数运行。

我遵循了React Native Firebase的指南,似乎看不到我要去哪里。我怀疑它与RNFirebase无关,而与我的AndroidManifest无关,但是我所做的任何更改都无济于事。

0 个答案:

没有答案