Platform | Screenshoots |
---|---|
Android | |
iOS |
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);
};
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);
}
}
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.
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.
Namespace | Description |
---|---|
LocalNotifications | ~ |
LocalNotifications.Platform | ~ |
LocalNotifications.Platform | ~ |
For more information please visit:
- Github repository: https://github.com/xDaijobu/LocalNotifications