调用setGroup()
后,我无法让手表振动代码:
NotificationCompat.Builder notification = new NotificationCompat.Builder(context); notification.setGroup(GROUP_KEY_NOTIFY); notification.setVibrate(patternArray); NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context); notificationManager.notify(id, notification.build());
观察:
尝试:
由于
更新:解决方案
完成发送堆叠通知的示例代码
static boolean CHILD2 = true;
public static void postNotifications(Context context) {
NotificationTest test = new NotificationTest();
Notification[] notifications = test.buildNotifications(context);
for (int i = 0; i < notifications.length; i++) {
Notification not = notifications[i];
NotificationManagerCompat.from(context).notify(i, not);
}
}
private static class NotificationTest {
public Notification[] buildNotifications(Context context) {
// Summary
NotificationCompat.Builder summaryBuilder = new
NotificationCompat.Builder(context)
.setGroup(EXAMPLE_GROUP_KEY)
.setGroupSummary(true)
.setContentTitle("The title")
.setContentText("The text")
.setSmallIcon(R.mipmap.ic_launcher)
.setVibrate(new long[] {0, 1000, 50, 2000} );
// Child 1
NotificationCompat.Builder childBuilder1 = new
NotificationCompat.Builder(context)
.setContentTitle("child 1 title")
.setContentText("child 1 text")
.setSmallIcon(R.mipmap.ic_launcher)
.setLocalOnly(false)
.setGroup(EXAMPLE_GROUP_KEY);
// Child 2
if (CHILD2) {
NotificationCompat.Builder childBuilder2 = null;
childBuilder2 = new NotificationCompat.Builder(context)
.setContentTitle("child 2 title")
.setContentText("child 2 text")
.setSmallIcon(R.mipmap.ic_launcher);
return new Notification[] { summaryBuilder.build(), childBuilder1.build(), childBuilder2.build() };
}
return new Notification[] { summaryBuilder.build(), childBuilder1.build() };
}
}
}
我发现SDK中的示例有点难以理解,因为它使用了许多类来组合通知。我希望这可以解决需要做的事情。
我以前的代码有两个问题:
说明:
可能不直观,因为摘要通知的文本没有显示在手表上,但它使用摘要通知中的振动模式,因此将其设置为本地只能防止振动。