如何在应用程序处于后台时为 firebase 通知显示自定义 UI?
How to show custom UI for firebase notification when the app is in background?
我使用这个 class 来显示我自己的 UI (RemoteViews) 的通知,它是从 firebase 控制台收到的。这在应用程序处于前台时工作正常,但是当应用程序处于后台时,通知以 device.What 的默认样式显示,即使应用程序是前台或后台,我是否应该以我自己的 UI 显示通知?
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());
}
}
}
注意:感谢您的正确答案,我可以使用此 link 发送带有我自己的 UI 的通知,即使应用程序在后台或前景:
http://www.androiddeft.com/2017/11/18/push-notification-android-firebase-php/
实际上我们在发送通知时使用了两种类型的负载,
一个是Notification Payload,另一个是Data Payload。
当您在前台时,通知负载会自动管理通知,他们会从 firebase 服务调用 onMessageReceived,但当您在后台时,他们不会调用 onMessageReceived,
因此,为了解决这个问题,只需在 Data 有效负载中发送数据并删除 Notification 有效负载,这样您就可以在每个 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'
);
我使用这个 class 来显示我自己的 UI (RemoteViews) 的通知,它是从 firebase 控制台收到的。这在应用程序处于前台时工作正常,但是当应用程序处于后台时,通知以 device.What 的默认样式显示,即使应用程序是前台或后台,我是否应该以我自己的 UI 显示通知?
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());
}
}
}
注意:感谢您的正确答案,我可以使用此 link 发送带有我自己的 UI 的通知,即使应用程序在后台或前景: http://www.androiddeft.com/2017/11/18/push-notification-android-firebase-php/
实际上我们在发送通知时使用了两种类型的负载,
一个是Notification Payload,另一个是Data Payload。 当您在前台时,通知负载会自动管理通知,他们会从 firebase 服务调用 onMessageReceived,但当您在后台时,他们不会调用 onMessageReceived,
因此,为了解决这个问题,只需在 Data 有效负载中发送数据并删除 Notification 有效负载,这样您就可以在每个 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'
);