Search code examples
javaandroidandroid-notificationswear-os

Display multiple notifications in WearOS


I want to display few notifications But I only see a one notification I do this ;

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());

}

And this is how I test display notification:

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

Solution

  • here is the issue notification id should be unique

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

    Please update it like below

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