Skip to content

Latest commit

 

History

History
255 lines (191 loc) · 8.88 KB

README.md

File metadata and controls

255 lines (191 loc) · 8.88 KB

icon

Build status CI NuGet NuGet

Plugin.LocalNotification

The local notification plugin provides a way to show local notifications from Xamarin.Forms apps.

Setup

Platform Support

Feature Xamarin.iOS Xamarin.Android
Required SDK >= 10 >= API 19
Title
Description
Subtitle
Scheduled
Custom Sounds
Images
Notification Actions
Pending Notifications
Delivered Notifications

Android Notes:

Project should target Android framework 11.0+

Usage

Show local notification

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 NotificationCenter.Current.Show(notification);

Or with Notification Request Builder

await NotificationCenter.Current.Show((notification) => notification
                        .NotifyAt(DateTime.Now.AddSeconds(30)) // Used for Scheduling local notification, if not specified notification will show immediately.
                        .WithTitle("Test Title")
                        .WithDescription("Test Description")
                        .WithReturningData("Dummy Data") // Returning data when tapped on notification.
                        .WithNotificationId(100)
                        .Create());

With platform specific options

await NotificationCenter.Current.Show((notification) => notification
                    .WithScheduleOptions((schedule) => schedule
					.NotifyAt(DateTime.Now.AddSeconds(30))
					.Build())
                    .WithAndroidOptions((android) => android
                         .WithAutoCancel(true)
                         .WithChannelId("General")
                         .WithPriority(NotificationPriority.High)
                         .WithOngoingStatus(true)
                         .Build())
                    .WithiOSOptions((ios) => ios
                        .WithForegroundAlertStatus(true)
                        .WithForegroundSoundStatus(true)
                        .Build())
                    .WithReturningData("Dummy Data")
                    .WithTitle("Test Title")
                    .WithDescription("Test Description")
                    .WithNotificationId(100)
                    .Create());

Receive local notification tap event

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

		// Local Notification tap event listener
		NotificationCenter.Current.NotificationTapped += OnLocalNotificationTapped;

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

Notification received event

On iOS this event is fired only when the app is in foreground

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

		// Local Notification received event listener
		NotificationCenter.Current.NotificationReceived += OnLocalNotificationReceived;

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

Platform Specific Notes

Android

Project should target Android framework 11.0+

image

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)
	{
	    	.....
	    	// Must create a Notification Channel when API >= 26
        	// you can created multiple Notification Channels with different names.
        	NotificationCenter.CreateNotificationChannel();		
		.....		
		LoadApplication(new App());
		.....	
		NotificationCenter.NotifyNotificationTapped(Intent);
	}

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

            // Ask the user for permission to show notifications on iOS 10.0+ at startup.
            // If not asked at startup, user will be asked when showing the first notification.
            Plugin.LocalNotification.NotificationCenter.AskPermission();

            LoadApplication(new App());

            return base.FinishedLaunching(app, options);
        }

	public override void WillEnterForeground(UIApplication uiApplication)
        {
            Plugin.LocalNotification.NotificationCenter.ResetApplicationIconBadgeNumber(uiApplication);
        }
}

Screen Record

Screen Record

Video

Local Push Notifications in Xamarin.Forms

SourceLink Support

In Visual Studio, confirm that SourceLink is enabled. Also, Turn off "Just My Code" since, well, this isn't your code.

https://docs.microsoft.com/en-us/dotnet/standard/library-guidance/sourcelink

Limitations

Only support iOS and Android for the moment.

6.1.0 Documentation

  • Please go to 6.1.0 Documentation, if you are referencing a version below 7.0.0.
  • Version 7.* and 8.* has setup differences upgrading from version 6.*

5.2.0 Documentation

  • Please go to 5.2.0 Documentation, if you are referencing a version below 6.0.0.
  • Version 6.* has setup differences if upgrading from version 5.*

4.1.4 Documentation

  • Please go to 4.1.4 Documentation, if you are referencing a version below 5.0.0.
  • Version 5.* has setup differences in Android if upgrading from version 4.*

3.0.2 Documentation

  • Please go to 3.0.2 Documentation, if you are referencing a version below 4.0.0.
  • Version 4.* has setup differences in Android if upgrading from version 3.*

2.0.7 Documentation

  • Please go to 2.0.7 Documentation, if you are referencing a version below 3.0.0.
  • Version 3.* has breaking changes if upgrading from version 2.*

Contributing

Contributions are welcome. Feel free to file issues and pull requests on the repo and they'll be reviewed as time permits.

Thank you