我想在应用关闭时显示本地通知,而且应用不在设备的最近历史记录中。我该怎么做。
请帮帮我..
这是我的代码 MainPage.cs
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
var startTimeSpan = TimeSpan.Zero;
var periodTimeSpan = TimeSpan.FromMinutes(5);
Device.StartTimer(TimeSpan.FromSeconds(5), () =>
{
GetData();
return true;
});
}
public void GetData()
{
IAlarmReceiver _local = DependencyService.Get<IAlarmReceiver>();
_local.LocalNotifications("Xamarin", "Hello world");
}
private void btn_Click_Clicked(object sender, EventArgs e)
{
GetData();
}
}
这里我每 5 秒调用一次通知方法。
这是我的 AlarmReciever.CS
public void GetNotification(string Title, string Text)
{
Intent _intent = new Intent(Application.Context, typeof(MainActivity));
const int _pendingIntentId = 0;
PendingIntent _pintent = PendingIntent.GetActivity(Application.Context, _pendingIntentId, _intent, PendingIntentFlags.CancelCurrent);
Notification.Builder builder = new Notification.Builder(Application.Context)
.SetContentIntent(_pintent)
.SetContentTitle(Title)
.SetContentText(Text)
.SetDefaults(NotificationDefaults.Sound)
.SetAutoCancel(true)
.SetSmallIcon(Resource.Drawable.icon);
Notification _notification = builder.Build();
NotificationManager _notificationmanager = Android.App.Application.Context.GetSystemService(Context.NotificationService)
as NotificationManager;
_notificationmanager.Notify(0, _notification);
最佳答案
您可以使用 Xamarin 的 ACR 通知插件,这将在应用关闭时显示本地通知。
示例 await CrossNotifications.Current.Send("Happy Birthday", "I sent this a long time ago", when = TimeSpan.FromDays(50));
关于c# - 当应用程序不在使用 Xamarin Crossplatform 的设备的最近历史记录中时如何显示本地通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49788344/