通过Firebase Cloud Messing显示大视图通知



在firebase云消息文档中,没有任何关于具有大视图/扩展布局的通知。

当应用程序为背景时,我应该如何显示大视图通知?根据此常见问题,在FirebaseMessagingServiceonMessageReceived中创建自定义通知似乎是不可能的:

当您的应用在后台时,在系统托盘中显示通知消息,并且未调用OnMessageCeeperive。对于带有数据有效载荷的通知消息,通知消息在系统托盘中显示,并且可以从用户点击通知时启动的意图中随附的数据。

发送您要使用数据对象看到的通知。您可以将所需的所有内容基本地放入数据对象中,并始终以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);
    }
}

最新更新