1

I have a scenario where I want to add a local reminder of medication. The reminder can be daily or weekly of all medication which I used and duration of medicine can be in months. I already know that there is only 64 notification/app limit in iOS. Currently, I used a code with repeat true but notification work but I cannot restrict them to a specific date. Can anyone help me so I can set the notification for both daily and weekly and cannot cross the limit of 64?

let calender = Calendar.current

//   let gmtZone = TimeZone(secondsFromGMT: 0)
//   let dateFormatter =  DateFormatter()
//   dateFormatter.dateFormat = "yyyy-MM-dd HH:mm";
//   dateFormatter.timeZone = gmtZone
//  let dateString = dateFormatter.string(from: Date())
let date = Date()//dateFormatter.date(from: dateString)
var componentsFireDate = calender.dateComponents([.year, .day, .month,.hour ,.minute], from: date)
componentsFireDate.day = componentsFireDate.day! + expireIn
componentsFireDate.hour = hour
componentsFireDate.minute = minutes
componentsFireDate.second = 0
let fireDate = calender.date(from: componentsFireDate)
print("Date %@",fireDate!)

let center = UNUserNotificationCenter.current()
let content = UNMutableNotificationContent()
// content.title = title
content.body = body
content.sound = UNNotificationSound.default()
// content.categoryIdentifier = identifier

// let dailyComp = Calendar.current.dateComponents([.hour, .minute,.second], from: fireDate!)
let timeTrigger = UNTimeIntervalNotificationTrigger(timeInterval:60, repeats: true)

// let trigger = UNCalendarNotificationTrigger(dateMatching: dailyComp, repeats: false)
let request = UNNotificationRequest(identifier:identifier, content: content, trigger: timeTrigger)

center.add(request)
Piyush Bansal
  • 1,635
  • 4
  • 16
  • 39
  • Well if it's daily and weekly, the time interval should be for daily: 60 * 60 * 24, and for weekly it would be 60 * 60 * 24 * 7. – danypata Sep 20 '18 at 19:13
  • Is `fireDate` the date on which the medication expires? – ielyamani Sep 20 '18 at 23:20
  • @Carpsen90 yes firedate is medication expires – Syed Abdul Basit Sep 21 '18 at 03:32
  • @danypata thanks for your answer basically i want to set end date of reminder.for example if i set reminder for next 5 days how to ensure it not ring on 6th day? – Syed Abdul Basit Sep 21 '18 at 09:55
  • So, your app will handle notification every time it is triggered, what you have to do is check if it should be delivered (with a simple counter logic). If it shouldn't be displayed anymore you can cancel it. Check this one https://stackoverflow.com/a/45635699/2315974 – danypata Sep 21 '18 at 10:00

0 Answers0