-
Notifications
You must be signed in to change notification settings - Fork 68
2. Usage 10.0.0 Xamarin.Forms (Support ended on May 1, 2024)
Elvin (Tharindu) Thudugala edited this page Jul 21, 2024
·
1 revision
-
Plugin.LocalNotification
Available on NuGet: https://www.nuget.org/packages/Plugin.LocalNotification - Install into your platform-specific projects (iOS/Android), and any .NET Standard 2.0/2.1 projects required for your app.
- Must Use Xamarin.Forms 4.5.0.356 or above.
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);
public partial class App : Application
{
public App()
{
InitializeComponent();
// Local Notification tap event listener
LocalNotificationCenter.Current.NotificationActionTapped += OnNotificationActionTapped;
MainPage = new MainPage();
}
private void OnNotificationActionTapped(NotificationEventArgs 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
}
}
}
The project should target Android framework 12.0+
[assembly: UsesPermission(Manifest.Permission.WakeLock)]
[assembly: UsesPermission(Manifest.Permission.ReceiveBootCompleted)]
[assembly: UsesPermission(Manifest.Permission.Vibrate)]
[assembly: UsesPermission("android.permission.SCHEDULE_EXACT_ALARM")]
[assembly: UsesPermission("android.permission.POST_NOTIFICATIONS")]
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
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)
{
.....
LocalNotificationCenter.MainActivity = this;
// Must create a Notification Channel when API >= 26
// you can create multiple Notification Channels with different names.
LocalNotificationCenter.CreateNotificationChannel();
.....
LoadApplication(new App());
.....
LocalNotificationCenter.NotifyNotificationTapped(Intent);
}
protected override void OnNewIntent(Intent intent)
{
LocalNotificationCenter.NotifyNotificationTapped(intent);
base.OnNewIntent(intent);
}
}
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();
LocalNotificationCenter.SetUserNotificationCenterDelegate();
LoadApplication(new App());
return base.FinishedLaunching(app, options);
}
public override void WillEnterForeground(UIApplication uiApplication)
{
LocalNotificationCenter.ResetApplicationIconBadgeNumber(uiApplication);
base.WillEnterForeground(uiApplication);
}
}