当我的应用在React Native Firebase中关闭时,推送通知不起作用

时间:2020-07-22 07:45:48

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

我的应用程序关闭时,我没有收到任何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",

2 个答案:

答案 0 :(得分:1)

您应该创建可以处理设备启动事件的接收器,然后创建连接到Firebase的后台服务。在这种情况下,即使应用程序被用户终止,服务也将起作用。

引导接收器示例:

public class MyReceiver extends BroadcastReceiver {

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

    }
}

答案 1 :(得分:0)

您要使用哪个模块进行推送通知? 推送通知是否可以在iOS上使用?