現在、FCMを使ってAndroidアプリにPush通知機能を送っています。
サーバー側(PHP)からトピックを管理できるよう、
以下サイトを参考にトピックを作成〜メッセージの送信までの処理を記述いたしました。
▼参考サイト
https://firebase-php.readthedocs.io/en/5.11.0/cloud-messaging.html#send-messages-to-topics
PHP
1use Kreait\Firebase\Messaging\CloudMessage; 2 3// サービスアカウントのパスを指定 4$factory = (new Factory)>withServiceAccount('my paths'); 5$messaging = $factory->createMessaging(); 6 7// トピックを作成 8$topic = 'test-topic'; 9 10// 送信対象のデバイストークン 11$deviceTokens = array("token1", "token2"); 12 13// トピックにデバイストークンを登録 14$messaging->subscribeToTopic($topic, $deviceTokens); 15 16// 通知内容を作成 17$notification = Notification::fromArray([ 18 'title' => "テストタイトル", 19 'body' => "テスト本文" 20]); 21 22// トピックに対してPush通知を送信 23$message = CloudMessage::fromArray([ 24 'topic' => $topic, 25 'notification' => $notification 26]); 27 28// こちらをvar_dumpすると、正しいトークンでも間違ったトークンでも以下が表示される 29// ex) 'name' => string 'projects/my-project/messages/111111' 30$messaging->send($message);
無事Push通知は送信できたのですが、
送信後のレスポンスの中身(success / failure)の取得の仕方が分からず困っています。
参考サイトの「複数デバイスに送信する」の項目には結果の取得方法が記載されていたのですが、
「トピックに送信する」の項目には結果の取得方法は記載されていませんでした。
▼ご参考までに、「複数デバイスに送信する」の項目に記載されていた結果の取得方法を記載いたします。
PHP
1$report = $messaging->sendMulticast($message, $deviceTokens); 2 3echo 'Successful sends: '.$report->successes()->count().PHP_EOL; 4echo 'Failed sends: '.$report->failures()->count().PHP_EOL; 5 6if ($report->hasFailures()) { 7 foreach ($report->failures()->getItems() as $failure) { 8 echo $failure->error()->getMessage().PHP_EOL; 9 } 10}
何かしらクラスやメソッドは用意されていると思うのですが、
ちゃんとPush通知が送られているのかサーバー側で確認する方法をご存知であれば、
ご教示いただけますと幸いです。
よろしくお願いいたします。
あなたの回答
tips
プレビュー