简体   繁体   中英

How do I stop a scheduled local notification from triggering after a particular date?

I am using the UNUserNotificationCenter to show some local notifications on iOS. These are scheduled notifications. Here is my high-level requirement: (Something similar to Reminders app on iOS. You have the capability of choosing a time for the reminder, setting the interval (how frequent) and also setting the expiry date.

I am given a Start Date (01-June-2018) and an End Date(28-June-2018) and a time (For ex: 2:00 PM everyday). These local notifications should trigger in between the specified dates only.

Here is my code:

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

And I am checking the condition in WillPresentNotification method:

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

This works fine if the app is in foreground and the notifications automatically cancel off. But if the app is killed then these notifications still keep coming. Is there a way to check when a local notification is fired in the background so that the above logic can be checked and stop these notifications?

Is there any other way we can stop this exactly on june 28th 2:00 PM?

Also, I am not quite familiar with iOS, does UICalenderNotification trigger have an expiry date?

I tried to experiment with ComponentsFromDateToDate but the notifications were still coming..

        //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;

Am I doing something wrong here or should be thinking of doing it differently?

I was also thinking of using a background fetch and have a piece of code that would check the above logic for the date range.

Any help is appreciated. Thanks!

I think the best solution is : add 28 UNNotificationRequest into UNUserNotificationCenter with the UNCalendarNotificationTrigger from 6.1-6.28 , the notification will never be triggered after 6.28, so in this way you don't need to remove that request manually.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM