0

Am Working on a timer based app where the notification shows up when the timer starts to run. I have set it as ongoing so that it cannot be cleared.

I have used cancelAll() method for some cases which works fine but when I force close the app, the notification still shows up and cannot be removed and tried to use the method in onDestroy() method still the problem prevails.

Here is my code and created the channel in another class :

public void sendNotif(){

    Intent resultIntent = new Intent(this, TimerActivity.class);


    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);


    not = new NotificationCompat.Builder(this,Notif.CHANNEL_ID)
            .setSmallIcon(R.drawable.curved_shape)
            .setContentTitle("Productivity Timer")
            .setContentText("Your Timer is Running")
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setCategory(NotificationCompat.CATEGORY_PROGRESS)
            .setOngoing(true)
            .setContentIntent(pendingIntent)
            .build();

    notificationManager.notify(1,not);

}
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
Harish V
  • 1
  • 1
  • 7

1 Answers1

0

I found this great solution once, I will retype it here

Since your application and the notification are handled in different threads, so killing your application won't kill the notification. The solution is to create a Service to kill notification, since services will restart themselves when the app is killed suddenly, you can use the automatic restart to kill the notification.

Create the service class

public class KillNotificationsService extends Service {

public class KillBinder extends Binder {
    public final Service service;

    public KillBinder(Service service) {
        this.service = service;
    }

}

public static int NOTIFICATION_ID = 666;
private NotificationManager mNM;
private final IBinder mBinder = new KillBinder(this);

@Override
public IBinder onBind(Intent intent) {
        return mBinder;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
        return Service.START_STICKY;
}
@Override
public void onCreate() {
        mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        mNM.cancel(NOTIFICATION_ID);
}
}

Add it to your manifest

<service android:name="KillNotificationsService"></service>

Always create the Service before fireing the notification, and use the static notificationid

ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className,
        IBinder binder) {
    ((KillBinder) binder).service.startService(new Intent(
            MainActivity.this, KillNotificationsService.class));
    Notification notification = new Notification(
            R.drawable.ic_launcher, "Text",
            System.currentTimeMillis());
    Intent notificationIntent = new Intent(MainActivity.this,
            Place.class);
    PendingIntent contentIntent = PendingIntent.getActivity(
            MainActivity.this, 0, notificationIntent, 0);
    notification.setLatestEventInfo(getApplicationContext(),
            "Text", "Text", contentIntent);
    NotificationManager mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    mNM.notify(KillNotificationsService.NOTIFICATION_ID,
            notification);
}

public void onServiceDisconnected(ComponentName className) {
}

};
 bindService(new Intent(MainActivity.this,
    KillNotificationsService.class), mConnection,
    Context.BIND_AUTO_CREATE);
Amine
  • 2,241
  • 2
  • 19
  • 41