React Native Firebase应用程序关闭后推送通知无法工作。

3

当我的应用程序处于关闭状态时,我没有收到FCM通知,在前台和打开状态下我可以收到通知,但在后台状态下无法接收通知。我认为我的代码都没有问题,但仍然无法接收通知。

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      package="com.sample_project">

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

<application
  android:usesCleartextTraffic="true"
  tools:targetApi="28"
  android:name=".MainApplication"
  android:label="@string/app_name"
  android:icon="@mipmap/ic_launcher"
  android:roundIcon="@mipmap/ic_launcher_round"
  android:allowBackup="false"
  android:theme="@style/AppTheme"
>


    <meta-data
            android:name="com.google.firebase.messaging.default_notification_channel_id"
            android:value="@string/default_notification_channel_id"/>

    <meta-data android:name="com.google.firebase.messaging.default_notification_icon"
               android:resource="@mipmap/ic_launcher"/>

    <receiver android:name="io.invertase.firebase.notifications.RNFirebaseNotificationReceiver"/>
    <receiver android:enabled="true" android:exported="true"  android:name="io.invertase.firebase.notifications.RNFirebaseNotificationsRebootReceiver">
        <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>

    <receiver
            android:name="com.google.firebase.iid.FirebaseInstanceIdReceiver"
            android:exported="true"
            android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />

            <category android:name="com.infuy.toshisanapp" />
        </intent-filter>
    </receiver>


    <service android:name="io.invertase.firebase.notifications.RNFirebaseBackgroundNotificationActionsService"/>

    <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.RNFirebaseBackgroundMessagingService" />

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



    <activity
    android:name=".MainActivity"
    android:label="@string/app_name"
    android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
    android:windowSoftInputMode="adjustResize"
    android:screenOrientation="portrait"
    android:launchMode="singleTop"

    >
    <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" />
</application>

在索引文件中,我放置了以下内容:

import {AppRegistry} from 'react-native';
import App from './App';
import {name as appName} from './app.json';
import {firebaseBackgroundMessage} from "./res/Global/FcmManager";
AppRegistry.registerComponent(appName, () => App);
AppRegistry.registerHeadlessTask('RNFirebaseBackgroundMessage', () => firebaseBackgroundMessage); 

在 firebaseBackgroundMessage 中,我有这段代码:

export async function firebaseBackgroundMessage(message: RemoteMessage) {
console.log("firebaseBackgroundMessage::::::::::::::::::::::::::::::");
let notif=message['data'];
console.log(JSON.stringify(message));
const groupNotificationId = 'test';
const body = 'Chats list';
const smallIcon = 'ic_launcher';
const channel = new firebase.notifications.Android.Channel(
"reminder", // channelId
              "Reminders Channel", // channel name
              firebase.notifications.Android.Importance.High // channel importance
            ).setDescription("Used for getting reminder notification"); // channel description
            firebase.notifications().android.createChannel(channel);

            const groupNotification = new firebase.notifications.Notification()
              .setNotificationId(groupNotificationId)
              .setSubtitle(body)
              .setTitle(Data.data.title)
              .setBody(Data.data.text)
            groupNotification
              .android.setGroup(groupNotificationId)
              .android.setGroupSummary(true)
              .android.setChannelId('reminder')
              .android.setSmallIcon(smallIcon)
              .android.setAutoCancel(true);
            firebase.notifications().displayNotification(groupNotification);
return Promise.resolve();

请帮帮我,我已经尝试了很多示例代码和权限设置,但都没有生效,让我感到很累。

这是package.json文件的示例:

"react": "16.9.0",
"react-native": "0.61.5",
"react-native-firebase": "^5.5.6",
"react-navigation": "^4.1.0",
"react-navigation-stack": "^2.1.0",

你们使用哪个推送通知提供商?Cloud Messaging?Amazon SNS?你们能否将提供商的优先级设置为HIGH?对于Google Cloud Messaging,请尝试使用此链接:https://firebase.google.com/docs/cloud-messaging/concept-options#setting-the-priority-of-a-message - Filipe Merker
嗨,萨米拉, 你是如何解决这个问题的?我现在也面临着同样的问题。 - mr.volatile
2个回答

1
你应该创建一个接收器来处理设备启动事件,然后创建一个连接到Firebase的后台服务。在这种情况下,即使用户终止了应用程序,服务仍将运行。
启动接收器示例:
public class MyReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // do your coed

    }
}

在React Native中如何在清单文件中添加接收(receiver)?我认为这是在Java Android(本地)中完成的,但在React Native中有所不同。 - Samira Gheibipour
我认为你可以在Java部分处理通知的整个部分,除了React Native之外,有很多教程可以参考。 - Farhad

0
你正在使用哪个模块来进行推送通知? 推送通知在iOS上工作吗?

我只在Android操作系统中进行测试...因此我更新了我的问题。 - Samira Gheibipour
你只是尝试发送本地通知而不是从服务器发送推送通知。对吗? - Yashvant
我调用我的API进行请求(目前仅在本地测试,尚未发布到我的服务器)...你是什么意思? - Samira Gheibipour
你是从Firebase控制面板发送推送通知,还是通过Postman调用Firebase API发送推送通知呢? - Yashvant
我使用Postman从任何Firebase API发送推送通知,当我从Firebase仪表板发送推送通知时,我收到了它。 - Samira Gheibipour

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接