I have an app that I use sometimes. I must have left it there in the background before I slept. When I woke up I saw this notification on my screen.
Does anyone have any suggestions on how I can make a notification like this appear with my XF application?
Also, do these notifications appear on Android also? I've never seen them on my Android phone but that could be because I use it much less.
We can use Shiny.Notifications NuGet Package to create cross-platform Local Notifications in Xamarin.Forms
Sample App
A completed sample app created using the code below can be found here: https://github.com/brminnick/LocalNotificationsSample
Walkthrough
1. Install Shiny.Notifications
Add the Shiny.Notifications NuGet Package to your Xamarin.Forms project, your Xamarin.iOS project and Xamarin.Android project.
2. Initialize Shiny.Notifications
Android
In the
[Application]
class, inOnCreate
, initialize Shiny by callingShiny.AndroidShinyHost.Init
and setting its icon by callingShiny.Notifications.AndroidOptions.DefaultSmallIconResourceName
:In
MainActivity.cs
, inOnRequestPermission
, allow Shiny to present request notifications permissions from the user by addingShiny.AndroidShinyHost.OnRequestPermissionsResult(requestCode, permissions, grantResults);
iOS
In
AppDelegate.cs
, inFinishedLaunching
, initialize Shiny by callingShiny.iOSShinyHost.Init
:3. Schedule a Local Notification
In this example, we will send a Local Notification immediately and schedule one to be sent one minute after the app launches
https://github.com/brminnick/LocalNotificationsSample
You can use
Notifications
to achieve this function. From document Local notifications in Xamarin.Forms,we will find:You can defines a cross-platform API that the application can use to interact with notifications.
For more details, you can check above document, and this link also contains a sample about Notifications.It should be helpful for you.
Of course, if you want your app send notification when app is in background, you can use background-tasks.
For more details,you can check:
https://xamarinhelp.com/xamarin-background-tasks/
https://docs.microsoft.com/zh-cn/xamarin/ios/app-fundamentals/backgrounding/
https://docs.microsoft.com/en-ie/xamarin/android/app-fundamentals/services/creating-a-service/