当群组只有一个通知时,如何取消群组(捆绑)通知?

时间:2018-09-17 13:06:20

标签: android notifications

以下是示例活动,具有创建捆绑软件通知和分组的代码。 当我单击上一个通知(剩余一个时)。组通知不会在使用上一个通知时消失。 strong文字

公共类MainActivity扩展了AppCompatActivity实现的View.OnClickListener {

Button btnBundleNotification, btnSingleNotification;
NotificationManager notificationManager;
int bundleNotificationId = 100;
int singleNotificationId = 100;
NotificationCompat.Builder summaryNotificationBuilder;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    btnBundleNotification = findViewById(R.id.btnBundleNotification);
    btnSingleNotification = findViewById(R.id.btnSingleNotification);
    btnBundleNotification.setOnClickListener(this);
    btnSingleNotification.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.btnBundleNotification:


            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
                NotificationChannel groupChannel = new NotificationChannel("bundle_channel_id", "bundle_channel_name", NotificationManager.IMPORTANCE_LOW);
                notificationManager.createNotificationChannel(groupChannel);
            }
            bundleNotificationId += 100;
            singleNotificationId = bundleNotificationId;
            String bundle_notification_id = "bundle_notification_" + bundleNotificationId;
            Intent resultIntent = new Intent(this, MainActivity.class);
            resultIntent.putExtra("notification", "Summary Notification Clicked");
            resultIntent.putExtra("notification_id", bundleNotificationId);
            resultIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
            PendingIntent resultPendingIntent = PendingIntent.getActivity(this, bundleNotificationId, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);


            summaryNotificationBuilder = new NotificationCompat.Builder(this, "bundle_channel_id")
                    .setGroup(bundle_notification_id)
                    .setGroupSummary(true)
                    .setContentTitle("Bundled Notification. " + bundleNotificationId)
                    .setContentText("Content Text for bundle notification")
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentIntent(resultPendingIntent);

            notificationManager.notify(bundleNotificationId, summaryNotificationBuilder.build());

            break;

        case R.id.btnSingleNotification:


            bundle_notification_id = "bundle_notification_" + bundleNotificationId;

            resultIntent = new Intent(this, MainActivity.class);
            resultIntent.putExtra("notification", "Summary Notification Clicked");
            resultIntent.putExtra("notification_id", bundleNotificationId);
            resultIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
            resultPendingIntent = PendingIntent.getActivity(this, bundleNotificationId, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);

            //We need to update the bundle notification every time a new notification comes up.
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
                if (notificationManager.getNotificationChannels().size() < 2) {
                    NotificationChannel groupChannel = new NotificationChannel("bundle_channel_id", "bundle_channel_name", NotificationManager.IMPORTANCE_LOW);
                    notificationManager.createNotificationChannel(groupChannel);
                    NotificationChannel channel = new NotificationChannel("channel_id", "channel_name", NotificationManager.IMPORTANCE_DEFAULT);
                    notificationManager.createNotificationChannel(channel);
                }
            }
            summaryNotificationBuilder = new NotificationCompat.Builder(this, "bundle_channel_id")
                    .setGroup(bundle_notification_id)
                    .setGroupSummary(true)
                    .setContentTitle("Bundled Notification " + bundleNotificationId)
                    .setContentText("Content Text for group summary")
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentIntent(resultPendingIntent);


            if (singleNotificationId == bundleNotificationId)
                singleNotificationId = bundleNotificationId + 1;
            else
                singleNotificationId++;

            resultIntent = new Intent(this, MainActivity.class);
            resultIntent.putExtra("notification", "Single notification clicked");
            resultIntent.putExtra("notification_id", singleNotificationId);
            resultIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
            resultPendingIntent = PendingIntent.getActivity(this, singleNotificationId, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);


            NotificationCompat.Builder notification = new NotificationCompat.Builder(this, "channel_id")
                    .setGroup(bundle_notification_id)
                    .setContentTitle("New Notification " + singleNotificationId)
                    .setContentText("Content for the notification")
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setGroupSummary(false)
                    .setContentIntent(resultPendingIntent);

            notificationManager.notify(singleNotificationId, notification.build());
            notificationManager.notify(bundleNotificationId, summaryNotificationBuilder.build());
            break;

    }
}

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);

    Bundle extras = intent.getExtras();
    if (extras != null) {
        int notification_id = extras.getInt("notification_id");
        Toast.makeText(getApplicationContext(), "Notification with ID " + notification_id + " is cancelled", Toast.LENGTH_LONG).show();
        notificationManager.cancel(notification_id);
    }
}

}

2 个答案:

答案 0 :(得分:0)

据我了解,您希望清除系统托盘中的所有通知。

您可以在onNewIntent()方法中添加以下内容

NotificationManager nMgr = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            nMgr.cancelAll();

希望它对您有用。

答案 1 :(得分:0)

摘要通知不会在使用该组的最后一个通知时自动清除。您必须自己手动清除摘要通知。

这意味着您必须跟踪应用创建的所有通知。

private int countForNotificationsInGroup = 0 ;

每次在该摘要通知下创建新通知时,都会增加该值。

然后在消耗每个通知时递减通知计数。当计数达到零时,清除数字通知。

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);

    Bundle extras = intent.getExtras();
    if (extras != null) {
        int notification_id = extras.getInt("notification_id");
        Toast.makeText(getApplicationContext(), "Notification with ID " + notification_id + " is cancelled", Toast.LENGTH_LONG).show();
        notificationManager.cancel(notification_id);

        countForNotificationsInGroup = countForNotificationsInGroup - 1;

        if(countForNotificationsInGroup==0)
        {
           //clear summary notification
           notificationManager.cancelAll();
        }
    }
}

您可能想了解有关正确处理捆绑的Android通知here的更多信息。