【问题标题】:Xamarin.forms Schedule Local notificationsXamarin.forms 安排本地通知
【发布时间】:2020-07-14 11:55:08
【问题描述】:

我遵循来自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/ 现在我不知道如何停止 Alarm Manager UILocalNotification 当我发送示例用户输入发送 20 个通知时,必须停止 20 个通知。

【问题讨论】:

  • 您好,您的意思是要在新设置到来之前停止通知?
  • @JuniorJiang-MSFT 如果用户每 10 分钟输入 10 次。 10 点后通知必须取消。
  • 好的,我们需要在每次执行任务后计算时间。次数可以本地存储,如果时间符合设置,会取消你想要的日程。
  • @JuniorJiang-MSFT 如何取消?
  • 这意味着我们需要根据另一个计算停止时间的计划手动取消计划。如果不介意在 Page 中添加开关控制,那会让客户决定是否需要关闭重复的通知。

标签: xamarin.forms localnotification


【解决方案1】:

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

安卓

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");  
    }  
}

【讨论】:

  • 2021 自从奥利奥以来,我认为这不再有效。应用关闭后将无法使用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-06
相关资源
最近更新 更多