0

By default from notification we open the first ViewController of the Mail.Storyboard. Is it possible to open another Storyboard? I tried few solutions already but don't works for me. Whole code is below. Let me know please if need more information. Thanks a lot.

import UIKit
import UserNotifications

class Notifications: UIViewController, UNUserNotificationCenterDelegate {

@IBOutlet weak var scheludeMeal: UIButton!
@IBOutlet weak var firstDatePicker: UIDatePicker!
...

var savedFirstNotification: Date? {
get {
return UserDefaults.standard.object(forKey: "firstMealTime") as? Date
}
set {
UserDefaults.standard.set(newValue, forKey: "firstMealTime")
}
}
...
}

override func viewDidLoad() {
super.viewDidLoad()

turnOnNotification.isHidden = true

if let savedFirstNotification = savedFirstNotification {
firstDatePicker.date = savedFirstNotification
}       
...              
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.alert, .badge, .sound]) { (granted, error) in
if granted {
print("Yes!")
} else {
print("No...?")
}
}

}
...

@IBAction func scheludeButton(_ sender: Any) {

let center = UNUserNotificationCenter.current()

center.removeAllPendingNotificationRequests()

let content = UNMutableNotificationContent()
content.title = "Ttile"
content.body = "Description of notification :)"
content.sound = UNNotificationSound.default

let firstMealComponent = firstDatePicker.calendar.dateComponents([.hour, .minute], from: firstDatePicker.date)
let trigger = UNCalendarNotificationTrigger(dateMatching: firstMealComponent, repeats: true)
let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
center.add(request)

savedFirstNotification = firstDatePicker.date         
...
}         
}

enter image description here

Rengers
  • 14,911
  • 1
  • 36
  • 54
Roger
  • 3
  • 2

1 Answers1

0

This is a rough version of the solution you're looking for, of course you can add more verifications, like depending on the notification payload (userInfo) you can go to different controllers.

extension AppDelegate: UNUserNotificationCenterDelegate {
    @available(iOS 10.0, *)
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {

        completionHandler([.alert, .badge, .sound])
    }

    @available(iOS 10.0, *)
    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {

            let storyboard = UIStoryboard(name: "yourStoryboardName", bundle: nil)
            let initialViewController = storyboard.instantiateViewController(withIdentifier: "yourControllerName")

            self.window?.rootViewController = initialViewController
            self.window?.makeKeyAndVisible()
    }

    @available(iOS 9, *)
    func application(_ application: UIApplication, handleActionWithIdentifier identifier: String?, forRemoteNotification userInfo: [AnyHashable : Any], withResponseInfo responseInfo: [AnyHashable : Any], completionHandler: @escaping () -> Void) {

            let storyboard = UIStoryboard(name: "yourStoryboardName", bundle: nil)
            let initialViewController = storyboard.instantiateViewController(withIdentifier: "yourControllerName")

            self.window?.rootViewController = initialViewController
            self.window?.makeKeyAndVisible()

            completionHandler()
    }
}
Arie Pinto
  • 1,274
  • 1
  • 11
  • 19