LocalNotifications 2.0.5.1
See the version list below for details.
dotnet add package LocalNotifications --version 2.0.5.1
NuGet\Install-Package LocalNotifications -Version 2.0.5.1
<PackageReference Include="LocalNotifications" Version="2.0.5.1" />
paket add LocalNotifications --version 2.0.5.1
#r "nuget: LocalNotifications, 2.0.5.1"
// Install LocalNotifications as a Cake Addin #addin nuget:?package=LocalNotifications&version=2.0.5.1 // Install LocalNotifications as a Cake Tool #tool nuget:?package=LocalNotifications&version=2.0.5.1
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
NotificationCenter.Current.Show(notificationId: NotificationId,
title: "ShowNow",
description: "Hello World",
payload: "",
androidOptions: new AndroidOptions(),
iOSOptions = new iOSOptions());
// Show Hourly / Daily / Weekly Local Notification
NotificationCenter.Current.ShowHourly(int notificationId, string title, string description, Time time, string payload, AndroidOptions androidOptions = null, iOSOptions iOSOptions = null);
NotificationCenter.Current.ShowDaily(int notificationId, string title, string description, Time time, string payload, AndroidOptions androidOptions = null, iOSOptions iOSOptions = null);
NotificationCenter.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;
NotificationCenter.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
NotificationCenter.Current.Cancel(notificationId: 9999);
// Cancel All Notification
NotificationCenter.Current.CancelAll();
// Get Pending Notification Requests
var pendingNotifications = await NotificationCenter.Current.GetPendingNotificationRequests();
// Events
NotificationCenter.Current.OnNotificationReceived += (e) =>
{
Debug.WriteLine("OnNotificationReceived: NotificationId " + e.NotificationId);
};
NotificationCenter.Current.OnNotificationTapped += (e) =>
{
Debug.WriteLine("OnNotificationTapped: NotificationId " + e.NotificationId);
};
// Firebase ~
NotificationCenter.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.Droid.NotificationService.NotificationTapped(intent);
}
#elif IOS
events.AddiOS(iOS => iOS.FinishedLaunching((app, options) => InitLocalNotifications(options)));
static bool InitLocalNotifications(Foundation.NSDictionary options)
{
LocalNotifications.Platform.iOS.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.Droid.NotificationService.NotificationTapped(Intent);
}
protected override void OnNewIntent(Intent intent)
{
LocalNotifications.Platform.Droid.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.iOS.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.iOS.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.iOS.NotificationService.Initialize on AppDelegate FinishedLaunching
LocalNotifications.Platform.iOS.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.Droid | ~ |
LocalNotifications.Platform.iOS | ~ |
Further information
For more information please visit:
- Github repository: https://github.com/xDaijobu/LocalNotifications
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-android31.0 is compatible. net6.0-ios was computed. net6.0-ios16.0 is compatible. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. monoandroid10.0 is compatible. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. xamarinios10 is compatible. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- Newtonsoft.Json (>= 13.0.1)
-
MonoAndroid 10.0
- Newtonsoft.Json (>= 13.0.1)
- Xamarin.AndroidX.Core (>= 1.6.0.3)
- Xamarin.Firebase.Common (>= 120.0.0.3)
- Xamarin.Firebase.Messaging (>= 122.0.0.3)
- Xamarin.GooglePlayServices.Base (>= 117.6.0.3)
- Xamarin.GooglePlayServices.Basement (>= 117.6.0.4)
- Xamarin.GooglePlayServices.Tasks (>= 117.2.1.3)
-
net6.0-android31.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)
-
net6.0-ios16.0
- Newtonsoft.Json (>= 13.0.1)
- System.Runtime.InteropServices.NFloat.Internal (>= 6.0.1)
- Xamarin.Firebase.iOS.CloudMessaging (>= 8.10.0.2)
-
Xamarin.iOS 1.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.