Android 当应用程序位于firebase后台时,使推送通知显示为弹出窗口
我正在使用我的react本机应用程序,通过Firebase云消息向android用户设置推送通知。到目前为止,我主要遵循了这一点。我设法使推送通知显示在锁屏上,并在应用程序位于前台时处理它们。但当应用程序在后台时,我无法将通知显示为弹出窗口。它会出现在通知栏上,但不会像gmail或whatsapp的通知那样显示弹出窗口 我相信我的问题是我没有发送正确的参数。我使用的是firebase控制台,所以它不是很灵活。我如何(以编程方式)配置通知以在接收时显示为弹出窗口 编辑: 设置通知通道适用于较新的android设备-在android 8.1(API级别27)上测试 在旧设备上——在Android 6.0(API级别23)上测试——仍然不会出现提示通知。我使用aws sns控制台发送以下消息:Android 当应用程序位于firebase后台时,使推送通知显示为弹出窗口,android,firebase,react-native,push-notification,react-native-firebase,Android,Firebase,React Native,Push Notification,React Native Firebase,我正在使用我的react本机应用程序,通过Firebase云消息向android用户设置推送通知。到目前为止,我主要遵循了这一点。我设法使推送通知显示在锁屏上,并在应用程序位于前台时处理它们。但当应用程序在后台时,我无法将通知显示为弹出窗口。它会出现在通知栏上,但不会像gmail或whatsapp的通知那样显示弹出窗口 我相信我的问题是我没有发送正确的参数。我使用的是firebase控制台,所以它不是很灵活。我如何(以编程方式)配置通知以在接收时显示为弹出窗口 编辑: 设置通知通道适用于较新的a
{
优先级:“高”,
可用内容:正确,
通知:{
标题:“你好”,
正文:“只是测试”,
android_channel_id:“测试频道”,
声音:“默认”
},
数据:{title:'title',body:'body',sound:'default'}
}
我还使用Firebase控制台发送消息,将优先级设置为高,并启用声音,无论是否使用Android频道Id。这些都不起作用。通知以静默方式到达托盘栏。这表明了同样的问题,但指出的一个人对我不起作用。我没有经常编辑react本机库代码。我尝试了旧Android版本(前台)的部分问题,它使头部出现在前台,而不是后台,这是这里的预期行为
此外,对于使用react本机软件包(,)的许多人来说,这似乎是一个尚未解决的问题
所以,我想我应该重新表述我的问题。对于Android 7.1或更低版本(在6.0上测试):
要在像whatsApp这样的弹出窗口中显示通知,您应该将通知频道的重要性设置为
importance\u HIGH
从官方文件
发出声音并显示为提示通知
编辑:
对于运行棒棒糖的设备-->牛轧糖,您需要设置振动或铃声以使磁头正常工作。然而,这里有一个快速的黑客程序,它不需要振动许可就可以生成平视通知
感谢@Ismailaloui对这个问题的贡献。当系统托盘收到通知时,我无法以提示的形式通知节目,所以我不得不进行变通。我将展示我是如何使用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
}
对于较旧的设备,我必须进行变通。我没有使用通知消息,而是使用了数据消息,因此您可以在后台收听
首先创建一个新文件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();
}
在index.js文件中,添加:
import bgMessaging from './src/bgMessaging'; // <-- Import the file you just created
...
AppRegistry.registerHeadlessTask('RNFirebaseBackgroundMessage', () => bgMessaging);
对于react native firebase v6: 要创建频道,请将以下行添加到MainActivity.java
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.os.Build;
protected void onCreate(Bundle savedInstanceState) {
// any other code goes here
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel("500", "MainChannel", NotificationManager.IMPORTANCE_HIGH);
notificationChannel.setShowBadge(true);
notificationChannel.setDescription("Test Notifications");
notificationChannel.enableVibration(true);
notificationChannel.enableLights(true);
notificationChannel.setVibrationPattern(new long[]{400, 200, 400});
//notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(notificationChannel);
}
}
这是从服务器发送的消息
const message = {
notification: {
title: data.subject,
body: data.message,
},
data: {
id: data.id,
title: data.subject,
body: data.message,
origin: 'chat',
request: 'test'
},
android: {
priority: 'high',
notification: {
title: data.subject,
body: data.message,
sound: 'default',
priority: 'high',
channelId: '500'
}
},
token: "TOKEN"
};
//firebase admin
admin.messaging().send(message)
.then((response) => {
// Response is a message ID string.
console.log('Successfully sent message:', response);
})
.catch((error) => {
console.log('Error sending message:', error);
});
在GitHub的问题线程上找到了这个答案。所有的功劳都归于他们。
在android牛轧糖(v7)和派(v9)上测试,你真的在使用GCM吗?它已经被弃用了。您应该改用FCM。此外,FCM文档还解释了如何执行您所描述的操作。你看过那些文件了吗?我确实在用FCM。我已经把概念弄乱了。谢谢你的评论。我会在我的帖子中更正它。你是否查阅了FCM文档以确定它是否告诉你需要做什么?我找到了这个指定FCM消息属性的文件。不过,我并没有看到任何与show相关的属性是否在后台弹出。我还通过firebase和firebase react native docs检查了安装说明是否与教程中的一致。请检查我的回答谢谢您的回答。这适用于android 8.1。对于安卓6.0,通知仍然不会显示为提示。我已经编辑了我的问题,请看一看。为了将来的参考,是如何在react native和react native Firebase上创建通知通道的?我应该将此代码放在哪里?在我的客户端还是服务器上?因为在接收通知时,只有后台消息。代码的任何部分是否在接收时与仅通知消息交互?如果您打算在发送消息时将此代码包含在服务器上,那么firebase控制台上的“配置声音”启用优先级是否足够高?您应该将此代码放在移动端的通知生成器中!你能更具体一点吗?我已将您的代码放在notification builder上,可以在应用程序运行时使用。它对后台通知没有任何作用。我不太熟悉android代码。我确实看了里面,可以做修改,但我需要知道我在找什么。我使用的是react native firebase,因此会丢失很多库代码。
<application>
...
<service android:name="io.invertase.firebase.messaging.RNFirebaseBackgroundMessagingService" /> <!--Add this-->
...
</application>
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.os.Build;
protected void onCreate(Bundle savedInstanceState) {
// any other code goes here
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel("500", "MainChannel", NotificationManager.IMPORTANCE_HIGH);
notificationChannel.setShowBadge(true);
notificationChannel.setDescription("Test Notifications");
notificationChannel.enableVibration(true);
notificationChannel.enableLights(true);
notificationChannel.setVibrationPattern(new long[]{400, 200, 400});
//notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(notificationChannel);
}
}
const message = {
notification: {
title: data.subject,
body: data.message,
},
data: {
id: data.id,
title: data.subject,
body: data.message,
origin: 'chat',
request: 'test'
},
android: {
priority: 'high',
notification: {
title: data.subject,
body: data.message,
sound: 'default',
priority: 'high',
channelId: '500'
}
},
token: "TOKEN"
};
//firebase admin
admin.messaging().send(message)
.then((response) => {
// Response is a message ID string.
console.log('Successfully sent message:', response);
})
.catch((error) => {
console.log('Error sending message:', error);
});