Stack notifications in Kitkat (API 19) using setGroup() not working

混江龙づ霸主 提交于 2019-12-03 13:40:11

The reason you are getting results as in http://developer.android.com/images/android-5.0/notifications/Summarise_Dont.png is because you set group summary to true on all your notifications. In the API docs it is described as: Set this notification to be the group summary for a group of notifications. This means that every notification will be treated as a summary, and thus will be displayed as a new notification.

To solve your problem, you can send a new summary every time you get a new notification. Note that I am not 100% sure that this is the best solution, but it solves the problem. I created a test application which adds notifications based on button clicks:

public class MyActivity extends Activity {

    private final static String GROUP_KEY_MESSAGES = "group_key_messages";
    public static int notificationId = 0;
    private NotificationManagerCompat manager;


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

        manager = NotificationManagerCompat.from(this);
        manager.cancelAll();
    }

    public void addNotification(View v) {
        notificationId++; // Increment ID

        Bitmap icon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);

        Intent viewIntent = new Intent(this, ShowNotificationActivity.class);
        viewIntent.putExtra("notificationId", notificationId); // Put the notificationId as a variable for debugging purposes

        PendingIntent viewPendingIntent = PendingIntent.getActivity(this, notificationId, viewIntent, 0); // Intent to next activity

        // Group notification that will be visible on the phone
        Notification summary = new NotificationCompat.Builder(this)
                .setAutoCancel(true)
                .setContentIntent(viewPendingIntent)
                .setContentTitle("New notifications")
                .setContentText("You have "+notificationId+" new messages")
                .setLargeIcon(icon)
                .setGroup(GROUP_KEY_MESSAGES)
                .setGroupSummary(true)
                .build();

        manager.cancelAll(); // Is this really needed?
        manager.notify(notificationId, summary); // Show this summary
    }
}

ShowNotificationActivity:

public class ShowNotificationActivity extends Activity {

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

        Intent intent = getIntent();
        int counter = intent.getIntExtra("COUNTER", -57);
        Log.d("COUNTER", ""+counter);

        TextView view = (TextView)(findViewById(R.id.textView));
        view.setText("You have just read "+counter+" messages");
        MyActivity.notificationId = 0; // Reset ID. Should not be done like this, but used to show functionality.
    }
}

The first activity MyActivity is used to handle notifications. I guess that this is mostly done in receivers though. A button in MyActivity triggers addNotification which pushes the new notification summary. Before adding the summary all old summaries are cancelled. when clicking on the summary you get to the startup activity, called ShowNotificationActivty where data is shown. If this was, for instance, an email app, MyActivity would be some sort of email receiver which saved the email to a database. The notificationId could be set to some id saved in the database. When the notifications are clicked, the email can be viewed based on notificationId.

I hope this helps :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!