やりたいこと
ローカル通知がタップされたときに、特定の画面へ遷移させるような機能を実装したいと思っています
試したこと
以下のページを参考に、Scene Delegateを使わない設定をおこなった上で、以下のコードを書きました
https://qiita.com/PictoMki/items/19ea9ba8787bcbebd15c
swift
1import UIKit 2import Firebase 3import UserNotifications 4 5@main 6class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate { 7 8 // SVProgressHUDをXcode11で実行するための環境調整コード 9 var window: UIWindow? 10 11 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { 12 // Override point for customization after application launch. 13 FirebaseApp.configure() 14 15 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { 16 // Override point for customization after application launch. 17 let center = UNUserNotificationCenter.current() 18 // delegateを設定 19 center.delegate = self 20 21 center.requestAuthorization(options: [.alert, .sound]) { (granted, error) in 22 // Enable or disable features based on authorization. 23 } 24 return true 25 } 26 27 // 通知をタップするとこのメソッドが呼ばれる 28 func userNotificationCenter(_ center: UNUserNotificationCenter, 29 didReceive response: UNNotificationResponse, 30 withCompletionHandler completionHandler: @escaping () -> Void) { 31 self.window = UIWindow(frame: UIScreen.main.bounds) 32 print("呼ばれた") 33 // Storyboardを指定 34 let storyboard = UIStoryboard(name: "Main", bundle: nil) 35 // Viewcontrollerを指定 36 let initialViewController = storyboard.instantiateViewController(withIdentifier:"Morning") 37 // rootViewControllerに入れる 38 self.window?.rootViewController = initialViewController 39 // 表示 40 self.window?.makeKeyAndVisible() 41 } 42 43 44 return true 45 } 46 47} 48 49
問題点
プリントデバッグをしてみたところ、そもそもローカル通知のタップ時にこのコードが呼ばれていないようです。
回答1件
あなたの回答
tips
プレビュー