繁体   English   中英

通知 android (FCM)

[英]Notification android (FCM)

我想在应用程序关闭时自定义通知布局并且通知到达时会显示默认通知,即使应用程序关闭,我也希望在其中显示自定义通知。

以下是我的 Firebase onMessageReceievd代码

@Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Log.d("remoteMessage",String.valueOf(remoteMessage.getFrom()));

        if (remoteMessage.getData().size() > 0) {
            Log.e("MyFirebaseMsgService", "Data Payload: " + remoteMessage.getData().toString());

            try {
                JSONObject json = new JSONObject(remoteMessage.getData().toString());
                sendPushNotificationData(json);
            } catch (Exception e) {
                Log.e("MyFirebaseMsgService", "Exception: " + e.getMessage());
            }
        }

        if (remoteMessage.getNotification() != null) {
            //Log.e(TAG, "Notification Body: " + remoteMessage.getNotification().getBody());
            sendPushNotification(String.valueOf(remoteMessage.getNotification().getBody()),String.valueOf(remoteMessage.getNotification().getTitle()));
        }
    }

我使用BitTextStyle()在通知中添加突出显示的文本。

return new NotificationCompat.Builder(context)
       .setSmallIcon(R.drawable.ic_mono)
       .setContentTitle(title)
       .setContentText(message)
       .setLargeIcon(icon)
       .setColor(ContextCompat.getColor(context, R.color.notification_color))
       .setStyle(new NotificationCompat.BigTextStyle().bigText(title))
       .setStyle(new NotificationCompat.BigTextStyle().bigText(message).setSummaryText("#hashtag"))
       .setShowWhen(true)
       .setAutoCancel(true);

Firebase 远程消息具有notificationdata字段。 根据此文档, Firebase remoteMessage.notification自动处理remoteMessage.notification并在您的应用关闭时将remoteMessage.data传递到 Intent Extras 中。 但是当您的应用程序处于活动状态时, remoteMessage会到达您的接收器并由它处理。 但是很少有依赖于remoteMessage状态的场景。 请参阅文档中的表格。 因此,如果您想在应用程序关闭时处理所有通知,则需要将所有数据放入remoteMessage.data字段。 在这种情况下,Firebase 会将远程消息直接传送到您的接收器,您可以在那里使用您的数据创建自定义布局。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM