Skip to content

2. Usage 10.0.0 Xamarin.Forms

Elvin (Tharindu) Thudugala edited this page Jun 29, 2023 · 3 revisions

Setup

Usage

Show local notification

if (await LocalNotificationCenter.Current.AreNotificationsEnabled() == false)
{
    await LocalNotificationCenter.Current.RequestNotificationPermission();
}

var notification = new NotificationRequest
{
    NotificationId = 100,
    Title = "Test",
    Description = "Test Description",
    ReturningData = "Dummy data", // Returning data when tapped on notification.
    Schedule = 
    {
        NotifyTime = DateTime.Now.AddSeconds(30) // Used for Scheduling local notification, if not specified notification will show immediately.
    }
};
await LocalNotificationCenter.Current.Show(notification);

Receive local notification tap event

public partial class App : Application
{
	public App()
	{
		InitializeComponent();

		// Local Notification tap event listener
		LocalNotificationCenter.Current.NotificationActionTapped += OnNotificationActionTapped;

		MainPage = new MainPage();
	}
	
	private void OnNotificationActionTapped(NotificationEventArgs e)
    	{
           if (e.IsDismissed)
           {
               // your code goes here
               return;
           }
	   if (e.IsTapped)
           {
               // your code goes here
               return;
           }
           // if Notification Action are setup
           switch (e.ActionId)
           {
               // your code goes here
           }
	}
}

Platform Specific Notes

Android

The project should target Android framework 12.0+

image

Add the assembly-based permission:

[assembly: UsesPermission(Manifest.Permission.WakeLock)]
[assembly: UsesPermission(Manifest.Permission.ReceiveBootCompleted)]
[assembly: UsesPermission(Manifest.Permission.Vibrate)]

[assembly: UsesPermission("android.permission.SCHEDULE_EXACT_ALARM")]
[assembly: UsesPermission("android.permission.POST_NOTIFICATIONS")]

Or Update the Android Manifest:

<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.VIBRATE" />

<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />

Setup

To receive the Local Notification, tap event. Include the following code in the OnNewIntent() method of MainActivity:

public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
	protected override void OnCreate(Bundle savedInstanceState)
	{
	    	.....
                LocalNotificationCenter.MainActivity = this;

	    	// Must create a Notification Channel when API >= 26
        	// you can create multiple Notification Channels with different names.
        	LocalNotificationCenter.CreateNotificationChannel();		
		.....		

		LoadApplication(new App());
		.....	
		LocalNotificationCenter.NotifyNotificationTapped(Intent);
	}

	protected override void OnNewIntent(Intent intent)
	{
		LocalNotificationCenter.NotifyNotificationTapped(intent);
		base.OnNewIntent(intent);
	}
}

iOS

Setup

You must get permission from the user to allow the app to show local notifications. Also, To receive the Local Notification tap event. Include the following code in the FinishedLaunching() method of AppDelegate:

public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
{
        public override bool FinishedLaunching(UIApplication app, NSDictionary options)
        {
            global::Xamarin.Forms.Forms.Init();

            LocalNotificationCenter.SetUserNotificationCenterDelegate();

            LoadApplication(new App());

            return base.FinishedLaunching(app, options);
        }

	public override void WillEnterForeground(UIApplication uiApplication)
        {
            LocalNotificationCenter.ResetApplicationIconBadgeNumber(uiApplication);
            base.WillEnterForeground(uiApplication);
        }
}