2

I need to fire a notification at every 48 hours from a specific date. I can schedule it using UNTimeIntervalNotificationTrigger as -

let trigger : UNTimeIntervalNotificationTrigger = UNTimeIntervalNotificationTrigger.init(timeInterval: TimeInterval(48 * 60 * 60), repeats: true);
let content = UNMutableNotificationContent()   
   // Set the content whatever you want like title, subtitle, sound, userInfo 

let request = UNNotificationRequest(identifier: alertId, content: content, trigger: trigger);

My problem is how to set the fireDate. I need to start this notification from a future date but using it, it is started from today.

I also tried using UNCalendarNotificationTrigger(using component [.hour, .minute]) but it repeats every day as particular hour and minute matches every day. I also followed other solutions of stack overflow but still don't get any solution.

Dharmesh Kheni
  • 71,228
  • 33
  • 160
  • 165
Pooja Gupta
  • 785
  • 10
  • 22
  • just add the time of from todaydate to fromdate in your 48 hours – Devil Decoder Jul 05 '18 at 07:19
  • Can you please show a example. – Pooja Gupta Jul 05 '18 at 07:26
  • @RahulGUsai That would affect all triggering of the notification, not just the first, wouldn't it? – Joakim Danielson Jul 05 '18 at 07:29
  • @JoakimDanielson Yes, as per my understanding if the fireDate is 2 days from now and adding it would repeat the notification at every 4th day instead of 48 hours. – Pooja Gupta Jul 05 '18 at 07:32
  • i telling that take today date and your future date and count the interval like today is 5 july and you want to fire it on 7 july so its 48 hours and add you 48 hours so total it will be 96 hours from today so it will fire from 96 hours from today which is you desired time – Devil Decoder Jul 05 '18 at 07:45
  • @RahulGUsai, yes but it is set to repeat so the second notification and all future notifications will be fired 96 hours later, not 48 hours. Read the code. – Joakim Danielson Jul 05 '18 at 07:47
  • You can’t. You need to wait until you want the repeating notification to start and create it then. – matt Jul 05 '18 at 07:55
  • Why downvote? Please also comment so that I can improve. – Pooja Gupta Jul 05 '18 at 07:55
  • @matt In means I need to check for start date and my app may also be in killed state on that date. How to manage this? – Pooja Gupta Jul 05 '18 at 07:57
  • One workaround, but it is not the best one, is to create multiple notifications that fire once but after each other so the first at +96 hours, the second at +(96 + 48) hours and so on. I am not sure if there is some limitation on how many you can/should create but if you can get by with only a few this could work even if it's more work keeping track of them and cancelling them if needed. – Joakim Danielson Jul 05 '18 at 08:09
  • but you future date is going to change so interval also its not going to 96 hours every time – Devil Decoder Jul 05 '18 at 08:33
  • @JoakimDanielson Thanks for your solution but if I have to repeat this notification forever then it will be tough to manage the notifications. – Pooja Gupta Jul 05 '18 at 10:25

0 Answers0