How To Programmatically Clear/Cancel Notifications In Xamarin.forms

Introduction

This article demonstrates how to programmatically we can clear app notifications without clicking notifications from the notification tray in Xamarin.Forms.

Description

For example, if the Gmail app is getting some notifications, now the user can go into the application without clicking on the notification and the app should clear all app-related notifications from the notification tray.

Also, let's assume there will be a Notification Page in our application that helps the user to see all notifications. If the user navigates to this page, we have to implement functionality to clear all notifications from the notification trays.

Portable Class Library(PCL)

Step 1: 

Create an interface, IPushCancel, with the method declaration of CancelPush.

IPushCancel.cs

  1. using System;  
  2. namespace PushCancel {  
  3.     public interface IPushCancel {  
  4.         void CancelPush(int id);  
  5.     }  
  6. }  

Call to DependencyService

DependencyService.Get<IPushCancel>().CancelPush(Convert.ToInt32("Pass your NotificationID"));

We need to call the above dependency service in the page where we want to clear notifications on the notifications tray.

Xamarin.Android

Create a class, PushCancelService, and implement the IPushCancel method like below.

PushCancelService.cs 

  1. using System;
  2. using Xamarin.Forms;  
  3. using Android.Provider;  
  4. using PushCancel.Droid;  
  5. using Adnroid.Telephony;    
  6. using Java.Util;  
  7. using PushCancel;  
  8. using Android.App;  
  9. using Android.Support.V4.App;  
  10. using Android.Content;  
  11. [assembly: Dependency(typeof(PushCancelService))]  
  12. namespace PushCancel.Droid {  
  13.     public class PushCancelService: IPushCancel {  
  14.         public void CancelPush(int id) {            
  15.             var notificationManager = NotificationManagerCompat.From(Android.App.Application.Context);  
  16.             notificationManager.CancelAll(); 
  17.             CreateIntent(id) 
  18.         } 
  19.         private Intent CreateIntent(int id) {  
  20.             return new Intent(Android.App.Application.Context, typeof(AppGcmListenerService /*"Here we have to put GCM Listener class"*/ )).SetAction("LocalNotifierIntent" + id);  
  21.         }  
  22.     }  
  23. }  

Xamarin.iOS

In iOS also, create a class PushCancelService and implement the IPushCancel method like below.

PushCancelService.cs

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using Foundation;  
  6. using UIKit;  
  7. using Xamarin.Forms;  
  8. using PushCancel.iOS;  
  9. [assembly: Dependency(typeof(PushCancelService))]  
  10. namespace PushCancel.iOS {  
  11.     public class PushCancelService: IPushCancel {  
  12.         private const string NotificationKey = "LocalNotificationKey";  
  13.         public void CancelPush(int id) {  
  14.             UIApplication.SharedApplication.CancelAllLocalNotifications();  
  15.             var notifications = UIApplication.SharedApplication.ScheduledLocalNotifications;  
  16.             var notification = notifications.Where(n => n.UserInfo.ContainsKey(NSObject.FromObject(NotificationKey))).FirstOrDefault(n => n.UserInfo[NotificationKey].Equals(NSObject.FromObject(id)));  
  17.             UIApplication.SharedApplication.CancelAllLocalNotifications();  
  18.             if (notification != null) {  
  19.                 UIApplication.SharedApplication.CancelLocalNotification(notification);  
  20.                 UIApplication.SharedApplication.CancelAllLocalNotifications();  
  21.             }  
  22.         }  
  23.     }  
  24.