Updating message of existing notification

I made a class dedicated for starting a notification from the service to make it a foreground service. Now, I would like to periodically update message displayed on the notification without raising any alert (like sound). How could I do it?

class ForegroundServiceNotification
{
    private String CHANNEL_ID;
    private Service service;

    ForegroundServiceNotification(Service service)
    {
        this.service = service;

        setupChannel();

        Intent notificationIntent = new Intent(service, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(service, 0, notificationIntent, 0);

        Notification notification = new NotificationCompat.Builder(service, CHANNEL_ID)
            .setContentTitle("Some initial title")
            .setContentText("Some initial text")
            .setSmallIcon(R.drawable.ic_launcher_foreground)
            .setContentIntent(pendingIntent)
            .build();

        service.startForeground(1, notification);
    }

    private void setupChannel()
    {
        CHANNEL_ID = service.getResources().getString(R.string.app_name);

        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
            return;

        NotificationManager manager = service.getSystemService(NotificationManager.class);
        if (manager == null)
            return;

        NotificationChannel serviceChannel = new NotificationChannel(CHANNEL_ID, CHANNEL_ID, NotificationManager.IMPORTANCE_DEFAULT);
        serviceChannel.setSound(null, null);
        serviceChannel.enableVibration(false);
        serviceChannel.setShowBadge(false);
        manager.createNotificationChannel(serviceChannel);
    }

    public void update(String title, String text)
    {
        // HOW CAN I IMPLEMENT IT?
        // ...
    }
}

1 answer

  • answered 2020-03-26 06:55 no one special

    I found a complete solution. Below example works even if user closed existing notification. Also Android O is supported.

    import android.app.Notification;
    import android.app.NotificationChannel;
    import android.app.NotificationManager;
    import android.app.PendingIntent;
    import android.app.Service;
    import android.content.Context;
    import android.content.Intent;
    import android.os.Build;
    import android.service.notification.StatusBarNotification;
    import android.support.v4.app.NotificationCompat;
    
    class ForegroundServiceNotification
    {
        private final int NOTIFICATION_ID = 11794;
        private String CHANNEL_ID;
        private Service service;
        private NotificationManager notificationManager;
    
        ForegroundServiceNotification(Service service)
        {
            this.service = service;
            notificationManager = (NotificationManager) service.getSystemService(Context.NOTIFICATION_SERVICE);
    
            setupChannel();
    
            update("Some title", "Some text");
        }
    
        void update(String title, String text)
        {
            boolean isNotificationVisible = false;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && notificationManager != null) {
    
                StatusBarNotification[] notifications = notificationManager.getActiveNotifications();
                for (StatusBarNotification notification : notifications)
                    if (notification.getId() == NOTIFICATION_ID) {
                        isNotificationVisible = true;
                        break;
                    }
            }
    
            Notification notification = buildNotification(title, text);
            if (isNotificationVisible)
                notificationManager.notify(NOTIFICATION_ID, notification);
            else
                service.startForeground(NOTIFICATION_ID, notification);
        }
    
        private void setupChannel()
        {
            CHANNEL_ID = service.getResources().getString(R.string.app_name);
    
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O || notificationManager == null)
                return;
    
            NotificationChannel serviceChannel = new NotificationChannel(
                CHANNEL_ID, CHANNEL_ID, NotificationManager.IMPORTANCE_DEFAULT);
            serviceChannel.setSound(null, null);
            serviceChannel.enableVibration(false);
            serviceChannel.setShowBadge(false);
            serviceChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
            notificationManager.createNotificationChannel(serviceChannel);
        }
    
        private Notification buildNotification(String title, String text)
        {
            Intent intent = new Intent(service.getApplicationContext(), MainActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            PendingIntent pendingIntent = PendingIntent.getActivity(
                service, 0, intent, 0);
    
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
                return new Notification.Builder(service, CHANNEL_ID)
                    .setContentTitle(title)
                    .setContentText(text)
                    .setSmallIcon(R.drawable.ic_launcher_foreground)
                    .setContentIntent(pendingIntent)
                    .setOnlyAlertOnce(true)
                    .setOngoing(true)
                    .build();
    
            return new NotificationCompat.Builder(service, CHANNEL_ID)
                .setContentTitle(title)
                .setContentText(text)
                .setSmallIcon(R.drawable.ic_launcher_foreground)
                .setContentIntent(pendingIntent)
                .setOnlyAlertOnce(true)
                .setOngoing(true)
                .build();
        }
    }