【问题标题】:Display multiple notifications in WearOS在 WearOS 中显示多个通知
【发布时间】:2020-01-24 07:05:52
【问题描述】:

我想显示几个通知但我只看到一个通知我这样做;

public void showNotification(int i){

    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    String NOTIFICATION_CHANNEL_ID = "my_channel_id_01 " + i;

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications " +  i, NotificationManager.IMPORTANCE_HIGH);

        notificationChannel.setDescription("Channel description " + i);
        notificationChannel.enableLights(true);
        notificationChannel.setLightColor(Color.RED);
        notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
        notificationChannel.enableVibration(true);
        notificationManager.createNotificationChannel(notificationChannel);
    }


    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);

    notificationBuilder.setAutoCancel(true)
            .setDefaults(Notification.DEFAULT_ALL)
            .setWhen(System.currentTimeMillis())
            .setSmallIcon(R.drawable.ic_launcher)
            .setTicker("Hearty365")
            .setVibrate(new long[]{0, 1000, 500, 1000})
            //     .setPriority(Notification.PRIORITY_MAX)
            .setContentTitle("Default notification")
            .setContentText("Lorem ipsum dolor sit amet, consectetur adipiscing elit.")
            .setContentInfo("Info");

    notificationManager.notify(/*notification id*/1, notificationBuilder.build());

}

这就是我测试显示通知的方式:

for(int i=0 ;i <2 ; i++){
    showNotification(i);
}

【问题讨论】:

  • 你检查我的答案了吗?它是否按您的预期工作。

标签: java android android-notifications wear-os


【解决方案1】:

这里是问题通知 id 应该是唯一的

 notificationManager.notify(/*notification id*/1, notificationBuilder.build());

请像下面这样更新它

 notificationManager.notify(/*notification id*/(int)(Math.random() * 100), notificationBuilder.build());

【讨论】:

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