【问题标题】:How to group android notifications like whatsapp?如何对像whatsapp这样的android通知进行分组?
【发布时间】:2016-01-07 13:09:27
【问题描述】:

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

【问题讨论】:

标签: android notifications stack google-cloud-messaging grouping


【解决方案1】:

您可以使用 setGroup 方法将所有通知堆叠到一个组中,并将您的 groupId 字符串作为参数传递。

builer.setGroup("组 ID 字符串") ;

NotificationManager nManager = (NotificationManager) 
getSystemService(NOTIFICATION_SERVICE);
Notification.Builder builder = new Notification.Builder(this);            
builder.setContentTitle("Lanes");
builder.setGroup("GROUP_ID_STRING");
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());

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 我已经为 FcmMessagingService 类中的 builder 编写了代码。在内部类onMessageRecieved中。我能够详细接收所有通知,但它看起来像是从不同应用程序接收的通知。
    【解决方案3】:

    对于完整的逻辑,请考虑检查我的答案。我使用了具有共享偏好和广播接收器的逻辑,因为我需要将每个用户消息分组为单个消息并查看活动通知。仅通过定位 api 级别 23 你可以得到主动通知,它根本没有帮助我。所以我决定写一些轻微的逻辑。如果你愿意,可以在这里查看。

    https://stackoverflow.com/a/38079241/6466619

    【讨论】:

      【解决方案4】:

      您需要创建通知,以便可以通过调用NotificationManager.notify(ID, notification) 使用通知 ID 对其进行更新。

      需要创建以下步骤来更新通知:

      1. 更新或创建NotificationCompat.Builder 对象
      2. 从中构建一个 Notification 对象
      3. 使用您之前使用的相同 ID 发出通知

      取自 android 开发者文档的示例:

      mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
      
      // Sets an ID for the notification, so it can be updated
      int notifyID = 1;
      
      mNotifyBuilder = new NotificationCompat.Builder(this)
          .setContentTitle("New Message")
          .setContentText("You've received new messages.")
          .setSmallIcon(R.drawable.ic_notify_status)
      numMessages = 0;
      
      // Start of a loop that processes data and then notifies the user
      ...
      mNotifyBuilder.setContentText(currentText).setNumber(++numMessages);
      
      // Because the ID remains unchanged, the existing notification is updated.
      mNotificationManager.notify(notifyID, mNotifyBuilder.build());
      ...
      

      另请参阅有关 Stacking Notifications 的 Android 文档 https://developer.android.com/training/wearables/notifications/stacks.html

      【讨论】:

      • 对。我发现 API 令人困惑: NotificationCompatBuilder.setGroup() 方法来自 Wearable API。它的目的是管理可穿戴设备上的通知组——尽管它也会影响手机。但大多数人都在手机上寻找类似 Gmail-Whastapp 的群组。答案是:您必须自己使用通知 ID 来更新您的通知。而且您必须在每个通知之间保存您的数据,以便能够计算“摘要”通知 - 这再次取决于您构建和发送相同的 ID 以替换之前的“非摘要”通知。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-22
      • 1970-01-01
      • 1970-01-01
      • 2023-01-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多