【问题标题】:Fcm Notification not Coming when App is in Kill State in React Native当应用程序在 React Native 中处于终止状态时,Fcm 通知不会出现
【发布时间】:2018-03-26 10:58:32
【问题描述】:

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

谢谢。

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

【问题讨论】:

  • 你能分享你的代码吗?就好像应用程序处于终止状态一样,本机端应该捕获通知。检查 android manifest 是否正确设置了所有依赖项。或者您可以使用 android Logcat 进行调试。
  • 这里有一个很好的Android参考——stackoverflow.com/q/39504805/4625829

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


【解决方案1】:

我也遇到了同样的问题,收到fcm令牌后添加下面的代码解决了,

FCM.setBadgeNumber(0);

以上是适用于 iOS 的 Android,您需要在 Xcode 中打开后台通知。 在 Xcode 中选择您的项目并打开功能,然后查看是否打开了后台模式。参考截图

这是我使用的代码的 sn-p

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
        });
       }

    }
  });
};

【讨论】:

  • 非常感谢兄弟....@Shashank .....从过去 7 天开始搜索......干得好......干杯!
  • 你的意思是,通过上面的代码,我可以在 IOS 中接收推送通知并执行一些逻辑,例如 - 从服务器获取数据以更新 APP 等。即使 IOS 我也想这样做应用程序处于终止状态。我可以吗?目前我正在使用 react-native-firebase 并且无法在 IOS(应用程序终止状态)中实现这一点。请帮忙
  • 是的,您将在已终止状态和点击事件中收到通知,如果您有刷新应用程序所需的数据,您将在应用程序中收到回调
猜你喜欢
  • 2022-08-03
  • 2021-08-18
  • 1970-01-01
  • 2017-08-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-07
  • 1970-01-01
  • 2017-11-24
相关资源
最近更新 更多