Search code examples
androidfirebase-notifications

Auto Remove Firebase Notifications


I have a question, I have read both Make notification disappear after 5 minutes and Clearing notification after a few seconds , but I still don't understand the part where they call removing of the notification. I have a incoming firebase notification coming to my phone and If the user does not click on it,I want the notification removed/disappear automatically after 20 seconds. May I know how to implement it?

P.S I did read on the services as well. I am new to Java language and picking it up as I try a little demo. https://developer.android.com/guide/components/services.html

My codes below are crashing my app everytime i receive a notification. Any help will be appreciated.

Edited: Its fully functional. For anyone's reference

        private void removeNotification()
{
    long delayInMilliseconds = 20000; 
    final Timer timer = new Timer();
    timer.schedule(new TimerTask() {
        @Override
        public void run()
        {
            NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            manager.cancel(0);
            timer.cancel();
        }
    }, delayInMilliseconds, 1000);
}

Solution

  • Yeah, it is very easy. Where you get notification there add one handler if notification is not read by user then remove notification.

    @Override
    public void onMessageReceived(RemoteMessage message) {
    sendNotification(message.getData().toString);
    }
    

    add notification code

    private void sendNotification(String messageBody) {
            Intent intent = new Intent(this, MainActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
                    PendingIntent.FLAG_ONE_SHOT);
    
            Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentTitle("TEST NOTIFICATION")
                    .setContentText(messageBody)
                    .setAutoCancel(true)
                    .setSound(defaultSoundUri)
                    .setContentIntent(pendingIntent);
    
            NotificationManager notificationManager =
                    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            int id = 0;
            notificationManager.notify(id, notificationBuilder.build());
            removeNotification(id);
        } 
    

    cancel notification code.

    private void removeNotification(int id) {
    Handler handler = new Handler();
        long delayInMilliseconds = 20000;
        handler.postDelayed(new Runnable() {
            public void run() {
                notificationManager.cancel(id);
            }
        }, delayInMilliseconds);
    }