简体   繁体   English

通知 android (FCM)

[英]Notification android (FCM)

I want to customize notification layout when app closed and notification arrives a default notification is shown where I want a custom notification to be displayed even if app the closed.我想在应用程序关闭时自定义通知布局并且通知到达时会显示默认通知,即使应用程序关闭,我也希望在其中显示自定义通知。

The following is my code for Firebase onMessageReceievd以下是我的 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()));
        }
    }

I used BitTextStyle() to add highlighted text in notification.我使用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 Remote message has notification and data fields. Firebase 远程消息具有notificationdata字段。 According to this documentation Firebase handles remoteMessage.notification automatically and pass remoteMessage.data into intent extras when your app is closed.根据此文档, Firebase remoteMessage.notification自动处理remoteMessage.notification并在您的应用关闭时将remoteMessage.data传递到 Intent Extras 中。 But when your app is active remoteMessage comes to your receiver and handled by it.但是当您的应用程序处于活动状态时, remoteMessage会到达您的接收器并由它处理。 But there are few scenarios which depends on remoteMessage state.但是很少有依赖于remoteMessage状态的场景。 See the table in documentation.请参阅文档中的表格。 So, if you want to handle all notification even when your app is closed, you need to put all your data into remoteMessage.data field.因此,如果您想在应用程序关闭时处理所有通知,则需要将所有数据放入remoteMessage.data字段。 In this case Firebase will deliver remote message directly into your receiver and there you can create custom layout with your data.在这种情况下,Firebase 会将远程消息直接传送到您的接收器,您可以在那里使用您的数据创建自定义布局。

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

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