在Firebase Cloud Messaging documentation中,没有提及大视图/扩展布局的通知。
当应用为后台时,我应该如何显示大视图通知?根据此faq,似乎无法在FirebaseMessagingService
onMessageReceived
中创建自定义通知:
当您的应用在后台时,通知消息会显示在系统托盘中,并且不会调用onMessageReceived。对于具有数据有效负载的通知消息,通知消息显示在系统托盘中,并且可以从用户点击通知时启动的意图中检索通知消息中包含的数据。
答案 0 :(得分:1)
使用数据对象发送您要查看的通知。您可以基本将所需的所有内容放在数据对象中,并始终使用onMessageReceived
方法接收它。这是一个例子。
public class AppFireBaseMessagingService extends FirebaseMessagingService {
private final static int REQUEST_CODE = 1;
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Map<String, String> data = remoteMessage.getData();
if (data == null) return;
if (data.containsKey("title") && data.containsKey("message")) {
showNotification(data.get("title"), data.get("message"));
}
}
private void showNotification(String title, String body) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setContentTitle(title)
.setSmallIcon(R.drawable.notification_icon);
if (body != null && !body.isEmpty()) {
builder.setStyle(new NotificationCompat.BigTextStyle().bigText(body));
builder.setContentText(body);
}
Intent intent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
builder.setAutoCancel(true);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification n = builder.build();
n.defaults = Notification.DEFAULT_ALL;
notificationManager.notify(0, n);
}
}