Creating notification when app is closed

70 Views Asked by At

I am using the onDestroy method, which I have learned is not always called when app is closed. And this has created a problem for me, where I would like to have the notification pop up every time the app closed. Is there a better way to do this? By the way, onStop and onPause are not options for me, because my app runs as a background service. Interestingly, my onDestroy method works everytime my background service is running, but whenever it is turned off, and just the app interface is open, my onDestroy never is called. This seems weird because I thought that onDestroy isn't called if the device is lacking resources, and running my service would be taking up more resources than not. Idk. Any help on this would be greatly appreciated!

`@Override
public void onDestroy(){
    super.onDestroy();

    Log.d("asdasd","asdasdasdasdasd");
    if(notif.isChecked()) {
        Intent intent1 = new Intent(this, MainActivity.class);
        PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent1, 0);
        Notification noti = new Notification.Builder(this)
                .setContentTitle("S-Dimmer")
                .setContentText(getResources().getString(R.string.sDimmerStopped))
                .setSmallIcon(R.drawable.ic_action_name1)
                .setContentIntent(pIntent).getNotification();
        noti.flags = Notification.FLAG_NO_CLEAR;
        NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        nm.notify(0, noti);
        doas.isRunning = false;
    }
}`
3

There are 3 best solutions below

0
On BEST ANSWER

This method is not always called, so you should try from background service, as there's also some destruction process alert. So please try to do what ever you want there in the services.

Please check out this ..

onDestroy() may or may not be called on any given activity or service. The general rule is that either onDestroy() is called, or your process is terminated, or your code crashed.

You don't. Your process may be terminated for any reason, at any time, by the user or by the OS. You may or may not be called with onDestroy() when that occurs. While you may be able to improve your success rate a little via onTaskRemoved() on a Service, this itself has proven unreliable, and it will not cover all scenarios.

1
On
onTaskRemoved()
{
if(notif.isChecked()) {
        Intent intent1 = new Intent(this, MainActivity.class);
        PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent1, 0);
        Notification noti = new Notification.Builder(this)
                .setContentTitle("S-Dimmer")
                .setContentText(getResources().getString(R.string.sDimmerStopped))
                .setSmallIcon(R.drawable.ic_action_name1)
                .setContentIntent(pIntent).getNotification();
        noti.flags = Notification.FLAG_NO_CLEAR;
        NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        nm.notify(0, noti);
        doas.isRunning = false;
    } 
}
1
On

you are doing it wrong way. In onDestroy method all other stuffs should implemented before super.onDestory()

try this

@Override
public void onDestroy(){


    Log.d("asdasd","asdasdasdasdasd");
    if(notif.isChecked()) {
        Intent intent1 = new Intent(this, MainActivity.class);
        PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent1, 0);
        Notification noti = new Notification.Builder(this)
                .setContentTitle("S-Dimmer")
                .setContentText(getResources().getString(R.string.sDimmerStopped))
                .setSmallIcon(R.drawable.ic_action_name1)
                .setContentIntent(pIntent).getNotification();
        noti.flags = Notification.FLAG_NO_CLEAR;
        NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        nm.notify(0, noti);
        doas.isRunning = false;
    }
    super.onDestroy();
}`