How to group android notifications like whatsapp?

后端 未结 4 603
说谎
说谎 2020-12-02 17:19

I don´t know how to group two or more notifications into only one and show a message like \"You have two new messages\".

4条回答
  •  佛祖请我去吃肉
    2020-12-02 18:01

    Steps to be taken care from the below code.

    NotificationCompat.Builder:contains the UI specification and action information
    NotificationCompat.Builder.build() :used to create notification (Which returns Notification object)
    Notification.InboxStyle: used to group the notifications belongs to same ID
    NotificationManager.notify():to issue the notification.
    

    Use the below code to create notification and group it. Include the function in a button click.

    private final int NOTIFICATION_ID = 237;
    private static int value = 0;
    Notification.InboxStyle inboxStyle = new Notification.InboxStyle();
    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.push_notify_icon);
    public void buttonClicked(View v)
    {
            value ++;
            if(v.getId() == R.id.btnCreateNotify){
                NotificationManager nManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                Notification.Builder builder = new Notification.Builder(this);            
                builder.setContentTitle("Lanes");
                builder.setContentText("Notification from Lanes"+value);
                builder.setSmallIcon(R.drawable.ic_launcher);
                builder.setLargeIcon(bitmap);
                builder.setAutoCancel(true);
                inboxStyle.setBigContentTitle("Enter Content Text");
                inboxStyle.addLine("hi events "+value);
                builder.setStyle(inboxStyle);
                nManager.notify("App Name",NOTIFICATION_ID,builder.build());
            }
    }
    

    For separate notifications assign different NOTIFICATION_IDs..

提交回复
热议问题