Skip to content

Usage 2.0.7

Elvin (Tharindu) Thudugala edited this page Mar 28, 2019 · 1 revision

Platform Support

Platform Supported Version Notes
Xamarin.iOS Yes iOS 8+
Xamarin.Android Yes API 16+ Project should target Android framework 8.1+

Usage

Show local notification

var notificationService = DependencyService.Get<ILocalNotificationService>();
var notification = new Plugin.LocalNotification.LocalNotification
{
    NotificationId = 100,
    Title = "Test",
    Description = "Test Description",
    ReturningData = "Dummy data", // Returning data when tapped on notification.
    NotifyTime = DateTime.Now.AddSeconds(30) // Used for Scheduling local notification.
};
notificationService.Show(notification);

Cancel a local notification

var notificationService = DependencyService.Get<ILocalNotificationService>();
notificationService.Cancel(100);

Receive local notification tap event

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

		// Local Notification tap event listener
		MessagingCenter.Instance.Subscribe<LocalNotificationTappedEvent>(this,
			typeof(LocalNotificationTappedEvent).FullName, OnLocalNotificationTapped);

		MainPage = new MainPage();
	}
	
	private void OnLocalNotificationTapped(LocalNotificationTappedEvent e)
    	{
		// your code goes here
	}
}

Platform Specific Notes

Android

Notification Icon must be set for notification to appear.

You can set the notification Icon by setting the following property from inside your Android project:

LocalNotificationService.NotificationIconId = Resource.Drawable.YOU_ICON_HERE

Scheduled local notifications will persist after device reboot, if permission is set and SDK more than 5.0 Lollipop (API 21)

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

Setup

To receive 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)
	{
		.....	
		LocalNotificationService.NotifyNotificationTapped(Intent);
	}

	protected override void OnNewIntent(Intent intent)
	{
		LocalNotificationService.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 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();

		LocalNotificationService.Init();

		LoadApplication(new App());
		
		.....
	}

	// This method only requires for iOS 8 , 9
        public override void ReceivedLocalNotification(UIApplication application, UILocalNotification notification)
        {
            //Change UIApplicationState to suit different situations
            if (UIApplication.SharedApplication.ApplicationState != UIApplicationState.Active)
            {
                LocalNotificationService.NotifyNotificationTapped(notification);
            }
        }
}