【问题标题】:Push notifications not working when my app is closed in react native firebase当我的应用程序在 react native firebase 中关闭时,推送通知不起作用
【发布时间】:2020-07-22 07:45:48
【问题描述】:

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

【问题讨论】:

  • 您的推送通知提供商是哪一个?云消息?亚马逊社交网络?您可以在提供程序上将优先级设置为 HIGH 吗?对于谷歌云消息,请尝试this link

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


【解决方案1】:

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

启动接收器示例:

public class MyReceiver extends BroadcastReceiver {

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

    }
}

【讨论】:

  • 原生反应如何?我把接收放在清单中。我认为它在 java android(native) 中,但在 react native 中是不同的
  • 我认为除了你的 react native 之外,你可以在 Java 中处理整个 Notification 部分,有很多教程。
【解决方案2】:

您使用哪个模块进行推送通知? 推送通知正在 iOS 上运行吗?

【讨论】:

  • 我只在安卓操作系统中测试...所以我更新了我的问题
  • 您只是尝试发送本地通知,而不是从服务器发送推送通知。对吗?
  • 我调用我的 api 进行 requset(现在在本地进行我的测试,不是在我的服务器上发布)...你是什么意思?
  • 您是从 Firebase 仪表板发送推送通知,还是从邮递员那里调用任何 Firebase API 来发送推送通知?
  • 我使用邮递员的任何 Firebase API 来发送推送通知,当我从 Firebase 仪表板发送推送通知时,我得到了它
猜你喜欢
  • 2022-01-13
  • 1970-01-01
  • 1970-01-01
  • 2016-11-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-08
  • 1970-01-01
相关资源
最近更新 更多