Calendar Date - Allows Local Notifications to be scheduled for a specific date and time.
In iOS 10 provides 4 different Trigger types:
1. Push notification
2. Time interval
3. Calendar Date
4. Location Based
You may refer to Xamarin website about about how to use it:
https://developer.xamarin.com/guides/ios/platform_features/introduction-to-ios10/user-notifications/enhanced-user-notifications/
If you going to use 'Time interval', then here is the example:
Embed script from gist.github.com
What If you plan to use 'Calendar Date' - Allow local notification to be scheduled for a specific date and time? Then.... you are lucky, because you on the right place now.
First of all, you wouldn't able to pass calendar datetime through this method:
UNTimeIntervalNotificationTrigger.CreateTriger(). Because this accept double type only. The expected value is milliseconds, not a date time.
Initially, I thought the method to accept calendar datetime has been left out from Xamarin.IOS. Because there are not much information about this can be found from the Xamarin website. After few hours of investigation, I finally realised that the CreateTrigger() method that support a specific date and time is actually located in different class!
"It is UNCalendarNotificationTrigger"
Please refer to the API reference about "UserNotitification - UNCalendarNotificationTrigger" : https://developer.apple.com/reference/usernotifications/uncalendarnotificationtrigger
In order to use this trigger, you need to create a DateComponents. In Xamarin, you can do something like this (this is in C#):
1 var d = new NSDateComponents();
2 d.Hour = 9;
3 d.Minutes = 10;
4 d.Month = 12;
5 d.Year = 2016;
6 d.Day = 2;
7 d.TimeZone = NSTimeZone.SystemTimeZone;
Next, create a trigger using UNCalendarNotificationTrigger:
var trigger = UNCalendarNotificationTrigger.CreateTrigger(d, false);
UNNotificationRequest request= UNNotificationRequest.FromIdentifier(requestId, content, trigger);
UNUserNotificationCenter.Current.AddNotificationRequest(request, (err)=>{
});
The above example make the calculation simple, and readable by building a trigger to fire on the exact date time.
Tested!,..It does fire on time!
No comments:
Post a Comment