How to set Local Notification repeat interval to custom time interval?
Got the answer, it is as straight as it gets.
You cannot create custom repeat intervals.
You have to use on NSCalendarUnit's in-built Unit Time Intervals.
I tried all the above solutions and even tried other stuffs, but neither of them worked.
I have invested ample time in finding out that there is no way we can get it to work for custom time intervals.
How to create local notifications with repeat interval as weekday?
In fact, you can refer to this link completely, just modify the notifyTime
and repeateForMinute
inside,change the notifyTime to the date of Monday or Friday ,then change the repeateForMinute to seven days
for example,if the begin date is 2019/10/18 17:00 Friday.you could change in the LocalNotificationService
:
public void LocalNotification(string title, string body, int id, DateTime notifyTime){
notifyTime = new DateTime(2019, 10, 18, 17, 0, 0, DateTimeKind.Utc);
long repeate7Days = 1000 * 60 * 60 * 24 * 7;
long totalMilliSeconds = (long)(notifyTime.ToUniversalTime() - _jan1st1970).TotalMilliseconds;
if (totalMilliSeconds < JavaSystem.CurrentTimeMillis()){
totalMilliSeconds = totalMilliSeconds + repeate7Days;
}
...
var alarmManager = GetAlarmManager();
alarmManager.SetRepeating(AlarmType.RtcWakeup, totalMilliSeconds, repeate7Days, pendingIntent);
}
you could download its Source codes.
Schedule Local Notification in iOS / irregular time interval
2 Hours = 60 secs * 60 min *2 = 7200 secs. Will this work? if you use 7200 in TimeInterval in following code which is for five second (from Apple website)
let content = UNMutableNotificationContent()
content.title = NSString.localizedUserNotificationStringForKey("Hello!", arguments: nil)
content.body = NSString.localizedUserNotificationStringForKey("Hello_message_body", arguments: nil)
content.sound = UNNotificationSound.default() // Deliver the notification in five seconds.
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
let request = UNNotificationRequest(identifier: "FiveSecond", content: content, trigger: trigger) // Schedule the notification.
let center = UNUserNotificationCenter.current()
center.add(request)
EDIT: Please see similar code for using UILocalNotification
UILocalNotification* localNotification = [[UILocalNotificationalloc] init];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:7200];
localNotification.alertBody = @"Your alert message";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
How to Set repeat interval as well as fire date in UNNotificationRequest
You can still use the fire date and a repeat interval but it is not as obvious as it used to be. When you initialize the notification trigger you set its repeat parameter to YES
. The repeat interval is defined by the date components that you pass to the trigger via the dateMatching
parameter:
Repeat daily:
Just pass the date components hour, minute, second
⟶ The trigger will fire every day at that time
Repeat weekly:
Also pass the desired weekday component: weekday, hour, minute, second
⟶ The trigger will fire every week on that weekday at that time
Here is an example that fires a notification tomorrow at the same time and repeats it every week:
Objective-C:
UNMutableNotificationContent *notification = [[UNMutableNotificationContent alloc] init];
// find out what weekday is tomorrow
NSDate *sameTimeTomorrow = [NSDate dateWithTimeIntervalSinceNow:60*60*24];
NSInteger weekdayTomorrow = [[NSCalendar currentCalendar] component:NSCalendarUnitWeekday fromDate: sameTimeTomorrow];
// set the current time (without weekday)
unsigned unitFlags = NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
NSDateComponents *firstFireDateComponents = [[NSCalendar currentCalendar] components:unitFlags fromDate:sameTimeTomorrow];
// add tomorrow's weekday
firstFireDateComponents.weekday = weekdayTomorrow;
// set a repeating trigger for the current time and tomorrow's weekday
// (trigger repeats every week on that weekday at the current time)
UNCalendarNotificationTrigger *notificationTrigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:firstFireDateComponents repeats:YES];
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"notification" content:notification trigger:notificationTrigger];
[[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler: nil];
Swift:
let notification = UNMutableNotificationContent()
// find out what weekday is tomorrow
let weekDayTomorrow = Calendar.current.component(.weekday, from: Date(timeIntervalSinceNow: 60*60*24))
// set the current time (without weekday)
var firstFireDateComponents = Calendar.current.dateComponents([.hour, .minute, .second], from: Date())
// add tomorrow's weekday
firstFireDateComponents.weekday = weekDayTomorrow
// set a repeating trigger for the current time and tomorrow's weekday
// (trigger repeats every week on that weekday at the current time)
let notificationTrigger = UNCalendarNotificationTrigger(dateMatching: firstFireDateComponents, repeats: true)
let request = UNNotificationRequest(identifier: "notification", content: notification, trigger: notificationTrigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
How Can I Repeat Local Notifications for Evey Hours in Every Day?
let interval:TimeInterval = 60.0 // 1 minute = 60 seconds
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: interval, repeats: true)
Use UNTimeIntervalNotificationTrigger
instead of UNCalendarNotificationTrigger
because UNCalendarNotificationTrigger
object is used when you want to schedule the delivery of a local notification at the specified date and time and UNTimeIntervalNotificationTrigger
object is used when you want to schedule the delivery of a local notification after the specified number of seconds elapse.
https://developer.apple.com/documentation/usernotifications/untimeintervalnotificationtrigger
Related Topics
Randomly Choosing an Item from a Swift Array Without Repeating
Please Clear Some Confusions Regarding Uiviewcontroller
Get Uiscrollview to Scroll to the Top
Uicollectionview's Cellforitematindexpath Is Not Being Called
Ios: Uiview Subclass Init or Initwithframe:
Uiscrollview's Origin Changes After Popping Back to the Uiviewcontroller
Number of Threads Created by Gcd
How to Get Image File Size in Swift
Detect App Crashed During Load/Last Time It Was Run
iOS - Detect Blow into Mic and Convert the Results! (Swift)
Bridge Google Drive API to Swift
Alamofire: Sending JSON as Request Parameter
Xcode/Simulator: How to Run Older iOS Version
When to Use Uicollectionview Instead of Uitableview
Change Color of Png in Buttons - iOS
Improper Advertising Identifier [Idfa] Usage