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



我只想在一个通知中显示 firbase 通知,但每次我通过控制台发送推送通知或我的 Web 服务时都会在不同的通知中自动创建。 即使我不在我的代码中使用通知生成器,并且 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;
}
}

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

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

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

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

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

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

相关内容

  • 没有找到相关文章

最新更新