在wearos中显示多个通知

dgtucam1  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(479)

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

  1. public void showNotification(int i){
  2. NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  3. String NOTIFICATION_CHANNEL_ID = "my_channel_id_01 " + i;
  4. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  5. NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications " + i, NotificationManager.IMPORTANCE_HIGH);
  6. notificationChannel.setDescription("Channel description " + i);
  7. notificationChannel.enableLights(true);
  8. notificationChannel.setLightColor(Color.RED);
  9. notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
  10. notificationChannel.enableVibration(true);
  11. notificationManager.createNotificationChannel(notificationChannel);
  12. }
  13. NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
  14. notificationBuilder.setAutoCancel(true)
  15. .setDefaults(Notification.DEFAULT_ALL)
  16. .setWhen(System.currentTimeMillis())
  17. .setSmallIcon(R.drawable.ic_launcher)
  18. .setTicker("Hearty365")
  19. .setVibrate(new long[]{0, 1000, 500, 1000})
  20. // .setPriority(Notification.PRIORITY_MAX)
  21. .setContentTitle("Default notification")
  22. .setContentText("Lorem ipsum dolor sit amet, consectetur adipiscing elit.")
  23. .setContentInfo("Info");
  24. notificationManager.notify(/*notification id*/1, notificationBuilder.build());
  25. }

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

  1. for(int i=0 ;i <2 ; i++){
  2. showNotification(i);
  3. }
yyyllmsg

yyyllmsg1#

以下是问题通知id应该是唯一的

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

请更新如下

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

相关问题