0

I am creating an app to set local notifications which can be triggered between the set time with an interval of minutes which can be selected by me. I have managed to run the local notification but dont know how to set it between a particular time of the day.

//Model

struct ReminderModal:Codable{
    var Title:String
    var Description:String
    var Frequency:TimeFrequencyType
    var Id:String
    var StartTime:Date?
    var EndTime:Date?
}
struct TimeFrequencyType:Codable {
    let id: Int
    let name: String
}

let frequencyTypeArr = [
    TimeFrequencyType(id: 1, name: "1 Minute"),
    TimeFrequencyType(id: 5, name: "5 Minutes"),
    TimeFrequencyType(id: 10, name: "10 Minutes"),
    TimeFrequencyType(id: 15, name: "15 Minutes"),
    TimeFrequencyType(id: 20, name: "20 Minutes"),
    TimeFrequencyType(id: 39, name: "30 Minutes"),
    TimeFrequencyType(id: 60, name: "60 Minutes")
]

// Code for Notification

let name = obj.Title
let description = obj.Description
let id =  obj.Id
let fid = obj.Frequency.id


// build notification
let content = UNMutableNotificationContent()
content.title = NSString.localizedUserNotificationString(forKey: "\(name)", arguments: nil)
content.body = NSString.localizedUserNotificationString(forKey: "\(description)", arguments: nil)
content.sound = UNNotificationSound.default
content.badge = UIApplication.shared.applicationIconBadgeNumber + 1 as NSNumber;
content.categoryIdentifier = "com.qtechsoftware.ReminderApp"

// Deliver the notification
let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: TimeInterval(fid*60), repeats: true)
let request = UNNotificationRequest.init(identifier: "\(id)", content: content, trigger: trigger)

// Schedule the notification.
let center = UNUserNotificationCenter.current()
center.add(request)

Git Path For Source - ReminderApp

Yogesh Tandel
  • 1,738
  • 1
  • 19
  • 25

1 Answers1

0

You are using the wrong syntax to trigger it. https://developer.apple.com/documentation/usernotifications/scheduling_a_notification_locally_from_your_app Use this guide to create exactly what you want.

tl;dr:


let trigger = UNCalendarNotificationTrigger(
         dateMatching: dateComponents, repeats: true)
Don
  • 490
  • 3
  • 9
  • My notification is triggered at a set time and repeats after whatever interval I set. i want it to stop at a specific time on that day and continue to work for the next days without making any changes. – Yogesh Tandel Nov 10 '19 at 09:05
  • Well you can use UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: [notificationIdentifier]) to "kill" the notification waiting to be shown then you can set another trigger for the next day using the dateMatching initializatio – Don Nov 10 '19 at 09:15
  • Hi.. will check this out and let you know. – Yogesh Tandel Nov 11 '19 at 05:49