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

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

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

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

Swift

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

Q&A

解決済

1回答

4131閲覧

FCM(Firebase Cloud Messaging)を利用したプッシュ通知が届かない

sunglass

総合スコア303

Firebase

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

Swift

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

0グッド

0クリップ

投稿2021/03/14 07:40

編集2021/03/19 14:14

参考記事をもとにFCM(Firebase Cloud Messaging)を手順通り実装したのですがプッシュ通知が届かないです。

FCMではなく、別途作成したAppleのローカル通知は届きます。

Sigin in
イメージ説明

import os import UIKit import Firebase import GoogleMobileAds import FirebaseMessaging import UserNotifications // import IQKeyboardManagerSwift @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate { var window: UIWindow? override init() { super.init() // Firebase関連の機能を使う前に必要 FirebaseApp.configure() } func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. // プッシュ通知許可の取得 UNUserNotificationCenter.current().requestAuthorization( options: [.alert, .sound, .badge]){ (granted, _) in if granted{ UNUserNotificationCenter.current().delegate = self } } 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() // for レビュー催促 let key = "startUpCount" UserDefaults.standard.set(UserDefaults.standard.integer(forKey: key) + 1, forKey: key) UserDefaults.standard.synchronize() let count = UserDefaults.standard.integer(forKey: key) if count == 8 { if #available(iOS 10.3, *) { SKStoreReviewController.requestReview() } } // Admob関連 GADMobileAds.configure(withApplicationID: "ca-app-pub-../..") // for close Keyboard // IQKeyboardManager.shared.enable = true return true } // MARK: - UISceneSession Lifecycle func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration { // Called when a new scene session is being created. // Use this method to select a configuration to create the new scene with. return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role) } func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) { // Called when the user discards a scene session. // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions. // Use this method to release any resources that were specific to the discarded scenes, as they will not return. } // FCMプッシュ通知 func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) { // Print message ID. if let messageID = userInfo["gcm.message_id"] { print("Message ID: (messageID)") } // Print full message. print(userInfo) } func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { // Print message ID. if let messageID = userInfo["gcm.message_id"] { print("Message ID: (messageID)") } // Print full message. print(userInfo) completionHandler(UIBackgroundFetchResult.newData) } @available(iOS 10, *) func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { let userInfo = notification.request.content.userInfo if let messageID = userInfo["gcm.message_id"] { print("Message ID: (messageID)") } print(userInfo) completionHandler([]) } func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { let userInfo = response.notification.request.content.userInfo if let messageID = userInfo["gcm.message_id"] { print("Message ID: (messageID)") } print(userInfo) completionHandler() } }

FCM側

イメージ説明
参考記事
https://note.com/t0_inoue/n/nb817e740654b#6W7Zk

【追記】2021/03/19 23:13
Apple Developer→Certificates, Identifiers & Profiles→keys
イメージ説明
イメージ説明

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

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

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

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

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

tomato879241

2021/03/16 11:09

>プッシュ通知が届かないです。 どの状態(アプリが起動中、アプリが起動していない、端末の電源が切れている)で何に(実機、Simulator)通知が届かないのでしょうか?
sunglass

2021/03/19 07:11

返信遅くなりました。 アプリ起動→通知✖️ アプリ起動していない→通知✖️ 端末の電源が切れている→通知✖️ 実機→通知✖️ シュミレーター→✖️ アプリ削除→再インストール→通知✖️ となっています。
sunglass

2021/03/19 07:13

つまり全てで通知が届かない状況になります。 Firebaseコンソールから送信はできているのが確認できます。 (FCMのレポート機能) なぜか受信できません。 もちろんプッシュ通知の許可はOKにしています。(確認済み) ローカル通知自体は動作しているので証明書関連でもない気がしていて。。 何が原因かサッパリわからないです。。
tomato879241

2021/03/19 13:39

APNs authentication keyまたはAPNs certificateは設定しましたか?
sunglass

2021/03/19 13:55

> APNs authentication keyまたはAPNs certificateは設定しましたか? こちらはApple Developer側で設定(作成)しました。 質問返しになって大変申し訳ありません ローカルプッシュ通知が動くのにFirebaseで動かない原因がよくわかっていないです。。 (資料を読むと証明書や設定やら同じようにも見えます。) 一から証明書を作成し直した方が良いでしょうか。 他にも確認すべき点がありましたらどんどんご指摘ください。 よろしくお願いいたします。
guest

回答1

0

自己解決

無事解決しました。
証明書関連を一から全部確認不備がないかチェックし再度作りなおすことで受信することが出来ました。
デバッグログで

FIRMessaging Remote Notifications proxy enabled, will swizzle remote notification receiver handlers. If you'd prefer to manually integrate Firebase Messaging, add "FirebaseAppDelegateProxyEnabled" to your Info.plist, and set it to NO. Follow the instructions at: https://firebase.google.com/docs/cloud-messaging/ios/client#method_swizzling_in_firebase_messaging to ensure proper integration.

と出るので info.plstに追加していたのですがこのメッセージは無視した方が良いです。
FirebaseAppDelegateProxyEnabledをboolで0(NO)に設定するとFCMも届かなります。
Firebase AuthenticationのTwitter認証も機能しくなります。。
(このメッセージに従ってせいで丸2日無駄にしました。。)

投稿2021/03/21 15:52

sunglass

総合スコア303

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問