在 Android KitKat 中调用 setGroup() 时不显示通知
我正在测试可堆叠通知(堆叠通知文章)。
我检测到在某些情况下,在运行android 4.X KitKat的设备中调用后不会显示通知。notify()
为了简单地解决问题,我创建了这个代码,它模拟了一个通知(button1)和第二个带有摘要的通知(button2)
private final static int NOTIFICATION_ID_A=6;
private final static int NOTIFICATION_ID_B = 7;
private final static int NOTIFICATION_ID_SUMMARY = 8;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showNotif(NOTIFICATION_ID_A,false);
}
});
findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showNotif(NOTIFICATION_ID_B,false);
showNotif(NOTIFICATION_ID_SUMMARY,true);
}
});
}
private void showNotif(int notificationId,boolean groupSummary) {
CharSequence title="Title "+notificationId;
CharSequence message="Message "+notificationId;
NotificationCompat.Builder notifBuilder = new NotificationCompat.Builder(this);
notifBuilder.setSmallIcon(R.drawable.icon_notifications);
notifBuilder.setContentTitle(title);
notifBuilder.setContentText(message);
notifBuilder.setGroupSummary(groupSummary);
notifBuilder.setContentIntent(PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT));
notifBuilder.setGroup("group_" + 1);
NotificationManagerCompat.from(this).notify(notificationId, notifBuilder.build());
}
这个想法是首先按下按钮1,然后按下按钮2。它在android 5.0 +中效果很好,当点击第二个按钮时,它会显示第一个通知和摘要,但在Android 4.X中,button1不会显示任何内容。
错误在哪里?
谢谢