当应用程序在React Native中处于Kill状态时,Fcm通知不会出现

时间:2018-03-26 10:58:32

标签: firebase react-native firebase-cloud-messaging react-native-fcm

当从FCM发送,即反应原生的Firebase控制台时,它很容易进入iOS和Android,但是当我从管理员或后端发送通知时,当应用程序处于终止状态时,它不会出现。 我正在使用react-native-fcm。

Thanx。

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

<uses-sdk
    android:minSdkVersion="16"
    android:targetSdkVersion="22" />

<application
  android:name=".MainApplication"
  android:allowBackup="true"
  android:label="@string/app_name"
  android:icon="@mipmap/brainbg"
  android:theme="@style/AppTheme">
  <activity
    android:name=".MainActivity"
    android:label="@string/app_name"
    android:launchMode="singleTop"
    android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
    android:windowSoftInputMode="adjustResize"
    android:screenOrientation="portrait"  >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
  </activity>
  <activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />


  <service android:name="com.evollu.react.fcm.MessagingService" android:enabled="true" android:exported="true">
    <intent-filter>
     <action android:name="com.google.firebase.MESSAGING_EVENT"/>
    </intent-filter>
  </service>

  <service android:name="com.evollu.react.fcm.InstanceIdService" android:exported="false">
    <intent-filter>
      <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
    </intent-filter>
  </service>


  <receiver android:name="com.evollu.react.fcm.FIRLocalMessagingPublisher"/>
    <receiver android:enabled="true" android:exported="true"  android:name="com.evollu.react.fcm.FIRSystemBootEventReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"/>
            <action android:name="android.intent.action.QUICKBOOT_POWERON"/>
            <action android:name="com.htc.intent.action.QUICKBOOT_POWERON"/>
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </receiver>

</application>

1 个答案:

答案 0 :(得分:1)

我遇到了同样的问题,通过在收到fcm令牌后添加以下代码解决了这个问题,

FCM.setBadgeNumber(0);

以上是针对Android的iOS,您需要在Xcode中打开背景通知。enter image description here在Xcode中选择您的项目并打开功能,看看是否有背景模式。请参阅屏幕截图

以下是我使用的代码片段

export const MyNotificationReceiver = props => {
  FCM.getInitialNotification().then(notif => {
  });

  FCM.setBadgeNumber(0); //Add this for background notification

  this.notificationListener = FCM.on(FCMEvent.Notification, async notif => {
    var notification_messgae = "";
    try {
      notification_messgae = notif.default;
    } catch (error) {
      notification_messgae = JSON.stringify(notif);
    }
    if (osDetection) {
      switch (notif._notificationType) {
        case NotificationType.Remote:
          notif.finish(RemoteNotificationResult.NewData) //other types available: RemoteNotificationResult.NewData, RemoteNotificationResult.ResultFailed
          break;
        case NotificationType.NotificationResponse:
          notif.finish();
          break;
        case NotificationType.WillPresent:
          notif.finish(WillPresentNotificationResult.All) //other types available: WillPresentNotificationResult.None
          break;
      }
    }
    if (!osDetection()) {
      if(notif.default)
      {
        FCM.presentLocalNotification({
          body: notif.default,
          priority: "high",
          show_in_foreground: true,
          large_icon: "noti_icon",                           // Android only
          icon: "noti_icon",                                // as FCM payload, you can relace this with custom icon you put in mipmap
          android_actions: JSON.stringify([{
            id: "view",
            title: 'view'
          }, {
            id: "dismiss",
            title: 'dismiss'
          }]) // for android, take syntax similar to ios's. only buttons are supported
        });
       }

    }
  });
};
相关问题