Podemos usar Shiny.Notifications NuGet Package para crear notificaciones locales multiplataforma en Xamarin.Forms
Aplicación de muestra
Puede encontrar una aplicación de muestra completada con el siguiente código aquí: https://github.com/brminnick/LocalNotificationsSample
Tutorial
1. Instalar Shiny.Notifications
Agregue el paquete NuGet Shiny.Notifications a su proyecto Xamarin.Forms, su proyecto Xamarin.iOS y su proyecto Xamarin.Android.
2. Inicialice Shiny.Notifications
Androide
En la [Application]
clase, en OnCreate
, inicialice Shiny llamando Shiny.AndroidShinyHost.Init
y estableciendo su icono llamando Shiny.Notifications.AndroidOptions.DefaultSmallIconResourceName
:
using System;
using Android.App;
using Android.Runtime;
using Shiny;
namespace LocalNotificationsSample.Droid
{
[Application]
public class YourApplication : Application
{
public YourApplication(IntPtr handle, JniHandleOwnership transfer) : base(handle, transfer)
{
}
public override void OnCreate()
{
base.OnCreate();
AndroidShinyHost.Init(this, platformBuild: services => services.UseNotifications());
Notifications.AndroidOptions.DefaultSmallIconResourceName = "icon.png";
}
}
}
En MainActivity.cs
, en OnRequestPermission
, permite que Shiny presente permisos de notificaciones de solicitud del usuario agregandoShiny.AndroidShinyHost.OnRequestPermissionsResult(requestCode, permissions, grantResults);
using Android.App;
using Android.Content.PM;
using Android.OS;
using Android.Runtime;
namespace LocalNotificationsSample.Droid
{
[Activity(Label = "LocalNotificationsSample", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
{
Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
Shiny.AndroidShinyHost.OnRequestPermissionsResult(requestCode, permissions, grantResults);
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
protected override void OnCreate(Bundle savedInstanceState)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
LoadApplication(new App());
}
}
}
iOS
En AppDelegate.cs
, en FinishedLaunching
, inicialice Shiny llamando Shiny.iOSShinyHost.Init
:
using Foundation;
using UIKit;
using Shiny;
namespace LocalNotificationsSample.iOS
{
[Register(nameof(AppDelegate))]
public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
{
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
iOSShinyHost.Init(platformBuild: services => services.UseNotifications());
global::Xamarin.Forms.Forms.Init();
LoadApplication(new App());
return base.FinishedLaunching(app, options);
}
}
}
3. Programe una notificación local
En este ejemplo, enviaremos una Notificación local de inmediato y programaremos una para que se envíe un minuto después de que se inicie la aplicación
using System;
using System.Threading.Tasks;
using Shiny;
using Shiny.Notifications;
using Xamarin.Forms;
namespace LocalNotificationsSample
{
public partial class App : Application
{
public App()
{
InitializeComponent();
MainPage = new MainPage();
}
protected override async void OnStart()
{
await SendNotificationNow();
await ScheduleLocalNotification(DateTimeOffset.UtcNow.AddMinutes(1));
}
Task SendNotificationNow()
{
var notification = new Notification
{
Title = "Testing Local Notifications",
Message = "It's working",
};
return ShinyHost.Resolve<INotificationManager>().RequestAccessAndSend(notification);
}
Task ScheduleLocalNotification(DateTimeOffset scheduledTime)
{
var notification = new Notification
{
Title = "Testing Local Notifications",
Message = "It's working",
ScheduleDate = scheduledTime
};
return ShinyHost.Resolve<INotificationManager>().Send(notification);
}
}
}
https://github.com/brminnick/LocalNotificationsSample