我想在Android Wear上实现堆叠通知为此,我为每个"项目"创建了1个摘要通知和N个单独通知.我只想在手机上显示摘要.这是我的代码:
private void showNotifications() {
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
showNotification1(notificationManager);
showNotification2(notificationManager);
showGroupSummaryNotification(notificationManager);
}
private void showNotification1(NotificationManager notificationManager) {
showSingleNotification(notificationManager, "title 1", "message 1", 1);
}
private void showNotification2(NotificationManager notificationManager) {
showSingleNotification(notificationManager, "title 2", "message 2", 2);
}
protected void showSingleNotification(NotificationManager notificationManager,
String title,
String message,
int notificationId) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentTitle(title)
.setContentText(message)
.setSmallIcon(R.mipmap.ic_launcher)
.setGroupSummary(false)
.setGroup("group");
Notification notification = builder.build();
notificationManager.notify(notificationId, notification);
}
private void showGroupSummaryNotification(NotificationManager notificationManager) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this); …Run Code Online (Sandbox Code Playgroud) 我正在测试可堆叠通知(Stacking Notifications文章).
我发现在某些情况下,notify()在运行android 4.X KitKat的设备中调用后,通知未显示.
为了简单地解决问题,我创建了这个模拟通知的代码(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); …Run Code Online (Sandbox Code Playgroud)