Search code examples
javaandroidandroid-notificationscountdowntimer

Stop Notification from being destroyed when the app closes


I am making a android app which will allow the user to press a button and show a notification with a timer counting down for a certain amount of time. Although I have made the notification persistent so it cannot be dismissed, when the app closes the notification gets destroyed.

Is there any way to allow a notification to continue running once the app is closed and not get destroyed.

Here is the code for starting my notification and timer:

final NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "notifyLemubit")
                            .setSmallIcon(holder.img_timer.getImageAlpha())
                            .setContentTitle("Timer Running")
                            .setContentText("Time Until Your " + timer.getTimer_name() + " Tree has Fully Grown: " + timer.getTimer_duration_s())
                            .setOngoing(true)
                            .setPriority(NotificationCompat.PRIORITY_DEFAULT);

                    final NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(context);
                    notificationManagerCompat.notify(timer.getTimer_id(), builder.build());

                    new CountDownTimer(10 * ONE_SECOND, ONE_SECOND) {

                        @Override
                        public void onTick(long ms_until_done) {
                            builder.setContentText("Time Until Your " + timer.getTimer_name() + " Tree has Fully Grown: " + ms_until_done / ONE_SECOND);
                            notificationManagerCompat.notify(timer.getTimer_id(), builder.build());
                        }

                        @Override
                        public void onFinish() {
                            notificationManagerCompat.cancel(timer.getTimer_id());
                            final NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "notifyLemubit")
                                    .setSmallIcon(holder.img_timer.getImageAlpha())
                                    .setContentTitle("Timer Finished")
                                    .setContentText("Your " + timer.getTimer_name() + " is Fully Grown!")
                                    .setPriority(NotificationCompat.PRIORITY_DEFAULT);

                            final NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(context);
                            notificationManagerCompat.notify(timer.getTimer_id(), builder.build());
                        }
                    }.start();

Any help is appreciated, Thanks


Solution

  • The only way by far i know is using a Foreground service by extending the Service or IntentService class

    And inside your activity or adapter use this to start the service

    context.startService(Intent(context,PersistentNotificationService.class))
    

    For the service here use this one

    public class PersistentNotificationService extends Service {
    
        private final static int ONE_SECOND = 1000;
    
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            final NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "notifyLemubit")
                    .setSmallIcon(R.drawable.img_timer)
                    .setContentTitle("Timer Running")
                    .setContentText("Your title goes here")
                    .setOngoing(true)
                    .setPriority(NotificationCompat.PRIORITY_DEFAULT);
    
    
            new CountDownTimer(10 * ONE_SECOND, ONE_SECOND) {
    
                @Override
                public void onTick(long ms_until_done) {
                    // Whatever code you want here
                }
    
                @Override
                public void onFinish() {
                    // To cancel , just close the service
                    stopForeground(true);
                    stopSelf();
                }
            }.start();
    
            startForeground(2342, builder.build());
    
            return START_STICKY;
        }
    
        @Nullable
        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }
    }