-
Notifications
You must be signed in to change notification settings - Fork 68
1. Usage 10.0.0 .Net MAUI
Elvin (Tharindu) Thudugala edited this page Mar 30, 2024
·
8 revisions
-
Plugin.LocalNotification
Available on NuGet: https://www.nuget.org/packages/Plugin.LocalNotification - Install Version 10.0.0 above
Open the Platforms/Android/MainApplication.cs file and add the following assembly attributes after using directives:
[assembly: UsesPermission(Manifest.Permission.WakeLock)]
//Required so that the plugin can reschedule notifications upon a reboot
[assembly: UsesPermission(Manifest.Permission.ReceiveBootCompleted)]
[assembly: UsesPermission(Manifest.Permission.Vibrate)]
[assembly: UsesPermission("android.permission.POST_NOTIFICATIONS")]
optional
[assembly: UsesPermission("android.permission.USE_EXACT_ALARM")]
[assembly: UsesPermission("android.permission.SCHEDULE_EXACT_ALARM")]
Open the Platforms/Android/AndroidManifest.xml file and add the following in the manifest node:
<uses-permission android:name="android.permission.WAKE_LOCK" />
<!--Required so that the plugin can reschedule notifications upon a reboot-->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
optional
<uses-permission android:name="android.permission.USE_EXACT_ALARM" />
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />
<key>UIBackgroundModes</key>
<array>
<string>fetch</string>
<string>remote-notification</string>
</array>
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.........
.UseLocalNotification();
return builder.Build();
}
if (await LocalNotificationCenter.Current.AreNotificationsEnabled() == false)
{
await LocalNotificationCenter.Current.RequestNotificationPermission();
}
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 LocalNotificationCenter.Current.Show(notification);
You can use INotificationService without LocalNotificationCenter.Current as it is dependency injected
public partial class App : Application
{
public App()
{
InitializeComponent();
// Local Notification tap event listener
LocalNotificationCenter.Current.NotificationActionTapped += OnNotificationActionTapped;
MainPage = new MainPage();
}
private void OnNotificationActionTapped(NotificationActionEventArgs e)
{
if (e.IsDismissed)
{
// your code goes here
return;
}
if (e.IsTapped)
{
// your code goes here
return;
}
// if Notification Action are setup
switch (e.ActionId)
{
// your code goes here
}
}
}