Introduction
This article demonstrates how to programmatically clear app notifications without clicking notification from notification tray in Xamarin.Forms.
This article demonstrates how to programmatically clear app notifications without clicking notification from notification tray in Xamarin.Forms.
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.
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
- 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 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
- using Xamarin.Forms;
- using Android.Provider;
- using PushCancel.Droid;
- using Android.Telephony;
- using System;
- 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();
- }
- }
- }
Xamarin.iOS:
In iOS also, create a class PushCancelService and need to implement 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();
- }
- }
- }
- }
No comments:
Post a Comment