[英]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 PM)。 這些本地通知應僅在指定日期之間觸發。
這是我的代碼:
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 PM停止這一操作嗎?
另外,我對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;
我在這里做錯了嗎,還是應該考慮以其他方式做?
我還考慮使用后台獲取,並提供一段代碼來檢查日期范圍的上述邏輯。
任何幫助表示贊賞。 謝謝!
我認為最好的解決方案是:從6.1-6.28
,使用UNCalendarNotificationTrigger
將28 UNNotificationRequest
添加到UNUserNotificationCenter
,該通知將永遠不會在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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.