Two or more Foreground Notification with Progress is replacing each other while updating its progress

前端 未结 4 1044
北恋
北恋 2021-01-22 04:15

I have a service that will run a upload task in foreground then showing a progress in the notification. Since a user may upload multiple times with different id request then the

4条回答
  •  南方客
    南方客 (楼主)
    2021-01-22 04:35

    You should pass different Id to get different notification.

    In Your Code:

    Use getId() method to get different Id for each notification:

    private static final AtomicInteger c = new AtomicInteger(0);
     public static int getID() {
            return c.incrementAndGet();
        }
    
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID_DEFAULT)
                .setSmallIcon(R.drawable.ic_file_upload_white_24dp)
                .setContentTitle(getString(R.string.app_name))
                .setContentText(caption)
                .setProgress(100, percentComplete, false)
                .setContentInfo(String.valueOf(percentComplete +"%"))
                .setOngoing(true)
                .setAutoCancel(false);
    
        NotificationManager manager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    
        manager.notify(getId(), builder.build());
    
        startForeground(getId(),builder.build()); // you need to pass different id here. Or i think this method is not there.
    

    For android O you need notification channel:

     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications",                      NotificationManager.IMPORTANCE_HIGH);
    
                // Configure the notification channel.
                notificationChannel.setDescription("Channel description");
                notificationChannel.enableLights(true);
                notificationChannel.setLightColor(Color.RED);
                notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
                notificationChannel.enableVibration(true);
                notificationChannel.setImportance(NotificationManager.IMPORTANCE_HIGH);
                notificationChannel.setSound(notification, Notification.AUDIO_ATTRIBUTES_DEFAULT);
                notificationManager.createNotificationChannel(notificationChannel);
            }
    
            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
    
            notificationBuilder.setAutoCancel(true)
                    .setDefaults(Notification.DEFAULT_ALL)
                    .setWhen(System.currentTimeMillis())
                    .setSound(notification)
                    .setContentIntent(pendingIntent)
                    .setContentText(message)
                    .setContentTitle(getResources().getString(R.string.app_name))
                    .setSmallIcon(R.drawable.ic_visualogyx_notification)
                    .setStyle(new NotificationCompat.BigTextStyle().bigText(message).setBigContentTitle(getResources().getString(R.string.app_name)))
                    .setTicker("Hearty365")
                    .setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.mipmap.ic_launcher_round));
    
            notificationManager.notify(getID(),notificationBuilder.build());
    

提交回复
热议问题