推送通知未捆绑

时间:2019-05-27 08:24:55

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

以下是我用于处理我的应用程序上的通知的代码。如您所见,使用flag变量会收到不同的通知,这些通知的处理方式不同。我的问题是我想捆绑我的通知,但是每次收到新通知时,旧的通知都会被覆盖,即,尽管使用setGroup()setGroupSummary(),但一次只能显示一个通知。可能是什么问题,我该如何解决?

private int notificationId = 1;
private String GROUP_KEY_CHAT = "com.example.CHAT";
private static final String CHANNEL_ID = "com.example.gummy.pushnotification";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    Map<String, String> data = remoteMessage.getData();
    String title = data.get("title");
    String body = data.get("body");
    String flag = data.get("flag"); // notification flag to categorize type of notification
    String receiverId = data.get("receiverId");
    String receiverName = data.get("receiverName");

    Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    Intent intent = null;
    assert flag != null;
    switch (flag) {
        case "2":
            intent = new Intent(getApplicationContext(), ViewVisitors.class);
            break;
        case "4": // reliever 1 notification
            intent = new Intent(getApplicationContext(), PendingRelieverRequests.class);
            break;
        case "5": // reliever 2 notification
            intent = new Intent(getApplicationContext(), Reliever2PendingRequests.class);
            break;
        case "6": // reliever 3 notification
            intent = new Intent(getApplicationContext(), Reliever3PendingRequests.class);
            break;
        case "7": // approval notification
            intent = new Intent(getApplicationContext(), EmployeeMainActivity.class);
            intent.setAction("LeaveHistory");
        case "8":
            intent = new Intent(getApplicationContext(), Chat.class);
            intent.putExtra(Constants.DISPLAY_NAME, receiverName);
            intent.putExtra(Constants.UID, receiverId);
        default:
            intent = new Intent(getApplicationContext(), LoginActivity.class);
            break;
    }
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
            | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 1410,
            intent, PendingIntent.FLAG_ONE_SHOT);

    NotificationCompat.Builder notificationBuilder = new
            NotificationCompat.Builder(this, CHANNEL_ID)
            .setSmallIcon(R.drawable.ic_notification) // small icon
            .setContentTitle(title) // title
            .setStyle(new NotificationCompat.BigTextStyle().bigText(body)) // set body to be expandable
            .setAutoCancel(true) // remove notification on tap
            .setGroup(GROUP_KEY_CHAT)
            .setGroupSummary(true)
            .setPriority(NotificationCompat.PRIORITY_MAX) // priority
            .setCategory(NotificationCompat.CATEGORY_MESSAGE) // system wide category
            .setContentIntent(pendingIntent); // set tap action

    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);

    notificationBuilder.setSound(uri); // set to default notification sound
    notificationManager.notify(notificationId++, notificationBuilder.build());

}

谢谢。

0 个答案:

没有答案