如何在特定日期后停止触发预定的本地通知?
How do I stop a scheduled local notification from triggering after a particular date?
我正在使用 UNUserNotificationCenter 在 iOS 上显示一些本地通知。这些是预定的通知。这是我的高级要求:(类似于 iOS 上的 Reminders 应用程序。您可以选择提醒时间、设置间隔(频率)以及设置到期日期。
我得到了开始日期(2018 年 6 月 1 日)和结束日期(2018 年 6 月 28 日)和时间(例如:每天下午 2:00)。这些本地通知应仅在指定日期之间触发。
这是我的代码:
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 方法中的条件:
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:00 下午停止这件事?
还有,我对iOS不是很熟悉,UICalenderNotification触发器有有效期吗?
我尝试使用 ComponentsFromDateToDate 进行试验,但通知仍然出现..
//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;
我是不是做错了什么,还是应该考虑换种方式?
我也在考虑使用后台获取并有一段代码可以检查上述日期范围的逻辑。
感谢任何帮助。谢谢!
我认为最好的解决方案是:将 28 UNNotificationRequest
添加到 UNUserNotificationCenter
和 6.1-6.28
中的 UNCalendarNotificationTrigger
,通知将永远不会在 6.28 之后触发,所以在这样您就不需要手动删除该请求。
for(int i = 1; i < 29; i++)
{
NSDateComponents d = new NSDateComponents() { Year = 2018, Month = 6,Day = i,Hour= 14 };
UNCalendarNotificationTrigger trigger = UNCalendarNotificationTrigger.CreateTrigger(d, true);
UNMutableNotificationContent content = new UNMutableNotificationContent() { Title = "xxx", Body = "xxx", CategoryIdentifier = "xxx" };
UNNotificationRequest request = UNNotificationRequest.FromIdentifier(i.ToString(), content, trigger);
UNUserNotificationCenter.Current.AddNotificationRequest(request,null);
}
我正在使用 UNUserNotificationCenter 在 iOS 上显示一些本地通知。这些是预定的通知。这是我的高级要求:(类似于 iOS 上的 Reminders 应用程序。您可以选择提醒时间、设置间隔(频率)以及设置到期日期。
我得到了开始日期(2018 年 6 月 1 日)和结束日期(2018 年 6 月 28 日)和时间(例如:每天下午 2:00)。这些本地通知应仅在指定日期之间触发。
这是我的代码:
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 方法中的条件:
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:00 下午停止这件事?
还有,我对iOS不是很熟悉,UICalenderNotification触发器有有效期吗?
我尝试使用 ComponentsFromDateToDate 进行试验,但通知仍然出现..
//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;
我是不是做错了什么,还是应该考虑换种方式?
我也在考虑使用后台获取并有一段代码可以检查上述日期范围的逻辑。
感谢任何帮助。谢谢!
我认为最好的解决方案是:将 28 UNNotificationRequest
添加到 UNUserNotificationCenter
和 6.1-6.28
中的 UNCalendarNotificationTrigger
,通知将永远不会在 6.28 之后触发,所以在这样您就不需要手动删除该请求。
for(int i = 1; i < 29; i++)
{
NSDateComponents d = new NSDateComponents() { Year = 2018, Month = 6,Day = i,Hour= 14 };
UNCalendarNotificationTrigger trigger = UNCalendarNotificationTrigger.CreateTrigger(d, true);
UNMutableNotificationContent content = new UNMutableNotificationContent() { Title = "xxx", Body = "xxx", CategoryIdentifier = "xxx" };
UNNotificationRequest request = UNNotificationRequest.FromIdentifier(i.ToString(), content, trigger);
UNUserNotificationCenter.Current.AddNotificationRequest(request,null);
}