当应用程序在 Firebase 后台运行时,使推送通知显示为弹出窗口

Vin*_*ius 4 android push-notification firebase react-native react-native-firebase

我正在使用 Firebase Cloud Messaging 使用 React Native 应用程序向 Android 用户设置推送通知。到目前为止,我主要遵循了本教程。我设法使推送通知显示在锁定屏幕上,并在应用程序位于前台时处理它们。但是,当应用程序在后台运行时,我无法将通知显示为弹出窗口。它出现在通知托盘上,但不会像 Gmail 或 Whatsapp 的通知那样显示弹出窗口。

我相信我的问题是我没有随消息发送正确的参数。我使用的是 firebase 控制台,所以它不是很灵活。如何配置(以编程方式)通知以在接收时弹出?

编辑:

设置通知通道适用于较新的 Android 设备 - 在 Android 8.1(API 级别 27)上进行了测试。

在较旧的设备上 - 在 Android 6.0(API 级别 23)上进行测试 - 仍然不会出现抬头通知。我使用 aws sns 控制台发送以下消息:

{ 
  priority: 'high',
  content_available: true,
  notification: { 
     title: 'hello',
     body: 'Just test',
     android_channel_id: 'test-channel',
     sound: 'default' 
  },
  data: { title: 'title', body: 'body', sound: 'default' }
}

Run Code Online (Sandbox Code Playgroud)

我还使用 Firebase 控制台设置“优先级高”和“启用声音”发送消息,无论是否带有 Android 通道 ID。这些都不起作用。通知以静默方式到达托盘栏。此讨论显示了相同的问题,但一个人指出的解决方案对我不起作用。我没有太多地编辑react-native库代码。我尝试了旧版 Android 版本(前台)的问题部分,它使抬头显示在前台,但不在后台,这是此处的预期行为。

此外,对于很多使用此 React Native 包的人来说,这似乎是一个未解决的问题(github问题github问题)。

所以,我想我应该重新表述我的问题。对于 Android 7.1 或更低版本(在 6.0 上测试):

  1. 设置priority='high' 和 notification.sound='default' 是否足以显示抬头通知?(根据我的研究应该是这样)

  2. 我是否需要对我的应用程序代码进行任何进一步的配置,以从默默地到达托盘栏上的通知到它作为提示出现?

Vin*_*ius 5

感谢@ismailalaoui 对这个问题的贡献。当系统托盘收到通知时,我无法将通知显示为提醒,因此我不得不采取解决办法。我将展示我是如何使用react-native-firebase 做到这一点的。

对于新的 Android 设备,您应该创建一个通知通道。在您的 App.js 中添加以下内容

componentDidMount(){
  ...
  const channel = new firebase.notifications.Android.Channel('test-channel', 'Test Channel', firebase.notifications.Android.Importance.Max).setDescription('My apps test channel'); //add this line

  firebase.notifications().android.createChannel(channel); //add this line
}
Run Code Online (Sandbox Code Playgroud)

对于较旧的设备,我必须采取解决办法。我没有使用通知消息,而是使用数据消息,因此您可以在后台收听。参见第 4 项

首先创建一个新文件bgMessaging.js:

import firebase from 'react-native-firebase';

export default async (message) => {
  // handle your message
  const notification = new firebase.notifications.Notification()
  .setNotificationId(message.messageId)
  .setTitle(message.data.title)
  .setBody(message.data.body)
  .android.setChannelId('test-channel')
  .android.setSmallIcon('ic_launcher')
  .android.setPriority(firebase.notifications.Android.Priority.Max)
  .setSound('default');

  await firebase.notifications().displayNotification(notification);
  console.log({message})
  return Promise.resolve();
}
Run Code Online (Sandbox Code Playgroud)

在您的 index.js 文件中,添加:

import bgMessaging from './src/bgMessaging'; // <-- Import the file you just created

...

AppRegistry.registerHeadlessTask('RNFirebaseBackgroundMessage', () => bgMessaging); 
Run Code Online (Sandbox Code Playgroud)

react-native-firebase 使用 Headless JS 来运行您在bgMessaging. 根据文档,您需要将服务添加到 AndroidManifest.xml。在 android/app/src/main/AndroidManifest.xml 添加:

<application>
  ...
  <service android:name="io.invertase.firebase.messaging.RNFirebaseBackgroundMessagingService" /> <!--Add this-->
  ...
</application>
Run Code Online (Sandbox Code Playgroud)