'How to immediately clear notification

I'm creating a notification with a pendingIntent that opens CallActivity:

NotificationCompat.Builder(context, Constants.CALL_NOTIF_ID)
                .setSmallIcon(R.drawable.my_ic)
                .setOngoing(true)
                .setPriority(NotificationCompat.PRIORITY_MAX)
                .setSound(null)
                .setVibrate(LongArray(0))
                .setCategory(NotificationCompat.CATEGORY_CALL)
                .setContentIntent(myPendingIntent)
                .setStyle(NotificationCompat.DecoratedCustomViewStyle())

Im showing this notification while the call is active And clearing it when the call stops with this code:

notificationManager.cancel(Constants.CALL_NOTIF_ID)

Now when I call a device and immediately stop the call, notification appears but it takes 1-2 seconds to disappear. In this short time user can click on notification and can see the CallActivity.

Actually I can make an if check to close activity if there is no call but I don't want to open it in the first place. So how can I remove the notification immediately without animation (even with reflection)?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source