WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

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.

   

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?

1      

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

   

Hacking with Swift is sponsored by Emerge

SPONSORED Why are Swift reference types bad for app startup time, and what’s the performance cost of protocol conformances? That’s just a couple of the topics you can learn about on the Emerge blog — written by the app performance experts behind Emerge’s advanced app optimization and monitoring tools, based on their experience of working at companies like Apple, Airbnb, Snap, and Spotify.

Find out more

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

Reply to this topic…

You need to create an account or log in to reply.

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.