UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Day 73 - Project21 - Challenge - Triggering Local Reminder 24hrs later

Forums > 100 Days of Swift

Hey everyone! I was trying to solve challenge 2 for project21, when came across this thread Day 73 - Project21 - Challenge - Triggering Local Reminder 24hrs later (the original thread is archived so I can't reply there) However with this solution, if I force quit the app(press shift+command+H twice then swipe up project 21), the notification will never pop up again.

I came up with this "hack" later, which I believe is working after the app is killed, but just wanna know if there are better ways to solve it:

  1. Add a remindLater action in registerCatgories() function, with an empty array of options, and add this action to the category

    let remindLater = UNNotificationAction(identifier: "remind later", title: "Remind me later", options: [])
    let category = UNNotificationCategory(identifier: "alarm", actions: [show, remindLater], intentIdentifiers: [])
  2. add a case for the actionIdentifier in didReceive delegate method:

    case "remind later":
                scheduleLocalNotification(postpone: true)

This changes the scheduleNotification() method's signature, so modify the method accordingly, in next step

  1. change scheduleNotification():
    @objc func scheduleLocalNotification(postpone: Bool = false) {
    // What's originally there
    let timeInterval: Double = 5
    let trigger: UNTimeIntervalNotificationTrigger
    if postpone {
            trigger = UNTimeIntervalNotificationTrigger(timeInterval: 3, repeats: false)
    } else {
            trigger = UNTimeIntervalNotificationTrigger(timeInterval: timeInterval, repeats: false)
    }
    // What's originally goes after
    }

By default, the postpone parameter is set to false, so the timeInterval we set inside the method is used. when postpone is set to true, we add a given number of seconds to that(for testing purpose I added 3 second instead of 86400) the notification will be shown after the given time interval(In my case it's 3 seconds after clicking the "Remind me later" button.

2      

One thing I forgot to metion: In project I didn't import UserNotifications as Paul did, but the project still works without any problem, anyone care to share some light on the reason?

3      

Good idea @ladiesman218 !. I almost quit while handling that challenge. As I was trying to pass data in scheduleLocalNotification() but somehow it did not work. And then looking at your code I tried another appoach. I set time interval as a global variable like this:

var timeInterval: Double = 5

then in scheduleLocalNoficiation() changed trigger as:

let trigger = UNTimeIntervalNotificationTrigger(timeInterval: timeInterval, repeats: false)

and what is left to do is: change in didRecieve case as:

case "remind":
        timeInterval = 86400
        scheduleLocalNotification()

and voila it works :) and the code is shorter

2      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.