我遵循https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/local-notifications中有关本地通知的文档。我实现了它,并且工作得很好。现在我的场景是:用户输入Start Date
,Repetition time
和Number of Repetitions
。我需要一些后台服务来调用此推送通知吗?有什么建议可以安排在设备中吗?
更新
我通过以下链接添加了共享服务:https://www.c-sharpcorner.com/article/how-to-send-local-notification-with-repeat-interval-in-xamarin-forms/
现在,当我发送示例用户输入要发送20条通知时,我不知道如何停止Alarm Manager
和 UILocalNotification
,必须停止20条通知。
答案 0 :(得分:3)
我们可以使用AlarmManager.Cancel
和UIApplication.SharedApplication.CancelLocalNotification
停止计划本地通知。
iOS :
void CancleScheduleNotification()
{
UILocalNotification[] localNotifications= UIApplication.SharedApplication.ScheduledLocalNotifications;
//Traverse this array to get the UILocalNotification we want according to the key
foreach (var localNotification in localNotifications)
{
if(localNotification.UserInfo.ObjectForKey(new NSString("key")).ToString() == "value")
{
UIApplication.SharedApplication.CancelLocalNotification(localNotification);
}
}
}
Android :
void CancleScheduleNotification()
{
AlarmManager am = (AlarmManager)GetSystemService(Context.AlarmService);
Intent intent = new Intent("LILY_TEST_INTENT");
intent.SetClass(this, LilyReceiver.class);
intent.SetData(Android.Net.Uri.Parse("content://calendar/calendar_alerts/1"));
PendingIntent sender = PendingIntent.GetBroadcast(this, 0, intent, PendingIntentFlags.NoCreate);
if (sender != null){
Log.Info("lily","cancel alarm");
am.Cancel(sender);
}else{
Log.Info("lily","sender == null");
}
}