Android 当应用程序处于后台时,如何显示firebase通知的自定义UI?
我使用这个类来显示从firebase控制台接收到的关于我自己的UI(RemoteView)的通知。当应用程序是前台时,这可以正常工作,但当应用程序是后台时,通知将以默认的设备样式显示。即使应用程序是前台或后台,我应该如何在自己的UI中显示通知Android 当应用程序处于后台时,如何显示firebase通知的自定义UI?,android,firebase,notifications,remoteview,Android,Firebase,Notifications,Remoteview,我使用这个类来显示从firebase控制台接收到的关于我自己的UI(RemoteView)的通知。当应用程序是前台时,这可以正常工作,但当应用程序是后台时,通知将以默认的设备样式显示。即使应用程序是前台或后台,我应该如何在自己的UI中显示通知 public class MyFirebaseMessagingService extends FirebaseMessagingService { private static final String TAG = "Mehdi";
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "Mehdi";
@Override
public void onMessageReceived(RemoteMessage remoteMessage)
{
super.onMessageReceived(remoteMessage);
if (remoteMessage.getNotification() != null)
{
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.payam_notification_layout);
contentView.setTextViewText(R.id.payam,remoteMessage.getNotification().getBody());
String channelId = "Default";
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this,channelId)
.setSmallIcon(R.drawable.status_icon_delivered)
.setLargeIcon(BitmapFactory.decodeResource( getResources(), R.mipmap.icon))
.setSound(Uri.parse("android.resource://" + getApplicationContext().getPackageName() + "/" + R.raw.notification))
.setCustomBigContentView(contentView)
.setContentTitle(remoteMessage.getNotification().getTitle())
.setContentText(remoteMessage.getNotification().getBody())
.setAutoCancel(true);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId, "Default channel", NotificationManager.IMPORTANCE_DEFAULT);
manager.createNotificationChannel(channel);
}
manager.notify(0, mBuilder.build());
}
}
}
注意:感谢您的正确回答,我可以使用此链接使用自己的UI发送通知,即使应用程序位于后台或前台:
实际上,我们在发送通知时使用了两种类型的有效负载 一个是通知有效负载,另一个是数据有效负载。 通知有效负载当您在前台时自动管理通知,他们从firebase服务调用onMessageReceived,但当您在后台时,他们不调用onMessageReceived 因此,出于解决方案的目的,只需在数据有效负载中发送数据,并删除通知有效负载,这样您就可以在每个状态下的onMessageReceived中获取通知,并管理该通知的UI 检查下面的示例
function sendFCMNotification($message,$id) {
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array (
'to' => $id,
'data' => array (
"body" => $message,
"title" => "Title Text"
)
);
$fields = json_encode ( $fields );
$headers = array (
'Authorization: key=' . "Legcy Key",
'Content-Type: application/json'
);
谢谢,你的意思是我应该使用一些服务器端服务来发送数据,而我不能使用firebase console来发送数据?@XFa firebase console不支持
数据
唯一有效负载。实现这一点最简单的方法是感谢您的回答,但这不是更改服务器端的好方法,并且对其他平台有一定影响,当我们从服务器接收到通知和数据时,是否有其他方法在后台显示自定义通知@AzinNilchi你是对的,我们不能编辑一个用于多个平台的服务器代码,但实际上没有我建议的其他直接解决方案,另一个你可以做一件事,当你要使用android时,只需在服务器上设置条件,然后更改其他put的代码即可。谢谢,我在这里再次询问了我的问题:,但似乎没有别的办法。