Java 如何在Android上用自己的通知替换firebase通知?
我的Android应用程序获得firebase通知。我需要本地化这些通知,这取决于客户端的应用程序语言,而不是服务器端 如果应用程序位于前台,我使用Java 如何在Android上用自己的通知替换firebase通知?,java,android,firebase,push-notification,android-broadcastreceiver,Java,Android,Firebase,Push Notification,Android Broadcastreceiver,我的Android应用程序获得firebase通知。我需要本地化这些通知,这取决于客户端的应用程序语言,而不是服务器端 如果应用程序位于前台,我使用FirebaseMessagingService中的onMessageReceived()并推送我自己的本地化通知。但若应用程序位于后台,则不会调用onMessageReceived() 在这种情况下,我使用我自己的扩展类BroadcastReceiveronReceive(上下文,意图)方法捕获通知,我将其本地化并推送。一切都很顺利,但最后我得到了
FirebaseMessagingService
中的onMessageReceived()
并推送我自己的本地化通知。但若应用程序位于后台,则不会调用onMessageReceived()
在这种情况下,我使用我自己的扩展类BroadcastReceiver
onReceive(上下文,意图)
方法捕获通知,我将其本地化并推送。一切都很顺利,但最后我得到了两个推送通知:我自己的本地化和firebase
我怎样才能摆脱这个firebase通知而只获得我自己的通知
public class FirebaseDataReceiver extends BroadcastReceiver {
Context context;
PendingIntent pendingIntent;
public void onReceive(Context context, Intent intent) {
this.context = context;
Bundle dataBundle = intent.getExtras();
String title = "";
String body = "";
String type = "";
String objectId = "";
if (dataBundle != null) {
type = dataBundle.getString("type");
objectId = dataBundle.getString("objectId");
title = NotificationUtils.getNotificationTitle(context, dataBundle);
body = NotificationUtils.getNotificationBody(context, dataBundle);
}
Intent newIntent = new Intent(context, TutorialActivity_.class);
newIntent.putExtra("target", "notification");
newIntent.putExtra("type", type);
newIntent.putExtra("objectId", objectId);
newIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
pendingIntent = PendingIntent.getActivity(context,
0,
newIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new Notification.Builder(context)
.setContentTitle(title)
.setContentText(body)
.setPriority(Notification.PRIORITY_HIGH)
.setDefaults(Notification.DEFAULT_ALL)
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.setSmallIcon(R.drawable.splash_mini)
.build();
deleteLastNotification();
NotificationManagerCompat.from(context).notify(0, notification);
}
}
您应该真正使用来自服务器的数据通知。普通消息通知无法实现您所寻找的这种行为。请在此处查看文档: 因此,您从服务器向Firebase发出的请求应如下所示:
{
"message":{
"token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"data":{
"Nick" : "Mario",
"body" : "great match!",
"Room" : "PortugalVSDenmark"
}
}
}
而不是:
{
"message":{
"token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification":{
"title":"Portugal vs. Denmark",
"body":"great match!"
}
}
}
请使用数据消息而不是通知消息来接收
onMessageReceived()
中的数据,请参阅此“我使用数据消息”。我唯一需要的是不显示firebase推送通知这些更改如何帮助我不显示firebase通知而只显示我的通知?或者当应用程序位于后台时,FirebaseMessagingService的onMessageReceived()将开始调用?问题是,当应用程序位于后台时,onMessageReceived()现在不调用。所以firebase不顾我的意愿推送它的通知,因为消息类型通知和数据类型通知的行为不同。在某些情况下,消息类型的通知由Android系统托盘处理idd