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

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

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

Firebaseは、Googleが提供するBasSサービスの一つ。リアルタイム通知可能、並びにアクセス制御ができるオブジェクトデータベース機能を備えます。さらに認証機能、アプリケーションのログ解析機能などの利用も可能です。

Swift

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

Q&A

0回答

778閲覧

Firebase Cloud Messaging プッシュ通知できない。

sacakoro

総合スコア35

Firebase

Firebaseは、Googleが提供するBasSサービスの一つ。リアルタイム通知可能、並びにアクセス制御ができるオブジェクトデータベース機能を備えます。さらに認証機能、アプリケーションのログ解析機能などの利用も可能です。

Swift

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

0グッド

0クリップ

投稿2020/07/04 08:30

編集2020/07/04 10:02

こちらのソースコードを参考に、プッシュ通知を受け取るコードを生成し、Firebase Cloud Messagingのコンソールから通知を送ってみましたが、プッシュ通知を受信できません。

https://github.com/firebase/quickstart-ios/blob/master/messaging/MessagingExampleSwift/AppDelegate.swift

実施済みの作業

Firebaseのプロジェクトの設定にAPN認証キーはアップロード済みです。
イメージ説明
Xcodeのプロジェクトで、Push Notification、Remote Notificationは有効にしています。
イメージ説明
AppDelegate.swiftはこの通りです。FirebaseApp.configure()の呼び出しは、Commonモジュール内に定義した、StartFirebase.shared()で行っています。

// // AppDelegate.swift import UIKit import Common import SwiftyStoreKit import FirebaseMessaging @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? var startFirebase : StartFirebase? let gcmMessageIDKey = "gcm.message_id" func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. startFirebase = StartFirebase.shared() //←この中でFirebaseApp.configure()を呼んでいる。 // ページを格納する配列 var viewControllers: [UIViewController] = [] // 1ページ目になるViewController let firstSB = UIStoryboard(name: "Main", bundle: nil) let firstVC = firstSB.instantiateInitialViewController()! as UIViewController firstVC.tabBarItem = UITabBarItem(title: NSLocalizedString("Tab_Movie", comment: ""), image: UIImage(named: "douga"), tag:1) // 2ページ目になるViewController let secondSB = UIStoryboard(name: "Config", bundle: nil) let secondVC = secondSB.instantiateInitialViewController()! as UIViewController secondVC.tabBarItem = UITabBarItem(title: NSLocalizedString("Tab_Setting", comment: ""), image: UIImage(named: "config"), tag: 2) // 入れ替えて登録 viewControllers.append(firstVC) viewControllers.append(secondVC) // ViewControllerをセット let tabBarCOntroller = UITabBarController() tabBarCOntroller.setViewControllers(viewControllers, animated: true) // rootViewControllerをUITabBarControllerにセット self.window = UIWindow() self.window?.rootViewController = tabBarCOntroller self.window?.makeKeyAndVisible() SwiftyStoreKit.completeTransactions(atomically: true) { purchases in for purchase in purchases { switch purchase.transaction.transactionState { case .purchased, .restored: if purchase.needsFinishTransaction { // Deliver content from server, then: SwiftyStoreKit.finishTransaction(purchase.transaction) } print("purchased: (purchase)") // Unlock content case .failed, .purchasing, .deferred: break // do nothing } } } // [START set_messaging_delegate] Messaging.messaging().delegate = self // [END set_messaging_delegate] // Register for remote notifications. This shows a permission dialog on first run, to // show the dialog at a more appropriate time move this registration accordingly. // [START register_for_notifications] if #available(iOS 10.0, *) { // For iOS 10 display notification (sent via APNS) UNUserNotificationCenter.current().delegate = self let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound] UNUserNotificationCenter.current().requestAuthorization( options: authOptions, completionHandler: {_, _ in }) } else { let settings: UIUserNotificationSettings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil) application.registerUserNotificationSettings(settings) } application.registerForRemoteNotifications() // [END register_for_notifications] return true } // [START receive_message] func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) { // If you are receiving a notification message while your app is in the background, // this callback will not be fired till the user taps on the notification launching the application. // TODO: Handle data of notification // With swizzling disabled you must let Messaging know about the message, for Analytics // Messaging.messaging().appDidReceiveMessage(userInfo) // Print message ID. if let messageID = userInfo[gcmMessageIDKey] { print("Message ID: (messageID)") } // Print full message. print(userInfo) } func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { // If you are receiving a notification message while your app is in the background, // this callback will not be fired till the user taps on the notification launching the application. // TODO: Handle data of notification // With swizzling disabled you must let Messaging know about the message, for Analytics // Messaging.messaging().appDidReceiveMessage(userInfo) // Print message ID. if let messageID = userInfo[gcmMessageIDKey] { print("Message ID: (messageID)") } // Print full message. print(userInfo) completionHandler(UIBackgroundFetchResult.newData) } // [END receive_message] func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) { print("Unable to register for remote notifications: (error.localizedDescription)") } // This function is added here only for debugging purposes, and can be removed if swizzling is enabled. // If swizzling is disabled then this function must be implemented so that the APNs token can be paired to // the FCM registration token. func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { print("APNs token retrieved: (deviceToken)") // With swizzling disabled you must set the APNs token here. // Messaging.messaging().apnsToken = deviceToken } } // [START ios_10_message_handling] @available(iOS 10, *) extension AppDelegate : UNUserNotificationCenterDelegate { // Receive displayed notifications for iOS 10 devices. func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { let userInfo = notification.request.content.userInfo // With swizzling disabled you must let Messaging know about the message, for Analytics // Messaging.messaging().appDidReceiveMessage(userInfo) // Print message ID. if let messageID = userInfo[gcmMessageIDKey] { print("Message ID: (messageID)") } // Print full message. print(userInfo) // Change this to your preferred presentation option completionHandler([[.alert, .sound]]) } func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { let userInfo = response.notification.request.content.userInfo // Print message ID. if let messageID = userInfo[gcmMessageIDKey] { print("Message ID: (messageID)") } // Print full message. print(userInfo) completionHandler() } } // [END ios_10_message_handling] extension AppDelegate : MessagingDelegate { // [START refresh_token] func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) { print("Firebase registration token: (fcmToken)") let dataDict:[String: String] = ["token": fcmToken] NotificationCenter.default.post(name: Notification.Name("FCMToken"), object: nil, userInfo: dataDict) // TODO: If necessary send token to application server. // Note: This callback is fired at each app startup and whenever a new token is generated. } // [END refresh_token] }
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data)

が呼ばれることは確認できています。
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) は呼ばれていません。

関係する要素が多く、状況の切り分けが難しく悩んでおります。
こういう観点で調べたらどうかというアドバイスをいただきたいなと思っています。

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

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

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

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

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

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.47%

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

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

質問する

関連した質問