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

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

新規登録して質問してみよう
ただいま回答率
85.50%
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

0回答

2164閲覧

FCM が機能しない

RyomaD

総合スコア34

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/03/30 13:36

【問題】
IOSアプリでFCMを実装中なのですが、firebase console上からpush通知を送信できないでいます。

【試したこと】
何度か証明書周りを生成し直したり、xcodeのキャッシュをクリアしました。
証明書周りが怪しかったのでonesignalを利用してpush通知を受け取れるか試したら、正常にpush通知を受け取ることができました。(onesignalでの設定時にも証明書を使用するので、証明書の失効ではない)

なので問題の原因は証明書周りではなくコードかと思ったのですが、実は以前まではFCMで正常にpush通知を受け取ることができていたのでswift,firebaseの大きなアップデートがない限り、コードが悪さしているとは思えない・・。

Provisioning fileも最新にアップデートしました。

AdHocで配信しているアプリはPush通知を受信しない

との情報を発見したので現在の検証は開発環境で行なっております。

APNSで通知が飛ばない場合に考えられる問題も参考にしたのですが、
Onesignalからpush通知が受信できたことを考えると当てはまらない事項が多かったです。

【教えて欲しいこと】
上記を踏まえた上で何か見落としている部分があればご教示いただけないでしょうか?
よろしくお願いします。

以下はpush通知を受け取るためのAppdelegate.swiftファイル内コードになります。

swift

1 2import UIKit 3import AVFoundation 4import Firebase 5import FBSDKCoreKit 6import FBSDKLoginKit 7import FacebookCore 8import FirebaseDatabase 9import UserNotifications 10import FirebaseMessaging 11import OneSignal 12 13//追加 14import CallKit 15import PushKit 16import SkyWay 17 18 19@UIApplicationMain 20class AppDelegate: UIResponder, UIApplicationDelegate, MessagingDelegate, CXProviderDelegate, PKPushRegistryDelegate { 21 22 23 override init() { 24 super.init() 25 //FirebaseApp 共有インスタンスを設定します。 26 FirebaseApp.configure() 27 28 print("(UUIDs)========UUIDs=======") 29 print("(UUID())========UUID=======") 30 31 } 32 33 34 35 36 //アプリを再起動とかしてもここは最初しか呼ばれない 37 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 38 // Override point for customization after application launch. 39 // PushKit 40 let registry = PKPushRegistry(queue: nil) 41 registry.delegate = self 42 registry.desiredPushTypes = [PKPushType.voIP] 43 44 // Converted to Swift 4 by Swiftify v4.1.6781 - https://objectivec2swift.com/ 45 FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions) 46 47 //追加コード 48 SDKApplicationDelegate.shared.application(application, didFinishLaunchingWithOptions: launchOptions) 49 50 //これで、ユーザーが初回にアプリを立ち上げるとプッシュ通知の許可ダイアログが出る 51 //push通知のための追加コード 52 if #available(iOS 10.0, *) { 53 // For iOS 10 display notification (sent via APNS) 54 UNUserNotificationCenter.current().delegate = self 55 56 let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound] 57 UNUserNotificationCenter.current().requestAuthorization( 58 options: authOptions, 59 completionHandler: {_, _ in }) 60 } else { 61 let settings: UIUserNotificationSettings = 62 UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil) 63 application.registerUserNotificationSettings(settings) 64 } 65 66 67 // アプリ起動時にFCMのトークンを取得し、表示する 68 let token = Messaging.messaging().fcmToken 69 70 print("FCM token: (token ?? "")") 71 72 //userIdがあったらfcmTokenと一緒にポストする 73 UserDefaults.standard.set(token, forKey: "FCM_TOKEN") 74 print("Firebase registration token: (token)") 75 76 Messaging.messaging().delegate = self 77 78 application.registerForRemoteNotifications() 79 Messaging.messaging().isAutoInitEnabled = true 80 81 let onesignalInitSettings = [kOSSettingsKeyAutoPrompt: false] 82 83 84 OneSignal.initWithLaunchOptions(launchOptions, 85 appId: "2e92****************a7f", 86 handleNotificationAction: nil, 87 settings: onesignalInitSettings) 88 89 90 OneSignal.inFocusDisplayType = OSNotificationDisplayType.notification; 91 92 OneSignal.promptForPushNotifications(userResponse: { accepted in 93 print("User accepted notifications: (accepted)") 94 }) 95 96 97 98 99 return true 100 } 101 102 103 104 // Handle updated push credentials 105 func pushRegistry(registry: PKPushRegistry!, didUpdatePushCredentials credentials: PKPushCredentials!, forType type: String!) { 106 // Register VoIP push token (a property of PKPushCredentials) with server 107 } 108 109 110 // 明示的にAPNsトークンをFCM登録トークンにマッピング 111 func application(application: UIApplication, 112 didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { 113 Messaging.messaging().apnsToken = deviceToken 114 115 print("sdfksjkjfksajkdnslfs") 116 } 117 118 119 //ここは特に動いていない(push通知) 120 func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) { 121 122 123 //userIdがあったらfcmTokenと一緒にポストする 124 UserDefaults.standard.set(fcmToken, forKey: "FCM_TOKEN") 125 print("Firebase registration token: (fcmToken)") 126 127 // TODO: If necessary send token to application server. 128 // Note: This callback is fired at each app startup and whenever a new token is generated. 129 } 130 131 //ここは特に動いていない(push通知) 132 func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) { 133 print("Received data message: (remoteMessage.appData)") 134 } 135 136 137 //追加コード 138 func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool { 139 return SDKApplicationDelegate.shared.application(application, 140 open: url, 141 sourceApplication: sourceApplication, 142 annotation: annotation) 143 } 144 145 // サイレント通知を受け取った場合以下のメソッドで通知を受け取ります。 146 func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) { 147 print("受信時に走るよ======================") 148 // Print message ID. 149 if let messageID = userInfo["gcm.message_id"] { 150 print("Message ID: (messageID)") 151 } 152 153 // Print full message. 154 print(userInfo) 155 } 156 157 // サイレントを受信したら以下メソッドが走るからこの中でデータ更新などの処理を記述 158 // (http://www.cl9.info/entry/2017/10/14/145342) 159 func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], 160 fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { 161 print("受信時に走るよ======================") 162 // Print message ID. 163 if let messageID = userInfo["gcm.message_id"] { 164 print("Message ID: (messageID)") 165 } 166 // Print full message. 167 print(userInfo) 168 169 completionHandler(UIBackgroundFetchResult.newData) 170 } 171 172 173 174 175 176 177 178 179} 180// Onesignalからpush通知を送信した時は以下のメソッドが走っている 181@available(iOS 10, *) 182extension AppDelegate : UNUserNotificationCenterDelegate { 183 // モバイルアプリがフォアグラウンドで動作中にメッセージを受信したときに呼ばれる。 184 func userNotificationCenter(_ center: UNUserNotificationCenter, 185 willPresent notification: UNNotification, 186 withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { 187 let userInfo = notification.request.content.userInfo 188 let message = "相手が見つかりました。" 189 190 if let messageID = userInfo["gcm.message_id"] { 191 print("Message ID: (messageID)") 192 print("push通知テスト①") 193 completionHandler([.alert, .sound]) 194 195 } 196 197 198 print(userInfo) 199 200 201 202 } 203 204 func userNotificationCenter(_ center: UNUserNotificationCenter, 205 didReceive response: UNNotificationResponse, 206 withCompletionHandler completionHandler: @escaping () -> Void) { 207 let userInfo = response.notification.request.content.userInfo 208 if let messageID = userInfo["gcm.message_id"] { 209 print("Message ID: (messageID)") 210 print("push通知テスト②") 211 } 212 213 print(userInfo) 214 215 216 } 217} 218

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

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

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

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

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

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

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

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

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問