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

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

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

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

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Swift

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

Q&A

1回答

7495閲覧

Push通知が届かなくなった(ios, fcm)

nyantaroo

総合スコア0

Firebase

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

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Swift

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

0グッド

0クリップ

投稿2020/12/30 05:33

編集2021/01/05 03:47

AppStoreにアプリが公開されてからPush通知が届かなくなりました。(正確なタイミングは不明、リリースが原因かも不明)
本番環境だけでなく、開発環境でも届かなくなりました。
公開される2日前までは、開発環境での実機シミューレートやTestFlightでのテストで、問題なく通知は届いていました。
公開されたのは昨日です。昨日はプッシュ通知のテストはしていません。

状況

SwiftUIを使って開発しています。
GitHubリポジトリ

認証キー(p8)を使用しています。

FCMのコンソールから通知を作成してみましたが、届きませんでした。(テストの段階では届いていました)

調べていたところ下のような質問を発見しました。
FCMでiOSアプリにPush通知が来ない

調べているうちに、「FireBaseの対応が追い付かず、リリース直後はCloudMessagingが動作しない事がある」との記述を見つけ、しばらく放置しました。先日、FireBaseから試験通知を行ったところ、無事アプリで受け取ることができました

上の記述は、他のページで発見することができませんでした。

試したこと

curlで通知を叩いて結果を調べてみる

zsh

1curl -X POST --header "Authorization: key=<FCMのコンソールにあるサーバキー>" \ 2--Header "Content-Type: application/json" \ 3https://fcm.googleapis.com/fcm/send \ 4-d @- << EOF 5{ 6 "to": "<通知をおくりたいFCM Token>", 7 "notification": { 8 "body": "お知らせテスト" 9 }, 10 "priority":10, 11 "mutable_content":true 12} 13EOF 14 15# 結果 16{"multicast_id":***********,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"**********"}]}

デバイストークンが誤っている、登録されていない場合はエラーレスポンスコードが返ってきます。
デバイストークンが登録されていない、サーバーキーが誤っている等の理由ではなさそうです。

公式リファレンス

AppDelegate

AppDelegateのコードを載せておきます。

swift

1 2import SwiftUI 3import Firebase 4import PartialSheet 5import FirebaseMessaging 6import UserNotifications 7import GoogleMobileAds 8 9@main 10struct StudyLevel_App: App { 11 @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate 12 let sheetManager: PartialSheetManager = PartialSheetManager() 13 let levelUpViewModel = LevelUpViewModel() 14 15 var body: some Scene { 16 WindowGroup { 17 ContentView() 18 .environmentObject(sheetManager) 19 .environmentObject(levelUpViewModel) 20 } 21 } 22} 23 24class AppDelegate: UIResponder, UIApplicationDelegate, MessagingDelegate { 25 26 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool { 27 FirebaseApp.configure() 28 GADMobileAds.sharedInstance().start(completionHandler: nil) 29 Messaging.messaging().delegate = self 30 if #available(iOS 10.0, *) { 31 let center = UNUserNotificationCenter.current() 32 center.delegate = self 33 34 let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound] 35 center.requestAuthorization(options: authOptions) { granted, error in 36 guard granted else { return } 37 38 DispatchQueue.main.async { 39 application.registerForRemoteNotifications() 40 } 41 } 42 } else { 43 let settings: UIUserNotificationSettings = 44 UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil) 45 application.registerUserNotificationSettings(settings) 46 } 47 48 application.registerForRemoteNotifications() 49 50 return true 51 } 52 53 func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) { 54 print("Firebase registration token: (String(describing: fcmToken))") 55 DeviceRegister().register(token: fcmToken) 56 } 57 58 func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration { 59 // Called when a new scene session is being created. 60 // Use this method to select a configuration to create the new scene with. 61 return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role) 62 } 63 64 func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) { 65 // Called when the user discards a scene session. 66 // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions. 67 // Use this method to release any resources that were specific to the discarded scenes, as they will not return. 68 } 69 70 func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) { 71 // Print message ID. 72 if let messageID = userInfo["gcm.message_id"] { 73 print("Message ID: (messageID)") 74 } 75 76 // Print full message. 77 print(userInfo) 78 } 79 80 func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], 81 fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { 82 // Print message ID. 83 if let messageID = userInfo["gcm.message_id"] { 84 print("Message ID: (messageID)") 85 } 86 87 // Print full message. 88 print(userInfo) 89 90 completionHandler(UIBackgroundFetchResult.newData) 91 } 92} 93 94@available(iOS 10, *) 95extension AppDelegate : UNUserNotificationCenterDelegate { 96 97 func userNotificationCenter(_ center: UNUserNotificationCenter, 98 willPresent notification: UNNotification, 99 withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { 100 let userInfo = notification.request.content.userInfo 101 102 if let messageID = userInfo["gcm.message_id"] { 103 print("Message ID: (messageID)") 104 } 105 106 print(userInfo) 107 108 completionHandler([.list, .banner, .sound, .badge]) 109 } 110 111 func userNotificationCenter(_ center: UNUserNotificationCenter, 112 didReceive response: UNNotificationResponse, 113 withCompletionHandler completionHandler: @escaping () -> Void) { 114 let userInfo = response.notification.request.content.userInfo 115 if let messageID = userInfo["gcm.message_id"] { 116 print("Message ID: (messageID)") 117 } 118 119 print(userInfo) 120 121 completionHandler() 122 } 123} 124 125extension Color { 126 static let backgroundGray = Color("backgroundGray") 127} 128

xcodeのログ

xcode

1StudyLevel_(12483,0x103e4b8c0) malloc: recording malloc and VM allocation stacks using lite mode 22021-01-04 20:46:46.322285+0900 StudyLevel_[12483:7352109] 7.3.0 - [Firebase/Analytics][I-ACS023007] Analytics v.7.3.0 started 32021-01-04 20:46:46.322600+0900 StudyLevel_[12483:7352109] 7.3.0 - [Firebase/Analytics][I-ACS023008] To enable debug logging set the following application argument: -FIRAnalyticsDebugEnabled (see http://goo.gl/RfcP7r) 42021-01-04 20:46:46.330274+0900 StudyLevel_[12483:7352113] 7.3.0 - [Firebase/Messaging][I-FCM001000] 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: 5https://firebase.google.com/docs/cloud-messaging/ios/client#method_swizzling_in_firebase_messaging 6to ensure proper integration. 72021-01-04 20:46:46.654976+0900 StudyLevel_[12483:7352102] 7.3.0 - [Firebase/Analytics][I-ACS800023] No pending snapshot to activate. SDK name: app_measurement 82021-01-04 20:46:46.667848+0900 StudyLevel_[12483:7352109] 7.3.0 - [Firebase/Analytics][I-ACS023012] Analytics collection enabled 92021-01-04 20:46:46.668168+0900 StudyLevel_[12483:7352109] 7.3.0 - [Firebase/Analytics][I-ACS023220] Analytics screen reporting is enabled. Call +[FIRAnalytics logEventWithName:FIREventScreenView parameters:] to log a screen view event. To disable automatic screen reporting, set the flag FirebaseAutomaticScreenReportingEnabled to NO (boolean) in the Info.plist 102021-01-04 20:46:48.385282+0900 StudyLevel_[12483:7351951] <Google> To get test ads on this device, set: GADMobileAds.sharedInstance.requestConfiguration.testDeviceIdentifiers = @[ @"bae7a10619e47a268758cd60b2f2643e" ]; 112021-01-04 20:46:48.387143+0900 StudyLevel_[12483:7352102] 7.3.0 - [Firebase/Analytics][I-ACS025026] Deferring to Firebase for event data collection 122021-01-04 20:46:49.797186+0900 StudyLevel_[12483:7351951] WF: === Starting WebFilter logging for process StudyLevel_ 132021-01-04 20:46:49.797361+0900 StudyLevel_[12483:7351951] WF: _userSettingsForUser mobile: { 14 filterBlacklist = ( 15 ); 16 filterWhitelist = ( 17 ); 18 restrictWeb = 1; 19 useContentFilter = 0; 20 useContentFilterOverrides = 0; 21 whitelistEnabled = 0; 22} 232021-01-04 20:46:49.797637+0900 StudyLevel_[12483:7351951] WF: _WebFilterIsActive returning: NO 242021-01-04 20:46:52.192272+0900 StudyLevel_[12483:7351951] [TraitCollection] Class CKBrowserSwitcherViewController overrides the -traitCollection getter, which is not supported. If you're trying to override traits, you must use the appropriate API.

バージョン

  • swift 5.3.1
  • Xcode 12.2
  • iOS 14.0

実機は、iOS14.2のiPhone11です。
他の端末も使用してテストしてみましたが、うまくいきませんでした。

補足

このあたりを調べると良い、この情報が欲しいなどのコメントがありましたらよろしくお願いします。

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

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

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

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

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

thyda.eiqau

2021/01/03 18:14

デバッグビルド (Xcodeからの実機ビルド) とストア版とで両方通知が届かないのですか?
nyantaroo

2021/01/04 00:46

両方届かないです。
thyda.eiqau

2021/01/04 14:12

届かないというのは画面に出ない(バナー等が出ない)という意味か、didReceiveRemoteNotificationの中を通っていない = print(userInfo)などが実行されていないという意味か、どちらですか?
nyantaroo

2021/01/05 00:36

画面に表示されないですし、didReceiveRemoteNotificationも呼ばれていないという状況です。
thyda.eiqau

2021/01/05 02:38

うーん、不思議ですね…… <通知をおくりたいFCM Token>と実機のトークンが一致していることは確かですか?didRefreshRegistrationTokenは実装されていないようですがトークンが更新されている可能性はないですかね?
nyantaroo

2021/01/05 03:49 編集

https://firebase.google.com/docs/cloud-messaging/ios/client?hl=ja#monitor-token-refresh 上記URLのコード例のように、didReceiveRegistrationTokenを実装してトークンを確認しているので問題ないと思います。 func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) { print("Firebase registration token: \(fcmToken)") } ↑のようにdidRefreshRegistrationTokenを実装して、ブレイクポイントを設定した状態でアプリを再インストールしてみましたが、呼び出されていないようでした。 ※公式リファレンスを確認した結果、didRefreshRegistrationTokenは削除され、didReceiveRegistrationTokenを使用するようになったようです。 https://firebase.google.com/support/release-notes/ios?q=didRefreshRegistrationToken#fcm_38
thyda.eiqau

2021/01/05 03:35

おっと、deprecatedどころかremovedとは……古い情報で失礼しました。 「<通知をおくりたいFCM Token>と実機のトークンが一致していることは確かですか?」についても引き続きお願いします。
nyantaroo

2021/01/05 03:50

Xcodeの実機デバックの際に、didReceiveRegistrationTokenが返すトークンを確認しているので、実機のトークンと一致していると思われます。 Firebaseのコンソールで全体に向けて通知を送信してみましたが、それでもとどきませんでした。
nyantaroo

2021/01/05 12:23

認証キーを用いてるので、開発環境と本番環境の区別はないはずです。 ↓認証キーと証明書の比較記事 https://takamii.hatenablog.com/entry/2020/07/13/190027 ちなみに証明書を使用する方法も試してみましたが、うまくいきませんでした。
guest

回答1

0

自分の体験談ですが,Build SettingのProduct Nameに日本語(utf-8文字列)が含まれており,本番での通知が届かないことがありました。こちら確認してみてください。

投稿2021/01/13 14:13

fathy

総合スコア254

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

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

nyantaroo

2021/01/15 12:30

貴重な情報ありがとうございます。 確認したところ、ProductName には日本語は含まれていませんでしたが、アンダーバーが含まれていたので、削除してテストしてみましたがうまくいきませんでした。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

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

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

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問