如何分组android通知,如whatsapp? [英] How to group android notifications like whatsapp?

查看:295
本文介绍了如何分组android通知,如whatsapp?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何将两个或更多的通知分组到一个,并显示一条消息,比如您有两条新消息。

解决方案

  NotificationCompat.Builder:包含UI规范和操作信息
NotificationCompat.Builder.build():用于创建通知(返回Notification对象)
Notification.InboxStyle:用于将通知分组属于同一个ID
NotificationManager.notify():发布通知。

使用下面的代码创建通知并对其进行分组。

  private final int NOTIFICATION_ID = 237; 
private static int value = 0;
Notification.InboxStyle inboxStyle = new Notification.InboxStyle();
位图位图= 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(来自泳道的通知+值);
builder.setSmallIcon(R.drawable.ic_launcher);
builder.setLargeIcon(bitmap);
builder.setAutoCancel(true);
inboxStyle.setBigContentTitle(输入内容文本);
inboxStyle.addLine(hi events+ value);
builder.setStyle(inboxStyle);
nManager.notify(App Name,NOTIFICATION_ID,builder.build());


$ / code>

对于单独的通知分配不同的NOTIFICATION_ID ..


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

解决方案

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..

这篇关于如何分组android通知,如whatsapp?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆