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

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

解決済

3回答

1892閲覧

FCMでiOSアプリにPush通知が来ない

Ituki0130

総合スコア12

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クリップ

投稿2019/01/10 14:16

編集2019/01/10 15:01

前提・実現したいこと

iOSアプリをxcodeで作っています。
FirebaseCloudMessagingを用いてプッシュ通知を送ろうとしています。

xcodeでこちらのページを参考にFCMを用いてプッシュ通知を実装しました。APNs証明書ではなく認証キーを使っています。実機デバックでは通知ができました。
ですが、いざストアにアップロードしてみると、アプリから通知の許可は求められるものの、通知が来ませんでした。
どのように実装すれば、通知が来るようになるのでしょうか??

AppDelegateのコードです

swift

1import UIKit 2import Firebase 3import FirebaseMessaging 4import UserNotifications 5 6@UIApplicationMain 7 8class AppDelegate: UIResponder, UIApplicationDelegate { 9 10 var window: UIWindow? 11 12 13 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { 14 15 FirebaseApp.configure() 16 17 if #available(iOS 10.0, *) { 18 // For iOS 10 display notification (sent via APNS) 19 UNUserNotificationCenter.current().delegate = self 20 21 let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound] 22 UNUserNotificationCenter.current().requestAuthorization( 23 options: authOptions, 24 completionHandler: {_, _ in }) 25 } else { 26 let settings: UIUserNotificationSettings = 27 UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil) 28 application.registerUserNotificationSettings(settings) 29 } 30 31 application.registerForRemoteNotifications() 32 // Override point for customization after application launch. 33 return true 34 } 35 36 func applicationWillResignActive(_ application: UIApplication) { 37 // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 38 // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. 39 } 40 41 func applicationDidEnterBackground(_ application: UIApplication) { 42 // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 43 // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 44 } 45 46 func applicationWillEnterForeground(_ application: UIApplication) { 47 // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. 48 } 49 50 func applicationDidBecomeActive(_ application: UIApplication) { 51 // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 52 } 53 54 func applicationWillTerminate(_ application: UIApplication) { 55 // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 56 } 57 58 59} 60 61func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) { 62 // Print message ID. 63 if let messageID = userInfo["gcm.message_id"] { 64 print("Message ID: (messageID)") 65 } 66 67 // Print full message. 68 print(userInfo) 69} 70 71 72func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], 73 fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { 74 // Print message ID. 75 if let messageID = userInfo["gcm.message_id"] { 76 print("Message ID: (messageID)") 77 } 78 79 // Print full message. 80 print(userInfo) 81 82 completionHandler(UIBackgroundFetchResult.newData) 83} 84 85@available(iOS 10, *) 86extension AppDelegate : UNUserNotificationCenterDelegate { 87 func userNotificationCenter(_ center: UNUserNotificationCenter, 88 willPresent notification: UNNotification, 89 withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { 90 let userInfo = notification.request.content.userInfo 91 92 if let messageID = userInfo["gcm.message_id"] { 93 print("Message ID: (messageID)") 94 } 95 96 print(userInfo) 97 98 completionHandler([]) 99 } 100 101 func userNotificationCenter(_ center: UNUserNotificationCenter, 102 didReceive response: UNNotificationResponse, 103 withCompletionHandler completionHandler: @escaping () -> Void) { 104 let userInfo = response.notification.request.content.userInfo 105 if let messageID = userInfo["gcm.message_id"] { 106 print("Message ID: (messageID)") 107 } 108 109 print(userInfo) 110 111 completionHandler() 112 } 113}

懸念点

上記の参考にしたページは、Appデベロッパーで端末登録をしているため、それに代替する作業が必要なのではと考えています。
調べているうちに、代替作業はTokenの実装なのではないかと思ったのですが、よくわかっていません。
よろしくお願いします。

補足情報

Xcode9.2

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

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

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

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

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

xenbeat

2019/01/12 08:58

> いざストアにアップロードしてみると、アプリから通知の許可は求められるものの、通知が来ませんでした。 実機デバックでは届く、ストアでは届かないというよりも、 フォアグラウンドでは届く、バックグラウンドでは届かない ではないでしょうか?
Ituki0130

2019/01/13 04:57

いえ。フォア、バックグラウンド関係なく、xcodeからの実機シミュレートは通知に成功したのですが、ストアにリリースしたアプリでは通知されないという状態です。
guest

回答3

0

自己解決

自己解決いたしました。

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

皆さんご回答いただきありがとうございました。

投稿2019/01/26 11:00

Ituki0130

総合スコア12

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

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

sacakoro

2020/07/04 10:04

初めまして。私もFCMを使用したプッシュ通知がうまくできず悩んでおります。 >調べているうちに、「FireBaseの対応が追い付かず、リリース直後はCloudMessagingが動作しない事がある」 という記述はどこにあったのか、教えていただきたいのですが可能でしょうか
guest

0

willPresentのdeleteメソッドの処理で

swift

1completionHandler([])

このようにすると、アプリがフォアグラウンドにいる時、
push通知のメッセージは出ないと思います。
実機デバッグでは通知ができたとのことですが、
今でも実機デバッグでアプリがフォアグラウンドにいる時
通知メッセージが出ますか?

正しくは

swift

1completionHandler([.sound, .alert])

としないと、アプリがフォアグラウンドにいる時
通知メッセージや通知音は出ないと思います。

投稿2019/01/20 17:48

TakeOne

総合スコア6299

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

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

0

FCM用のiOSのPush通知証明書は同じCSRで開発用と本番用を作っては絶対にダメ
このあたりではないでしょうか?

上記の参考にしたページは、Appデベロッパーで端末登録をしているため、それに代替する作業が必要なのではと考えています。

ストアにアップロードするため、リリース用(Distribution)のProvisioning Profileを作成したと思いますが、そちらでの端末登録等はありませんので、そこが問題ではないと思います。

投稿2019/01/16 03:24

taka_jun

総合スコア160

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

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

Ituki0130

2019/01/16 14:14

ご回答ありがとうございます。APNsは証明書ではなく、認証キーをつかっているのですが、それでも本番用と開発用の区別があるのでしょうか?? お教えいただけると幸いです。よろしくお願いいたします。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問