【问题标题】:Notifications not stacking通知未堆叠
【发布时间】:2017-01-26 17:21:09
【问题描述】:

我正在尝试在用户打开活动时创建堆叠的本地通知。我看不到它们堆叠在一起。它们都单独出现。我提到了许多其他堆栈溢出问题,但没有一个有效。以下是我的代码:

public class NotificationGroupActivity extends Activity {

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

private void createNotifications() {

    final  String GROUP_KEY_EMAILS = "group_key_emails";
    NotificationManagerCompat notificationManager =
            NotificationManagerCompat.from(this);
    for(int i = 0; i<10;i++) {

// Build the notification, setting the group appropriately
        Notification notif = new NotificationCompat.Builder(this)
                .setContentTitle("New mail from " + i)
                .setContentText("SUBJECT " + i)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setGroup(GROUP_KEY_EMAILS)
                .setGroupSummary(true)
                .build();
// Issue the notification
        notificationManager.notify(i + 10, notif);
    }

}
}

【问题讨论】:

    标签: android notifications localnotification


    【解决方案1】:

    这里的问题是,您发出的每个通知都使用了不同的 ID(.notify(int, Notification) 方法的第一个参数)。

    正如documentation 所说:

    要在您发出通知后更新它,请更新或创建一个 NotificationCompat.Builder 对象,从中构建一个 Notification 对象,并使用您之前使用的相同 ID 发出该通知。

    所以你必须更换

    notificationManager.notify(i + 10, notif);
    

    与:

    notificationManager.notify(0 /* Or any constant*/, notif);
    

    考虑到这将替换旧通知,如果您想实现类似 Whatsapp 或 Inbox 的功能,您必须为堆叠通知提供自己的自定义布局

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-27
      相关资源
      最近更新 更多