2

I would like to launch my app from a local notification that will appear when the home screen is locked or when the user is in another app based on similar discussions here and here I have the following code in my AppDelegate:

func userNotificationCenter(_: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {

        switch response.actionIdentifier {

        case "play":
                var setAlarmVC = self.window?.rootViewController as? SettingAlarmViewController
                if setAlarmVC == nil {
                    let storyboard = UIStoryboard(name: "Main", bundle: nil)
                    setAlarmVC = storyboard.instantiateViewController(withIdentifier: "AlarmViewController") as? SettingAlarmViewController
                    self.window = UIWindow(frame: UIScreen.main.bounds)
                    self.window?.rootViewController = setAlarmVC
                    self.window?.makeKeyAndVisible()
                }

        case "snooze":
            print("I pressed pause")
        default:
            break
        }

        completionHandler()

    }

Within my SettingAlarmViewController's viewDidLoad, I have set up some simple print-outs

override func viewDidLoad() {
    super.viewDidLoad()
    print("Setting Alarm View Controller was instantiated")
}

When I press play from the local notification while the app is in the background, I get the console print-out as expected:

Setting Alarm View Controller was instantiated

But the app does not actually launch and Setting Alarm View Controller does not appear. If I then click on the app, a fresh Setting Alarm View Controller is the first visible thing. I feel like I must be missing something obvious, but I cannot figure out what it is.

EDIT: Doing more testing. When the notification appears on the lock screen and the user presses "play" from the lock screen, the password / unlock screen does not appear, but the app still launches and I get the print-out " Setting Alarm View Controller was instantiated"

RMoore
  • 183
  • 1
  • 2
  • 9
  • did you try printing the value of `self.window?.rootViewController` ? – S1LENT WARRIOR Feb 04 '18 at 16:42
  • print out of self.window?.rootViewController after "self.window?.makeKeyAndVisible()" results in : "Optional(<[myApp].SettingAlarmViewController: 0x102127800>)" – RMoore Feb 04 '18 at 16:48
  • i guess this is why your if-condition is failing at: `if setAlarmVC == nil {}` – S1LENT WARRIOR Feb 04 '18 at 17:11
  • I apologize, my comment wasn't clear. The if condition is not failing. When I print out self.window?.rootViewController at the end of the if statement I get "Optional(<[myApp].SettingAlarmViewController: 0x102127800>)" . Before the if statement I get nil. – RMoore Feb 04 '18 at 18:03
  • can you confirm if the `viewWillAppear` or `viewDidAppear` are getting called? – S1LENT WARRIOR Feb 05 '18 at 16:20
  • P.S try to see if this solution helps you out: https://stackoverflow.com/a/39112580/634958 – S1LENT WARRIOR Feb 05 '18 at 16:23
  • 1
    Well, a day of my life wasted on this, here is the problem so that others do not have the same issue: https://developer.apple.com/documentation/usernotifications/unnotificationactionoptions?language=objc you must ad [.foreground] to your Notification Action, as in let playAction = UNNotificationAction(identifier: "play", title: "play", options: [.foreground]) – RMoore Feb 06 '18 at 01:05

1 Answers1

0

Well, a day of my life wasted on this, here is the problem so that others do not have the same issue: developer.apple.com/documentation/usernotifications/… you must ad [.foreground] to your Notification Action, as in let playAction = UNNotificationAction(identifier: "play", title: "play", options: [.foreground])

RMoore
  • 183
  • 1
  • 2
  • 9