android - 如何对像whatsapp这样的android通知进行分组?

标签 android notifications stack google-cloud-messaging grouping

我不知道如何将两个或多个通知组合成一个并显示“您有两条新消息”之类的消息。

最佳答案

以下代码中需要注意的步骤。

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.

使用以下代码创建通知并将其分组。在按钮单击中包含该功能。

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());
        }
}

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

关于android - 如何对像whatsapp这样的android通知进行分组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33040737/

相关文章:

ios - APS registerForRemoteNotifications 控制默认警报样式

c++ - 粉碎堆栈 : overflow

java - 如何为带有堆栈的 Iterator 类设置构造函数?

assembly - Arm Assembly-带有4个以上参数的调用函数

android - 如何最好地实现共享一项服务的两项 Activity (带蓝牙连接)?

java - Android - 操作系统关闭后台应用程序后 fragment 导航问题

ios - Firebase 通知在 iOS 11 中不起作用

android - 无法查看 RecyclerView 中的条目

符号的 Android 键码

android - 如何找到启动状态栏通知的应用程序