首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在特定日期后停止触发计划的本地通知?

如何在特定日期后停止触发计划的本地通知?
EN

Stack Overflow用户
提问于 2018-05-29 16:34:44
回答 1查看 978关注 0票数 1

我正在使用UNUserNotificationCenter在iOS上显示一些本地通知。这些是计划通知。这是我的高级需求:(类似于iOS上的提醒应用程序。您可以选择提醒的时间,设置时间间隔(频率),以及设置过期日期。

我得到了一个开始日期(2018年6月1日)和一个结束日期(2018年6月28日)和一个时间(例如每天下午2:00 )。这些本地通知只能在指定的日期之间触发。

下面是我的代码:

代码语言:javascript
复制
var content = new UNMutableNotificationContent();
    content.Title = "Title";
    content.Subtitle = "Subtitle";
    content.Body = "Body";
    content.Badge = 1;
    content.CategoryIdentifier = categoryID;
    content.Sound = UNNotificationSound.Default;

    var d = new NSDateComponents {
        Hour = 14
    };
    //Repeat is true, so it will keep triggering when ever the hour reads 14
    var newTrigger = UNCalendarNotificationTrigger.CreateTrigger(d, true);
    var requestID = "notificationRequest";
    var request = UNNotificationRequest.FromIdentifier(requestID, content, newTrigger);

    //Here I set the Delegate to handle the user tapping on notification
    UNUserNotificationCenter.Current.Delegate = new CustomUNUserNotificationCenterDelegate();

    UNUserNotificationCenter.Current.AddNotificationRequest(request, (err) => {
        if (err != null) {
            // Report error
            System.Console.WriteLine("Error: {0}", err);
        } else {
            // Report Success
            System.Console.WriteLine("Notification Scheduled: {0}", request);
        }
    });

我正在检查WillPresentNotification方法中的条件:

代码语言:javascript
复制
public override void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler)  {
    DateTime StartDate = new DateTime(2018, 6, 1, 0, 0, 0);
    DateTime EndDate = new DateTime(2018, 6, 28, 0, 0, 0);
    if ((DateTime.Now > StartDate) && (DateTime.Now < EndDate)) {
        UIAlertView alertView = new UIAlertView();
        alertView.Message = "within the date range";
        alertView.AddButton("OK");
        alertView.Show();
        completionHandler(UNNotificationPresentationOptions.Alert | UNNotificationPresentationOptions.Sound | UNNotificationPresentationOptions.Badge);
    } else {
        var requests = new string[] { "notificationRequest" };
        UNUserNotificationCenter.Current.RemovePendingNotificationRequests(requests);
    }
}

如果应用程序在前台,并且通知自动取消,则可以很好地工作。但如果应用程序被终止,那么这些通知仍然会继续发送。有没有办法检查本地通知何时在后台触发,以便检查上面的逻辑并停止这些通知?

有没有其他方法可以在6月28日下午2点停止这一切?

另外,我对iOS不是很熟悉,UICalenderNotification触发器有没有到期日?

我试着尝试使用ComponentsFromDateToDate,但通知还在不断出现。

代码语言:javascript
复制
        //This is just test data
        var d = new NSDateComponents();
        NSCalendar currCal = new NSCalendar(NSCalendarType.Gregorian);
        var d1 = new NSDateComponents();
        d1.Day = 29;
        d1.Month = 5;
        d1.Year = 2018;
        d1.Hour = 12;
        d1.Minute = 58;
        d1.Second = 00;
        var d2 = new NSDateComponents();
        d2.Day = 29;
        d2.Month = 5;
        d2.Year = 2018;
        d2.Hour = 13;
        d2.Minute = 02;
        d2.Second = 00;
        currCal.ComponentsFromDateToDate(NSCalendarUnit.Second, d1, d2, NSCalendarOptions.None);
        d.Second = 10;
        d.Calendar = currCal;

我是不是做错了什么,还是应该换个方式去做呢?

我还在考虑使用后台fetch,并编写一段代码来检查日期范围的上述逻辑。

任何帮助都是非常感谢的。谢谢!

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50579808

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档