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

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

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

2869閲覧

Firebase Cloud Messaging:インストール直後には、プッシュ通知が届かない

退会済みユーザー

退会済みユーザー

総合スコア0

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

投稿2021/08/17 10:03

インストール直後(実機にて実行)のみ通知が届かず困っております。

環境
Xcode:12.4
Swift: 5.4.2

FirebaseCloudMessagingより通知を配信しているのですが、実機にインストール後だと通知が届かず、添付画像のコンソールにはこのように出力されます。Firebaseの通知ステータスも「完了」と表示されており届いていないわけではないようでした。
イメージ説明

一度タスクキルしてから、通知を配信すると届きます。
いろいろ調べてみたのですが、同じような内容が見当たらず何をすればいいのか分かりません。
以下はAppDelegate.swiftのコード内容になります。

import UIKit import Firebase import FirebaseMessaging import UserNotifications @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. FirebaseApp.configure() 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() 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. } 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, *) extension AppDelegate : UNUserNotificationCenterDelegate { 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() } }

これは仕様なのでしょうか?
タスクキルせずに、インストール直後でも通知を受け取れるようにしたいです。
わかる方いましたらご教示のほど宜しくお願い致します。

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

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

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

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

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

guest

回答1

0

ベストアンサー

registerForRemoteNotifications が、requestAuthorization のクロージャの外で実行されているため、プッシュ通知の許可依頼画面を表示している間に registerForRemoteNotifications が実行されてしまい、この登録処理が空振りしたのだと思います。

requestAuthorization のクロージャの中で、ユーザーの選択結果を確認してregisterForRemoteNotifications を実行する必要があると思います。

(参考)
https://qiita.com/jollyjoester/items/63991657f0d631383c9b


(8/19 追記)

多くの記事がrequestAuthorizationで通知許可を得たタイミングでregisterForRemoteNotificationsを実施しているため、通知許可を得てからregisterForRemoteNotificationsを実施する必要があると認識していましたが、FCMのサンプルはクロージャの外で通知許可を得る前にregisterForRemoteNotificationsを実施していたので、実際の動きを確かめてみたところ、先にregisterForRemoteNotificationsを実施しても問題ないようでした。ただ、順番的には通知許可を得てからregisterForRemoteNotificationsを実施するよう実装した方が無難ではあると思います。

他に問題がないかコードを確認してみたところ、もし今問題となっている動作が、「実機にインストール後だと通知が届かない」というよりも、「アプリを画面に表示している状態だと画面上部にバナーが表示されないが
アプリを終了またはスタンバイ状態にしているとバナーが表示される」という問題なのであれば、

willPresentのdelegateメソッドの中で

swift

1completionHandler([])

このようにして通知の種類なしでcompletionHandlerを呼び出しているのが問題の原因だと思います。

ここは、参考記事で説明されているように

swuft

1completionHandler([.badge, .sound, .alert])

このようにしないと、アプリを画面に表示している状態でバナーや通知音が出ません。

なお、ターゲットがiOS14以上の場合は、 .alert がdeprecatedになり、代わりに .banner.list の2種類を別々に指定するようになっているので、次のようにする必要があります。

swuft

1completionHandler([.badge, .sound, .banner, .list])

投稿2021/08/18 05:31

編集2021/08/21 23:54
TakeOne

総合スコア6299

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

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

退会済みユーザー

退会済みユーザー

2021/08/19 00:42

ご回答頂きありがとうございます。 requestAuthorization のクロージャにregisterForRemoteNotificationsを記述して実行してみたのですが、同じ結果でした。コンソールの出力も質問時と同じ内容が出力されました。 https://firebase.google.com/docs/cloud-messaging/ios/client?hl=ja また上記サイトを確認したところ、FCMトークンの更新をすれば通知を取得できるのかと思い、 Messaging.messaging().delegate = selfでデリゲート指定をし、 func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) メソッドを記述しました。しかし、これだけだと「APNsデバイストークンの設定がされておりません」という記載がコンソールに出力されたので、func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data)メソッドを記述して実行してみました。 ですが、結果は同じでコンソール上には届いているみたいですが、通知は届きませんでした。また通知許可のダイアログ表示もなく許可の処理が走りました。 もし他でなにか方法をご存知なら、ご教示のほど宜しくお願い致します。
TakeOne

2021/08/19 05:24

上記のコメントを受けて回答を追記しました。
退会済みユーザー

退会済みユーザー

2021/08/21 17:57

返信が遅くなってしまい申し訳ありません。 ご回答ありがとうございます。 TakeOneさんの仰る通り、受信した際の表示処理を記述していなかったために「届いていない」ようなかたちになっておりました。確認不足でした、、、 アドバイス通りに記述したところ、初回起動時でもしっかり表示がされました! お騒がせしました、ありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問