TransWikia.com

Xamarin Android cancel background notification after a certain time

Stack Overflow Asked by RiccardoM on February 15, 2021

As part of my assignment I want to remove a notification that has been received but not been interacted with after a certain amount of time. This means if the notification is still in the notification tray after this amount of time, the app will delete it automatically.

For foreground notifications this wasn’t the issue, as I applied the following code:

    void SendNotification(RemoteMessage remotemessage)
    {
        var intent = new Intent(this, typeof(MainActivity));
        intent.AddFlags(ActivityFlags.ClearTop);
        var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot);
        long[] pattern = { 100, 100, 100, 100 };

        var notificationBuilder = new Notification.Builder(this)
            .SetVibrate(pattern)
            .SetSmallIcon(Resource.Drawable.mhu2)
            .SetContentTitle(remotemessage.GetNotification().Title)
            .SetContentText(remotemessage.GetNotification().Body)
            .SetAutoCancel(true)
            .SetContentIntent(pendingIntent);

        NotificationManager notificationManager =
                (NotificationManager)GetSystemService(Context.NotificationService);

        int id = 0;

        notificationManager.Notify(id, notificationBuilder.Build());

        Handler handler = new Handler(Looper.MainLooper);

        long delayInMilliseconds = 5000;

        handler.PostDelayed(new Runnable(() => notificationManager.Cancel(id)), delayInMilliseconds);
    }

When a notification is received, it will automatically be removed after 5 seconds (debugging purposes). However, as we all know, notifications are not handled the same depending on the state of the app.

This code works for foreground apps, but will never be run when the app is in the background or killed. So when the user receives a notification when the app was not opened or in the background, the notification will not be removed.

I’ve tried to look into this and saw partial solutions by executing code when overriding the OnDestroy/OnStop/OnPause state, but that still won’t help to remove the notification when the app was never opened.

Any suggestions are greatly appreciated!

One Answer

I just want to post a quick update as I've been able to solve this issue.

Android handles notification differently based on what is provided within the notification. I found out that if the notification is only build with datafields (so no notification body), the OnMessageReceived() will always be fired regardless of the state of the app. The timer that I realised will fire on foreground, background and app closed states. The only time this doesn't work is when the app is forced stopped, but in my context this won't cause issues.

Answered by RiccardoM on February 15, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP