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
- using System;
- namespace PushCancel {
- public interface IPushCancel {
- void CancelPush(int id);
- }
- }
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
- using System;
- using Xamarin.Forms;
- using Android.Provider;
- using PushCancel.Droid;
- using Adnroid.Telephony;
- using Java.Util;
- using PushCancel;
- using Android.App;
- using Android.Support.V4.App;
- using Android.Content;
- [assembly: Dependency(typeof(PushCancelService))]
- namespace PushCancel.Droid {
- public class PushCancelService: IPushCancel {
- public void CancelPush(int id) {
- var notificationManager = NotificationManagerCompat.From(Android.App.Application.Context);
- notificationManager.CancelAll();
- CreateIntent(id)
- }
- private Intent CreateIntent(int id) {
- return new Intent(Android.App.Application.Context, typeof(AppGcmListenerService )).SetAction("LocalNotifierIntent" + id);
- }
- }
- }
Xamarin.iOS
In iOS also, create a class PushCancelService and implement the IPushCancel method like below.
PushCancelService.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Foundation;
- using UIKit;
- using Xamarin.Forms;
- using PushCancel.iOS;
- [assembly: Dependency(typeof(PushCancelService))]
- namespace PushCancel.iOS {
- public class PushCancelService: IPushCancel {
- private const string NotificationKey = "LocalNotificationKey";
- public void CancelPush(int id) {
- UIApplication.SharedApplication.CancelAllLocalNotifications();
- var notifications = UIApplication.SharedApplication.ScheduledLocalNotifications;
- var notification = notifications.Where(n => n.UserInfo.ContainsKey(NSObject.FromObject(NotificationKey))).FirstOrDefault(n => n.UserInfo[NotificationKey].Equals(NSObject.FromObject(id)));
- UIApplication.SharedApplication.CancelAllLocalNotifications();
- if (notification != null) {
- UIApplication.SharedApplication.CancelLocalNotification(notification);
- UIApplication.SharedApplication.CancelAllLocalNotifications();
- }
- }
- }
- }