Xamarin.forms 安排本地通知

Xamarin.forms Schedule Local notifications

我遵循 https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/local-notifications 中关于本地通知的文档。我实施它并且工作得很好。现在我的场景是:用户输入 Start DateRepetition timeNumber of Repetitions 。我可能需要一些后台服务来调用此推送通知?有什么建议可以在设备中安排这个吗?

更新 我从这个 link 添加了共享服务:https://www.c-sharpcorner.com/article/how-to-send-local-notification-with-repeat-interval-in-xamarin-forms/ 现在我不知道如何停止 Alarm Manager UILocalNotification 当我发送示例用户输入发送 20 条通知时,20 条通知必须停止后。

我们可以使用 AlarmManager.CancelUIApplication.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");  
    }  
}