Xamarin.forms计划本地通知

时间:2020-07-14 11:55:08

标签: xamarin.forms localnotification

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

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

1 个答案:

答案 0 :(得分:3)

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