仅在1个通知中显示Firebase通知 [英] Display Firebase notification in Only 1 Notification

查看:817
本文介绍了仅在1个通知中显示Firebase通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想在一个通知中显示firbase通知,但每当我通过控制台发送推送通知,或者在不同的通知中自动创建我的web服务。即使我没有在我的代码中使用通知生成器,并且android本身自动创建通知,我最终还是要在收件箱样式中创建通知,并在其下方显示所有消息或标题,谢谢您的回应和您的帮助!



pre $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ .LOG_TAG,remote Message:+ remoteMessage.toString());
Log.e(MyApplication.LOG_TAG,remote Message->(Data):+ remoteMessage.getData()。toString());
Log.i(MyApplication.LOG_TAG,FROM:+ remoteMessage.getFrom());
Log.i(MyApplication.LOG_TAG,MessageID:+ remoteMessage.getMessageId());
Log.i(MyApplication.LOG_TAG,MessageType:+ remoteMessage.getMessageType());
Log.i(MyApplication.LOG_TAG,To:+ remoteMessage.getTo());
Log.i(MyApplication.LOG_TAG,SentTime:+ remoteMessage.getSentTime());
Log.i(MyApplication.LOG_TAG,Ttl:+ remoteMessage.getTtl());
Log.i(MyApplication.LOG_TAG,CollapseKey:+ remoteMessage.getCollapseKey());
if(remoteMessage.getNotification()!= null){

fcm.setTag(remoteMessage.getNotification()。getTag());
fcm.setTitle(remoteMessage.getNotification()。getTitle());
fcm.setBody(remoteMessage.getNotification()。getBody());
fcm.setBodyLocalizationKey(remoteMessage.getNotification()。getBodyLocalizationKey());
fcm.setClickAction(remoteMessage.getNotification()。getClickAction());
fcm.setColor(remoteMessage.getNotification()。getColor());
fcm.setIcon(remoteMessage.getNotification()。getIcon());

Log.i(MyApplication.LOG_TAG,Notification:+ remoteMessage.getNotification());
Log.i(MyApplication.LOG_TAG,Sound:+ remoteMessage.getNotification()。getSound());
Log.i(MyApplication.LOG_TAG,BodyLocalizationKey:+ remoteMessage.getNotification()。getBodyLocalizationKey());
Log.i(MyApplication.LOG_TAG,ClickAction:+ remoteMessage.getNotification()。getClickAction());
Log.i(MyApplication.LOG_TAG,Color:+ remoteMessage.getNotification()。getColor());
Log.i(MyApplication.LOG_TAG,Icon:+ remoteMessage.getNotification()。getIcon());
Log.i(MyApplication.LOG_TAG,Tag:+ remoteMessage.getNotification()。getTag());
Log.i(MyApplication.LOG_TAG,Title:+ remoteMessage.getNotification()。getTitle());
Log.i(MyApplication.LOG_TAG,Body:+ remoteMessage.getNotification()。getBody());如果(remoteMessage.getData()。size()> 0){
Log.i(MyApplication.LOG_TAG,Message data:+ remoteMessage.getData()。toString(
) ());
Log.i(MyApplication.LOG_TAG,Have field =+ remoteMessage.getData()。containsKey(MessageID));
尝试{
JSONObject data = new JSONObject(remoteMessage.getData()。toString());
message = messageParser(data);
} catch(Exception e){
Log.e(MyApplication.LOG_TAG,Exception:+ e.getMessage());
}
MessageClass.LogConsole(message);
showNotification(message.getTitle)
}

public static void showNotification(String Message){
Intent intent = new Intent(MyApplication.context,MyBroadcastReceiver.class) ;
PendingIntent broadcastIntent = PendingIntent.getBroadcast(MyApplication.context,0,intent,0);
位图图标= BitmapFactory.decodeResource(MyApplication.context.getResources(),R.drawable.shna);
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
MyApplication.preferenceManager.setNotification(Message);
String oldNotification = MyApplication.preferenceManager.getNotifications();
Log.e(MyApplication.LOG_TAG,Notification:+ oldNotification);
列表< String> messages = Arrays.asList(oldNotification.split(\\ |));
for(int i = messages.size() - 1; i> = 0; i--){
inboxStyle.addLine(messages.get(i));
}
ShortcutBadger.applyCount(MyApplication.context,messages.size());
inboxStyle.setSummaryText(messages.size()+More);
notification = new NotificationCompat.Builder(MyApplication.context)
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(icon)
.setWhen(0)
.setCategory(Notification.CATEGORY_MESSAGE)
.setGroup(Notification.CATEGORY_MESSAGE)
.setAutoCancel(true)
.setDeleteIntent(broadcastIntent)
.setContentTitle()
.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE | Notification.DEFAULT_LIGHTS)
.setStyle(inboxStyle)
.build();

MyApplication.notificationManager.notify(0,notification);
}
public message messageParser(JSONObject data){
Message message = new Message();
尝试{
message.setTitle(data.getJSONObject(data)。getString(Title));
message.setDated(data.getJSONObject(data)。getString(Dated));
} catch(JSONException e){
Log.e(MyApplication.LOG_TAG,Json Exception:+ e.getMessage());
返回null;
} catch(Exception e){
Log.e(MyApplication.LOG_TAG,Exception:+ e.getMessage());
返回null;
}
返回消息;


$ b


解决方案

您可以使用 标记

code> 通知有效载荷:


标识符用于替换通知抽屉中的现有通知。



如果未指定,每个请求都会创建一个新通知。 >如果已指定,并且已经显示具有相同标记的通知,则新通知将替换通知抽屉中的现有通知。


不过,请注意,从Firebase控制台发送消息时,您将无法为标记设置值。



或者您可以自己将通知捆绑出来。


I want to show firbase notification in only one notification but when per time i send push notification by console or my web service automatic create in the Different notifications. even when I don't use notification builder in my code, and android itself automatic create notification, Eventually I WANT create notification in Inbox style and display all message or title below them, thanks for per your responding and your help!!!

enter code here@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    Log.e(MyApplication.LOG_TAG, "remote Message:" + remoteMessage.toString());
    Log.e(MyApplication.LOG_TAG, "remote Message->(Data):" + remoteMessage.getData().toString());
    Log.i(MyApplication.LOG_TAG, "FROM:" + remoteMessage.getFrom());
    Log.i(MyApplication.LOG_TAG, "MessageID:" + remoteMessage.getMessageId());
    Log.i(MyApplication.LOG_TAG, "MessageType:" + remoteMessage.getMessageType());
    Log.i(MyApplication.LOG_TAG, "To:" + remoteMessage.getTo());
    Log.i(MyApplication.LOG_TAG, "SentTime:" + remoteMessage.getSentTime());
    Log.i(MyApplication.LOG_TAG, "Ttl:" + remoteMessage.getTtl());
    Log.i(MyApplication.LOG_TAG, "CollapseKey:" + remoteMessage.getCollapseKey());
    if (remoteMessage.getNotification() != null) {

        fcm.setTag(remoteMessage.getNotification().getTag());
        fcm.setTitle(remoteMessage.getNotification().getTitle());
        fcm.setBody(remoteMessage.getNotification().getBody());
        fcm.setBodyLocalizationKey(remoteMessage.getNotification().getBodyLocalizationKey());
        fcm.setClickAction(remoteMessage.getNotification().getClickAction());
        fcm.setColor(remoteMessage.getNotification().getColor());
        fcm.setIcon(remoteMessage.getNotification().getIcon());

        Log.i(MyApplication.LOG_TAG, "Notification:" + remoteMessage.getNotification());
        Log.i(MyApplication.LOG_TAG, "Sound:" + remoteMessage.getNotification().getSound());
        Log.i(MyApplication.LOG_TAG, "BodyLocalizationKey:" + remoteMessage.getNotification().getBodyLocalizationKey());
        Log.i(MyApplication.LOG_TAG, "ClickAction:" + remoteMessage.getNotification().getClickAction());
        Log.i(MyApplication.LOG_TAG, "Color:" + remoteMessage.getNotification().getColor());
        Log.i(MyApplication.LOG_TAG, "Icon:" + remoteMessage.getNotification().getIcon());
        Log.i(MyApplication.LOG_TAG, "Tag:" + remoteMessage.getNotification().getTag());
        Log.i(MyApplication.LOG_TAG, "Title:" + remoteMessage.getNotification().getTitle());
        Log.i(MyApplication.LOG_TAG, "Body:" + remoteMessage.getNotification().getBody());
    }
    if (remoteMessage.getData().size() > 0) {
        Log.i(MyApplication.LOG_TAG, "Message data: " + remoteMessage.getData().toString());
        Log.i(MyApplication.LOG_TAG, "Have field=" + remoteMessage.getData().containsKey("MessageID"));
        try {
            JSONObject data = new JSONObject(remoteMessage.getData().toString());
            message = messageParser(data);
        } catch (Exception e) {
            Log.e(MyApplication.LOG_TAG, "Exception: " + e.getMessage());
        }
        MessageClass.LogConsole(message);
        showNotification(message.getTitle)
    }

    public static void showNotification(String Message) {
    Intent intent = new Intent(MyApplication.context, MyBroadcastReceiver.class);
    PendingIntent broadcastIntent = PendingIntent.getBroadcast(MyApplication.context, 0, intent, 0);
    Bitmap icon = BitmapFactory.decodeResource(MyApplication.context.getResources(), R.drawable.shna);
    NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
    MyApplication.preferenceManager.setNotification(Message);
    String oldNotification = MyApplication.preferenceManager.getNotifications();
    Log.e(MyApplication.LOG_TAG, "Notification:" + oldNotification);
    List<String> messages = Arrays.asList(oldNotification.split("\\|"));
    for (int i = messages.size() - 1; i >= 0; i--) {
        inboxStyle.addLine(messages.get(i));
    }
    ShortcutBadger.applyCount(MyApplication.context, messages.size());
    inboxStyle.setSummaryText(messages.size() + " More");
    notification = new NotificationCompat.Builder(MyApplication.context)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setLargeIcon(icon)
            .setWhen(0)
            .setCategory(Notification.CATEGORY_MESSAGE)
            .setGroup(Notification.CATEGORY_MESSAGE)
            .setAutoCancel(true)
            .setDeleteIntent(broadcastIntent)
            .setContentTitle("")
            .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE | Notification.DEFAULT_LIGHTS)
            .setStyle(inboxStyle)
            .build();

    MyApplication.notificationManager.notify(0, notification);
}
public Message messageParser(JSONObject data) {
    Message message = new Message();
    try {
        message.setTitle(data.getJSONObject("data").getString("Title"));
        message.setDated(data.getJSONObject("data").getString("Dated"));
    } catch (JSONException e) {
        Log.e(MyApplication.LOG_TAG, "Json Exception: " + e.getMessage());
        return null;
    } catch (Exception e) {
        Log.e(MyApplication.LOG_TAG, "Exception: " + e.getMessage());
        return null;
    }
    return message;
}

}

解决方案

You could use the tag for the notification payload:

Identifier used to replace existing notifications in the notification drawer.

If not specified, each request creates a new notification.

If specified and a notification with the same tag is already being shown, the new notification replaces the existing one in the notification drawer.

However, note that you won't be able to set the value for tag when you send the message from the Firebase Console.

Or you could just bundle the notifications yourself.

这篇关于仅在1个通知中显示Firebase通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆