Thursday, 22 March 2018

How to programmatically clear notifications in Xamarin.Forms

Introduction

This article demonstrates how to programmatically clear app notifications without clicking notification from notification tray in Xamarin.Forms.
Description
For example, Gmail app getting some notifications, now user goes into the application without click on the notification and 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 notification. If the user navigates to this page, we have to implement functionality to clear all notification from notification tray.
Portable Class Library(PCL):
Step1: 

Create an interface IPushCancel having method declaration of CancelPush.


IPushCancel.cs



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

Call to DependencyService

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

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

Xamarin.Android:


Create a class PushCancelService and need to implement IPushCancel method like below.


PushCancelService.cs


  1. using Xamarin.Forms;      
  2. using Android.Provider;      
  3. using PushCancel.Droid;      
  4. using Android.Telephony;      
  5. using System;      
  6. using Java.Util;      
  7. using PushCancel;      
  8. using Android.App;      
  9. using Android.Support.V4.App;      
  10. using Android.Content;      
  11.       
  12. [assembly: Dependency(typeof(PushCancelService))]      
  13. namespace PushCancel.Droid      
  14. {      
  15.     public class PushCancelService : IPushCancel      
  16.     {      
  17.       
  18.         public void CancelPush(int id)      
  19.         {     
  20.             var notificationManager = NotificationManagerCompat.From(Android.App.Application.Context);      
  21.             notificationManager.CancelAll();      
  22.       
  23.         }   
  24.     }      
  25.    


Xamarin.iOS:


In iOS also, create a class PushCancelService and need to implement 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.       
  10. [assembly: Dependency(typeof(PushCancelService))]      
  11. namespace PushCancel.iOS      
  12. {      
  13.     public class PushCancelService : IPushCancel      
  14.     {      
  15.       
  16.         private const string NotificationKey = "LocalNotificationKey";      
  17.       
  18.         public void CancelPush(int id)      
  19.         {      
  20.             UIApplication.SharedApplication.CancelAllLocalNotifications();      
  21.             var notifications = UIApplication.SharedApplication.ScheduledLocalNotifications;      
  22.             var notification = notifications.Where(n => n.UserInfo.ContainsKey(NSObject.FromObject(NotificationKey)))      
  23.                 .FirstOrDefault(n => n.UserInfo[NotificationKey].Equals(NSObject.FromObject(id)));      
  24.             UIApplication.SharedApplication.CancelAllLocalNotifications();      
  25.             if (notification != null)      
  26.             {      
  27.                 UIApplication.SharedApplication.CancelLocalNotification(notification);      
  28.                 UIApplication.SharedApplication.CancelAllLocalNotifications();      
  29.             }      
  30.         }      
  31.     }      
  32. }    
  

No comments:

Post a Comment