Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
当应用程序处于后台或在React native firebase android中关闭时,无法获取通知数据、正文_Android_Firebase_React Native_React Native Android - Fatal编程技术网

当应用程序处于后台或在React native firebase android中关闭时,无法获取通知数据、正文

当应用程序处于后台或在React native firebase android中关闭时,无法获取通知数据、正文,android,firebase,react-native,react-native-android,Android,Firebase,React Native,React Native Android,我在我的应用程序中使用react native firebase进行云消息更改+通知。当应用程序正在使用时,它可以获取所有的身体和数据,但当应用程序关闭或处于后台时,它无法获取任何内容 我试过无头JS,但也不起作用 当我点击通知并打开应用程序时,它会显示这个{“google.priority”:“high”} 提前通知 这是我的android mainfest <application android:name=".MainApplication" android:

我在我的应用程序中使用react native firebase进行云消息更改+通知。当应用程序正在使用时,它可以获取所有的身体和数据,但当应用程序关闭或处于后台时,它无法获取任何内容

我试过无头JS,但也不起作用

当我点击通知并打开应用程序时,它会显示这个
{“google.priority”:“high”}

提前通知

这是我的android mainfest

<application
      android:name=".MainApplication"
      android:label="@string/app_name"
      android:icon="@mipmap/ic_launcher"
      android:allowBackup="false"
      android:theme="@style/AppTheme"
      android:largeHeap="true">
      <meta-data
    android:name="com.google.firebase.messaging.default_notification_icon"
    android:resource="@drawable/ic_stat_ic_notification" />
      <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:launchMode="singleTop"
        android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
        android:windowSoftInputMode="adjustResize">

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
      </activity>
      <!---firebase -->
      <service android:name="io.invertase.firebase.messaging.RNFirebaseMessagingService">
    <intent-filter>
      <action android:name="com.google.firebase.MESSAGING_EVENT" />
    </intent-filter>
  </service>
  <!-- <meta-data
    android:name="com.google.firebase.messaging.default_notification_channel_id"
    android:value="@string/default_notification_channel_id"/> -->
   <service android:name="io.invertase.firebase.messaging.RNFirebaseBackgroundMessagingService" />
  <!---firebase end-->
      <activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />
    </application>
这是我的bgMessaging.js

import firebase from 'react-native-firebase';
import type { RemoteMessage } from 'react-native-firebase';
import type { Notification,NotificationOpen} from 'react-native-firebase';

export default async (message: RemoteMessage) => {

    const newNotification = new firebase.notifications.Notification()
            .android.setChannelId(message.data.channelId)
            .setNotificationId(message.messageId)
            .setTitle(message.data.title)
            .setBody(message.data.body)
            .setSound("default")
            .setData(message.Data)
            .android.setAutoCancel(true)
            .android.setSmallIcon('ic_notification')
            .android.setCategory(firebase.notifications.Android.Category.Alarm)

    // Build a channel
    const channelId = new firebase.notifications.Android.Channel(message.data.channelId, channelName, firebase.notifications.Android.Importance.Max);

    // Create the channel
    firebase.notifications().android.createChannel(channelId);
    firebase.notifications().displayNotification(newNotification)

    return Promise.resolve();

}

有两种类型的消息:

通知+数据
FCM将在后台处理的消息(因此您的代码将无法访问通知),并将在前台调用
onNotification

data only
消息在后台/关闭时调用headlessjs,在前台调用
onMessage

注意:如果删除
标题
正文
,则您的邮件将被归类为第二封邮件。此外,数据在第一类中是可选的

重述:

对于仅限数据的消息:
  • 前台应用程序:
    onMessage
    已触发

  • 后台应用程序/应用程序关闭:后台处理程序(HeadlessJS)

对于通知+数据消息:
  • 前台应用程序:
    onNotification
    已触发

  • 后台应用程序/应用程序关闭:
    onNotificationOpened
    在点击通知时触发


有关更多信息和iOS,请阅读官方文档和

,RN firebase似乎不支持Android上的此功能

请参阅下文:

不幸的是,在Android上,无法访问已打开远程通知的标题和正文。如果需要,可以使用远程通知的数据部分提供此信息


你好,你能检查一下这个问题吗@AliKazemkhanloo
import firebase from 'react-native-firebase';
import type { RemoteMessage } from 'react-native-firebase';
import type { Notification,NotificationOpen} from 'react-native-firebase';

export default async (message: RemoteMessage) => {

    const newNotification = new firebase.notifications.Notification()
            .android.setChannelId(message.data.channelId)
            .setNotificationId(message.messageId)
            .setTitle(message.data.title)
            .setBody(message.data.body)
            .setSound("default")
            .setData(message.Data)
            .android.setAutoCancel(true)
            .android.setSmallIcon('ic_notification')
            .android.setCategory(firebase.notifications.Android.Category.Alarm)

    // Build a channel
    const channelId = new firebase.notifications.Android.Channel(message.data.channelId, channelName, firebase.notifications.Android.Importance.Max);

    // Create the channel
    firebase.notifications().android.createChannel(channelId);
    firebase.notifications().displayNotification(newNotification)

    return Promise.resolve();

}