Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 91519

How to catch a local notification when the app is in background?

$
0
0

I am using the UNUserNotificationCenter to show some local notifications on iOS. These are scheduled notifications. Here is my high-level requirement:

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?

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

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!


Viewing all articles
Browse latest Browse all 91519

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>