Xamarin.forms 安排本地通知

Raz*_*Man 1 localnotification xamarin.forms

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

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

Jun*_*ang 5

我们可以使用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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

安卓

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");  
    }  
}
Run Code Online (Sandbox Code Playgroud)