ios - Calendar Based Local Notification Not Working - Swift 3 -
i have setup local notification system can fire notification @ time every day. time determined user , store string. break down steps have done in code follow basically, problem notification won't fire.
step 1: in step setup alert ask permission send notification:
let center = unusernotificationcenter.current() let options: unauthorizationoptions = [.alert, .badge, .sound] center.requestauthorization(options: options) { (granted, error) in if granted { application.registerforremotenotifications() } } step 2: in step setup function call send notification:
static func sendnotification(stringdate: string) { let dateformatter = dateformatter() dateformatter.dateformat = "hh:mm" let content = unmutablenotificationcontent() content.title = "title" content.body = "detail" content.badge = 1 if let date = dateformatter.date(from: stringdate) { let calendar = calendar.current let hour = calendar.component(.hour, from: date) let minute = calendar.component(.minute, from: date) var datecomponents = datecomponents() datecomponents.hour = hour datecomponents.minute = minute let trigger = uncalendarnotificationtrigger(datematching: datecomponents, repeats: true) let request = unnotificationrequest(identifier: "datedone", content: content, trigger: trigger) unusernotificationcenter.current().add(request, withcompletionhandler: nil) } } step 3: call function in app delegate file so:
func applicationdidenterbackground(_ application: uiapplication) { let defaults = userdefaults.standard if defaults.object(forkey: "currentuser") != nil { if user.current.desiredtimefornews.characters.contains("a") { let stringtobeconverted = user.current.desiredtimefornews.replacingoccurrences(of: "am", with: "") helperfunctions.sendnotification(stringdate: stringtobeconverted) } else { let stringtobeconverted = user.current.desiredtimefornews.replacingoccurrences(of: "pm", with: "") helperfunctions.sendnotification(stringdate: stringtobeconverted) } } } important: can see within function take in "stringdate" parameter. variable has string value of date. convert date value. through using breakpoints , print statements have seen values including date, hour, minute, etc not nil.
overall, problem notification never sent. know fact called have used breakpoints prove that. appreciated!
for me personally, ran on real device , worked. although should work on simulator didn't me. try , run on real device before looking @ else!
Comments
Post a Comment