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

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

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

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

iOS

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

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

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

Q&A

解決済

1回答

1139閲覧

iOS11の端末でFirebaseからリモートPush通知を受け取りたい

alan-d-haller

総合スコア18

Firebase

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

iOS

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

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

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

0グッド

0クリップ

投稿2017/09/22 03:40

編集2017/09/22 07:20

###前提・実現したいこと
初めて質問させていただきます。
iOSアプリ開発を始めて1ヶ月半の初心者です。

iOS11をインストールした端末(iPhone SE)に
FirebaseからのリモートPush通知を受け取る機能をアプリに実装したいと思い、
Qiitaのこちらの記事を参考に準備を進めました。
https://qiita.com/jiiikki/items/31f294cf2afcfe8d868d

Podのインストールと、FirebaseへのAPNs証明書のアップロード、
プロビジョニングプロファイルのインストールまでは問題なく完了できたのですが、
最後のXcodeでプロジェクトの編集をする段階でつまづいています。

上記参考ページのコードではビルドできなかったため、
ご指導いただいている方から教えていただいた
後述するコードに変更したらビルドに成功したので、
実機でプッシュ通知を受け取ろうとテストしたところ、
通知を受け取ることができませんでした。

最近Firebase側で大規模なアップデートがあったと聞いたので、
それに関連してコードの仕様が変更になったのかと思ったのですが…
通知が受け取れない原因がいまいち特定できません。

Firebaseの仕様の変更後で、
iOS11の端末にFirebaseからのリモートPush通知を受け取るための
必要な最新のソースコードをご存知の方がいらっしゃいましたら、
ご教授いただけると幸いです。

どうぞ宜しくお願いいたします。

###【更新】該当のソースコード

Swift

1import UIKit 2import UserNotifications 3 4import Firebase 5 6@UIApplicationMain 7class AppDelegate: UIResponder, UIApplicationDelegate { 8 9 var window: UIWindow? 10 let gcmMessageIDKey = "gcm.message_id" 11 12 func application(_ application: UIApplication, 13 didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 14 15 FirebaseApp.configure() 16 17 // [START set_messaging_delegate] 18 Messaging.messaging().delegate = self 19 // [END set_messaging_delegate] 20 // Register for remote notifications. This shows a permission dialog on first run, to 21 // show the dialog at a more appropriate time move this registration accordingly. 22 // [START register_for_notifications] 23 if #available(iOS 10.0, *) { 24 // For iOS 10 display notification (sent via APNS) 25 UNUserNotificationCenter.current().delegate = self 26 27 let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound] 28 UNUserNotificationCenter.current().requestAuthorization( 29 options: authOptions, 30 completionHandler: {_, _ in }) 31 } else { 32 let settings: UIUserNotificationSettings = 33 UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil) 34 application.registerUserNotificationSettings(settings) 35 } 36 37 application.registerForRemoteNotifications() 38 39 // [END register_for_notifications] 40 return true 41 } 42 43 // [START receive_message] 44 func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) { 45 // If you are receiving a notification message while your app is in the background, 46 // this callback will not be fired till the user taps on the notification launching the application. 47 // TODO: Handle data of notification 48 // With swizzling disabled you must let Messaging know about the message, for Analytics 49 // Messaging.messaging().appDidReceiveMessage(userInfo) 50 // Print message ID. 51 if let messageID = userInfo[gcmMessageIDKey] { 52 print("Message ID: \(messageID)") 53 } 54 55 // Print full message. 56 print(userInfo) 57 } 58 59 func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], 60 fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { 61 // If you are receiving a notification message while your app is in the background, 62 // this callback will not be fired till the user taps on the notification launching the application. 63 // TODO: Handle data of notification 64 // With swizzling disabled you must let Messaging know about the message, for Analytics 65 // Messaging.messaging().appDidReceiveMessage(userInfo) 66 // Print message ID. 67 if let messageID = userInfo[gcmMessageIDKey] { 68 print("Message ID: \(messageID)") 69 } 70 71 // Print full message. 72 print(userInfo) 73 74 completionHandler(UIBackgroundFetchResult.newData) 75 } 76 // [END receive_message] 77 func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) { 78 print("Unable to register for remote notifications: \(error.localizedDescription)") 79 } 80 81 // This function is added here only for debugging purposes, and can be removed if swizzling is enabled. 82 // If swizzling is disabled then this function must be implemented so that the APNs token can be paired to 83 // the FCM registration token. 84 func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { 85 print("APNs token retrieved: \(deviceToken)") 86 87 // With swizzling disabled you must set the APNs token here. 88 // Messaging.messaging().apnsToken = deviceToken 89 } 90} 91 92// [START ios_10_message_handling] 93@available(iOS 10, *) 94extension AppDelegate : UNUserNotificationCenterDelegate { 95 96 // Receive displayed notifications for iOS 10 devices. 97 func userNotificationCenter(_ center: UNUserNotificationCenter, 98 willPresent notification: UNNotification, 99 withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { 100 let userInfo = notification.request.content.userInfo 101 102 // With swizzling disabled you must let Messaging know about the message, for Analytics 103 // Messaging.messaging().appDidReceiveMessage(userInfo) 104 // Print message ID. 105 if let messageID = userInfo[gcmMessageIDKey] { 106 print("Message ID: \(messageID)") 107 } 108 109 // Print full message. 110 print(userInfo) 111 112 // Change this to your preferred presentation option 113 completionHandler([]) 114 } 115 116 func userNotificationCenter(_ center: UNUserNotificationCenter, 117 didReceive response: UNNotificationResponse, 118 withCompletionHandler completionHandler: @escaping () -> Void) { 119 let userInfo = response.notification.request.content.userInfo 120 // Print message ID. 121 if let messageID = userInfo[gcmMessageIDKey] { 122 print("Message ID: \(messageID)") 123 } 124 125 // Print full message. 126 print(userInfo) 127 128 completionHandler() 129 } 130} 131// [END ios_10_message_handling] 132 133extension AppDelegate : MessagingDelegate { 134 // [START refresh_token] 135 func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) { 136 print("Firebase registration token: \(fcmToken)") 137 } 138 // [END refresh_token] 139 // [START ios_10_data_message] 140 // Receive data messages on iOS 10+ directly from FCM (bypassing APNs) when the app is in the foreground. 141 // To enable direct data messages, you can set Messaging.messaging().shouldEstablishDirectChannel to true. 142 func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) { 143 print("Received data message: \(remoteMessage.appData)") 144 } 145 // [END ios_10_data_message] 146}

###【更新】試したこと

・Firebase公式ガイドに記載されている手順の通りにコードを記述した。
https://firebase.google.com/docs/cloud-messaging/ios/client?hl=ja

・上記公式ガイドにリンク付けされているGitHubのページ記載のクイックスタートをAppDelegateに丸ごとコピペしてみた。
https://github.com/firebase/quickstart-ios/blob/master/messaging/MessagingExampleSwift/AppDelegate.swift#L40-L55

しかし、いずれもうまくいかず、以下のようなエラーがコンソールに出る。

###【更新】発生している問題・エラーメッセージ
実機テストでFirebaseからのリモートPush通知を受け取れない。

イメージ説明

Swift

12017-09-22 15:55:59.494542+0900 Swift4RemotePushTest1[4966:1766557] [Firebase/Core][I-COR000012] Could not locate configuration file: 'GoogleService-Info.plist'. 22017-09-22 15:55:59.494 Swift4RemotePushTest1[4966] <Error> [Firebase/Core][I-COR000012] Could not locate configuration file: 'GoogleService-Info.plist'. 32017-09-22 15:55:59.495386+0900 Swift4RemotePushTest1[4966:1766557] [Firebase/Core][I-COR000005] No app has been configured yet. 42017-09-22 15:55:59.495 Swift4RemotePushTest1[4966] <Error> [Firebase/Core][I-COR000005] No app has been configured yet. 52017-09-22 15:55:59.502100+0900 Swift4RemotePushTest1[4966:1766494] *** Terminating app due to uncaught exception 'com.firebase.core', reason: '`[FIRApp configure];` (`FirebaseApp.configure()` in Swift) could not find a valid GoogleService-Info.plist in your project. Please download one from https://console.firebase.google.com/.' 6*** First throw call stack: 7(0x183f1bd38 0x183430528 0x183f1bc80 0x1025b5830 0x102550c64 0x1025516d0 0x18d39a050 0x18d58d898 0x18d5926e4 0x18d820454 0x18daf01f0 0x18d8200b8 0x18d820928 0x18df896e8 0x18df8958c 0x18dd059c0 0x18de9afc8 0x18dd05870 0x18daef850 0x18d590e28 0x18d9946ec 0x1865bd768 0x1865c6070 0x1036f145c 0x1036fdb74 0x1865f1a04 0x1865f16a8 0x1865f1c44 0x183ec4358 0x183ec42d8 0x183ec3b60 0x183ec1738 0x183de22d8 0x185c73f84 0x18d38e880 0x1025542cc 0x18390656c) 8libc++abi.dylib: terminating with uncaught exception of type NSException 9(lldb)

###補足情報(言語/FW/ツール等のバージョンなど)
開発環境は以下の通りです。
macOS Sierra(10.12.6)
iOS 11.0
Xcode 9.0
Swift 4.0

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

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

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

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

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

guest

回答1

0

ベストアンサー

まず最初に読むべきはFirebaseのドキュメントです。
日本語のドキュメントあります。

iOS での Firebase Cloud Messaging クライアント アプリの設定

参考にしている記事はiOS10以降に対応していないと思います。

投稿2017/09/22 05:08

daisuke7

総合スコア1563

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

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

alan-d-haller

2017/09/22 06:56 編集

ご回答ありがとうございます。 実はお送りいただいた記事は事前に目を通していました。 その上で記載されていた手順の通りにコードを記述したり、GitHubに記載されているクイックスタートのサンプルコードをまとめてコピペするなどして一通り試してみたりもしましたが、やはり端末で通知を受け取ることはできませんでした。 ただ、上記のFirebase公式の記事の解説はやや複雑だったので、通知を受け取るために最低限必要なコードさえわかれば助かるのですが…
daisuke7

2017/09/22 06:46

なるほど、そこまでは試しているのですね。 https://github.com/firebase/quickstart-ios/blob/master/messaging/MessagingExampleSwift/AppDelegate.swift クイックスタートのAppDelegateは最低限に近いコードだと思います。 ここからはじめるのが一番オススメです。 気になったのは、このAppDelegateのコードの場合はFirebaseのメソッド実装入れ替えに頼っているので、Qiitaの記事にあるような「FirebaseAppDelegateProxyEnabledをNO」にするのは駄目です。
alan-d-haller

2017/09/22 07:22

再びのご回答ありがとうございます。 なるほど、そのような仕組みなのですね。 早速、「FirebaseAppDelegateProxyEnabledをNO」を削除した上で、クイックスタートのコードを丸ごとAppDelegateにコピペして実機テストを試みました。 しかし、上記で【更新】試したことで追加したようなエラーメッセージのようなものがコンソールに表示されてしまい、classにはThread 1: signal SIGABRTとエラーが出てしまいます。 これは何が原因なのでしょうか?
daisuke7

2017/09/22 07:30

ログに '`[FIRApp configure];` (`FirebaseApp.configure()` in Swift) could not find a valid GoogleService-Info.plist とあります。 GoogleService-Info.plistを作って組み込んでありますか?
alan-d-haller

2017/09/22 07:46

はい、GoogleService-Info.plistであれば、Firebaseコンソールの該当プロジェクトからダウンロードしてXcodeのInfo.plistの下にドラッグ&ドロップで組み込んであります。
daisuke7

2017/09/22 07:52 編集

ではそのGoogleService-Info.plistをクリックした時にXcode画面右側にでるGoogleService-Info.plistについての情報をチェックしてください。Target Membership ってのがありますが、そこにある各ターゲットで、試しているビルドターゲットがチェックなしになっていませんか? チェックがない場合、ビルドに含まれません。
alan-d-haller

2017/09/22 08:11

ああ、そういうことだったんですね! 早速GoogleService-Info.plistのユーティリティをチェックしたところ、ご指摘のようにTarget Membershipにチェックが付いていませんでした。 チェックをつけた上で再度実機テストでFirebaseからプッシュ通知を飛ばしたところ、おかげさまで今度はちゃんと受け取ることができました! 本当にありがとうございます! ただ、今回はコード自体は完全にクイックスタートのコピペそのままだったので、これから一つずつコードの意味を読み解いて、自分で自在に使いこなせるように勉強していきます。 重ね重ねになりますが、何度も助けてくださり本当に感謝しています。 どうもありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問