LocalNotifications 3.0.0
dotnet add package LocalNotifications --version 3.0.0
NuGet\Install-Package LocalNotifications -Version 3.0.0
<PackageReference Include="LocalNotifications" Version="3.0.0" />
paket add LocalNotifications --version 3.0.0
#r "nuget: LocalNotifications, 3.0.0"
// Install LocalNotifications as a Cake Addin #addin nuget:?package=LocalNotifications&version=3.0.0 // Install LocalNotifications as a Cake Tool #tool nuget:?package=LocalNotifications&version=3.0.0
Local Notifications for MAUI and Xamarin.Forms
Sample App
Platform | Screenshoots |
---|---|
Android | <img width="300" alt="Screenshot 2022-11-03 at 14 07 09" src="https://user-images.githubusercontent.com/22674537/199664085-a547575f-1506-4249-bfaf-5417df8dcbad.png"><img width="300" alt="Screenshot 2022-11-03 at 14 10 19" src="https://user-images.githubusercontent.com/22674537/199664456-dd9e8b62-c9c3-42c2-a91b-51e716861f57.png"> |
iOS | <img width="300" alt="Screenshot 2022-11-08 at 14 13 04" src="https://user-images.githubusercontent.com/22674537/200498405-03ebc105-2728-4bb4-bccf-573266f12ed7.png"><img width="300" alt="Screenshot 2022-11-08 at 14 13 47" src="https://user-images.githubusercontent.com/22674537/200498521-88d915ac-bc30-4e6b-b90a-7126248f73a8.png"> |
Usage
int notificationId = 1;
public int NotificationId => notificationId++;
// Show Local Notification
LocalNotificationCenter.Current.Show(notificationId: NotificationId,
title: "ShowNow",
description: "Hello World",
payload: "",
androidOptions: new AndroidOptions(),
iOSOptions = new iOSOptions());
// Show Hourly / Daily / Weekly Local Notification
LocalNotificationCenter.Current.ShowHourly(int notificationId, string title, string description, Time time, string payload, AndroidOptions androidOptions = null, iOSOptions iOSOptions = null);
LocalNotificationCenter.Current.ShowDaily(int notificationId, string title, string description, Time time, string payload, AndroidOptions androidOptions = null, iOSOptions iOSOptions = null);
LocalNotificationCenter.Current.ShowWeekly(int notificationId, string title, string description, Day weekDay, Time time, string payload, AndroidOptions androidOptions = null, iOSOptions iOSOptions = null);
// Schedule Local Notification
int value = 30;
LocalNotificationCenter.Current.Schedule(notificationId: NotificationId,
title: $"Schedule: {DateTime.Now.AddSeconds(value)}",
description: "Hello World",
dateTime: DateTime.Now.AddSeconds(value),
payload: "",
androidOptions: new AndroidOptions(),
iOSOptions: new iOSOptions());
// Cancel Local Notification
LocalNotificationCenter.Current.Cancel(notificationId: 9999);
// Cancel All Notification
LocalNotificationCenter.Current.CancelAll();
// Get Pending Notification Requests
var pendingNotifications = await LocalNotificationCenter.Current.GetPendingNotificationRequests();
// Events
LocalNotificationCenter.Current.OnNotificationReceived += (e) =>
{
Debug.WriteLine("OnNotificationReceived: NotificationId " + e.NotificationId);
};
LocalNotificationCenter.Current.OnNotificationTapped += (e) =>
{
Debug.WriteLine("OnNotificationTapped: NotificationId " + e.NotificationId);
};
// Firebase ~
LocalNotificationCenter.Current.OnTokenRefresh += (e) =>
{
Debug.WriteLine("Firebase Token: " + e.Token);
};
Getting Started
Platform Specific Notes [MAUI]
To receive the Local Notification tap event. Include the following code in the CreateMauiApp() method of MauiProgram:
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
})
.UseLocalNotifications(isFirebase: true, autoRegistration: true);
return builder.Build();
}
or
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
})
// https://learn.microsoft.com/en-us/dotnet/maui/fundamentals/app-lifecycle
.ConfigureLifecycleEvents(events =>
{
#if ANDROID
events.AddAndroid(android => android
.OnCreate((activity, bundle) => OnNotificationTapped(activity.Intent))
.OnNewIntent((activity, intent) => OnNotificationTapped(intent)));
static void OnNotificationTapped(Android.Content.Intent intent)
{
LocalNotifications.Platform.NotificationService.NotificationTapped(intent);
}
#elif IOS
events.AddiOS(iOS => iOS.FinishedLaunching((app, options) => InitLocalNotifications(options)));
static bool InitLocalNotifications(Foundation.NSDictionary options)
{
LocalNotifications.Platform.NotificationService.Initialize(options: options,
isFirebase: false,
autoRegistration: true);
return true;
}
#endif
}); ;
return builder.Build();
}
Platform Specific Notes [Xamarin]
Android
The project should target Android framework 11.0+
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)
{
.....
LoadApplication(new App());
.....
LocalNotifications.Platform.NotificationService.NotificationTapped(Intent);
}
protected override void OnNewIntent(Intent intent)
{
LocalNotifications.Platform.NotificationService.NotificationTapped(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.FormsApplicationDelegate
{
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
global::Xamarin.Forms.Forms.Init();
// The user will be asked when showing the first notification.
LocalNotifications.Platform.NotificationService.Initialize(options: options,
isFirebase: false,
autoRegistration: true);
LoadApplication(new App());
return base.FinishedLaunching(app, options);
}
}
Limitations
iOS pending notifications limit
There is a limit imposed by iOS where it will only keep 64 notifications that will fire the soonest.
Scheduled Android notifications
Some Android OEMs have their own customised Android OS that can prevent applications from running in the background.
Firebase
Create a Firebase project and enable Firebase Cloud Messaging
Android
add this permission:
<uses-permission android:name="android.permission.INTERNET" />
- Add google-services.json to Android project. Make sure build action is GoogleServicesJson
iOS
- Add GoogleService-Info.plist to iOS project. Make sure build action is BundleResource
- On Info.plist enable remote notification background mode → Enable Background Modes. Check the Enable Background Modes option and then check the Remote Notifications.
- Add FirebaseAppDelegateProxyEnabled in the app’s Info.plist file and set it to No
- Entitlements.plist. Choose the Push Notifications option from the left pane and check the Enable Push Notifications check box.
Call LocalNotifications.Platform.NotificationService.Initialize on AppDelegate FinishedLaunching
LocalNotifications.Platform.NotificationService.Initialize(options: options,
isFirebase: true,
autoRegistration: true);
Note: You need to configure the required certificates and provisioning profile for your iOS project additional to these steps.
Project Structure
Namespace | Description |
---|---|
LocalNotifications | ~ |
LocalNotifications.Platform | ~ |
LocalNotifications.Platform | ~ |
Further information
For more information please visit:
- Github repository: https://github.com/xDaijobu/LocalNotifications
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-android34.0 is compatible. net8.0-browser was computed. net8.0-ios was computed. net8.0-ios18.0 is compatible. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net8.0
- Newtonsoft.Json (>= 13.0.1)
-
net8.0-android34.0
- Newtonsoft.Json (>= 13.0.1)
- Xamarin.Firebase.Common (>= 120.1.2)
- Xamarin.Firebase.Messaging (>= 123.0.8)
- Xamarin.GooglePlayServices.Base (>= 118.1.0)
- Xamarin.GooglePlayServices.Basement (>= 118.1.0)
- Xamarin.GooglePlayServices.Tasks (>= 118.0.2)
-
net8.0-ios18.0
- Newtonsoft.Json (>= 13.0.1)
- Xamarin.Firebase.iOS.CloudMessaging (>= 8.10.0.2)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.