質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

1回答

604閲覧

swift ローカル通知のタップケントができない

mars111

総合スコア24

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2022/11/21 08:26

やりたいこと

ローカル通知がタップされたときに、特定の画面へ遷移させるような機能を実装したいと思っています

試したこと

以下のページを参考に、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

問題点

プリントデバッグをしてみたところ、そもそもローカル通知のタップ時にこのコードが呼ばれていないようです。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

hoshi-takanori

2022/11/21 08:39

didFinishLaunchingWithOptions メソッドの中に didFinishLaunchingWithOptions と userNotificationCenter メソッドがあるように見えますが、インデントがおかしいだけ?
mars111

2022/11/22 15:40

すみません、単純にインデントが間違っていただけでした
guest

回答1

0

自己解決

インデントのミスでした

正しいコード

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 // Override point for customization after application launch. 16 let center = UNUserNotificationCenter.current() 17 // delegateを設定 18 center.delegate = self 19 20 center.requestAuthorization(options: [.alert, .sound]) { (granted, error) in 21 // Enable or disable features based on authorization. 22 } 23 24 25 return true 26 } 27 28 29 30 // 通知をタップするとこのメソッドが呼ばれる 31 func userNotificationCenter(_ center: UNUserNotificationCenter, 32 didReceive response: UNNotificationResponse, 33 withCompletionHandler completionHandler: @escaping () -> Void) { 34 self.window = UIWindow(frame: UIScreen.main.bounds) 35 print("呼ばれた") 36 // Storyboardを指定 37 let storyboard = UIStoryboard(name: "Main", bundle: nil) 38 // Viewcontrollerを指定 39 let initialViewController = storyboard.instantiateViewController(withIdentifier:"Morning") 40 // rootViewControllerに入れる 41 self.window?.rootViewController = initialViewController 42 // 表示 43 self.window?.makeKeyAndVisible() 44 } 45 46 // MARK: UISceneSession Lifecycle 47 48// func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration { 49// // Called when a new scene session is being created. 50// // Use this method to select a configuration to create the new scene with. 51// return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role) 52// } 53// 54// func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) { 55// // Called when the user discards a scene session. 56// // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions. 57// // Use this method to release any resources that were specific to the discarded scenes, as they will not return. 58// } 59 60 61} 62

投稿2022/11/22 15:41

mars111

総合スコア24

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問