仅在1个通知中显示Firebase通知

时间:2017-05-15 13:54:42

标签: android firebase android-notifications firebase-cloud-messaging

我想只在一个通知中显示firbase通知,但每次我通过控制台或我的网络服务发送推送通知时,会自动在不同的通知中创建。即使我不在我的代码中使用通知构建器,并且android本身自动创建通知,最终我想在收件箱样式中创建通知并在他们下面显示所有消息或标题,感谢您的回复和您的帮助!

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;
}

}

1 个答案:

答案 0 :(得分:1)

您可以将tag用于notification有效负载:

  

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

     

如果未指定,则每个请求都会创建一个新通知。

     

如果已指定且已显示具有相同标记的通知,则新通知将替换通知抽屉中的现有通知。

但请注意,当您从Firebase控制台发送邮件时,您将无法设置tag的值。

或者你可以bundle the notifications自己。