Android 通知未堆叠

Android 通知未堆叠,android,notifications,localnotification,Android,Notifications,Localnotification,我试图在用户打开活动时创建堆叠的本地通知。我看不到它们堆叠在一起。它们都单独出现。我提到了许多其他的堆栈溢出问题,但没有一个有效。下面是我的代码: public class NotificationGroupActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContent

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

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

}
}
公共类NotificationGroupActivity扩展活动{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
创建通知();
}
私有void createNotifications(){
最终字符串GROUP\u KEY\u EMAILS=“GROUP\u KEY\u EMAILS”;
通知经理Compat notificationManager=
通知管理者比较来自(本);

对于(int i=0;i而言,这里的问题是,对于您发出的每个通知,您都使用了不同的ID(
.notify(int,Notification)
方法的第一个参数)

正如政府所说:

要在发出通知后更新此通知,请更新或创建NotificationCompat.Builder对象,从中生成通知对象,并使用以前使用的相同ID发出通知

所以你必须更换

notificationManager.notify(i + 10, notif);
与:

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

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