Help us understand the problem. What is going on with this article?

Firebaseを利用して特定端末へのpush通知

More than 3 years have passed since last update.

Android + PHP でテスト

Android

ここを見て実装
https://firebase.google.com/docs/notifications/android/console-device

sendRegistrationToServerメソッドで自サーバにpush通知用のトークン送信する処理書けばok。
もちろんサーバ側ではトークンを格納する処理を書く

サーバからの特定端末への通知

Ruby使えたらgemとかありそうだったんですがPHPしか使えないサーバだったので。
こんな感じでクラス作ってNotification::sendで通知。
$pushTokensに対象となる複数のトークン(配列)を設定してメッセージ設定したら送信できる。

notification.php
class Notification {
  private static $apiKey = "hoge";  // Firebase console:Overview > プロジェクトの設定 > クラウドメッセージング > サーバーキー
  private static $url = "https://fcm.googleapis.com/fcm/send";

  static public function send($pushTokens, $message) {
    $headers = [
      "Authorization: key=".self::$apiKey,
      "Content-Type: application/json"
    ];

    $fields = [
      "registration_ids" => is_array($pushTokens) ? $pushTokens : [$pushTokens],
      "notification" => [
        "text" => $message
      ]
    ];

    $handle = curl_init();
    curl_setopt($handle, CURLOPT_URL, self::$url);
    curl_setopt($handle, CURLOPT_POST, true);
    curl_setopt($handle, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($handle, CURLOPT_POSTFIELDS, json_encode($fields));
    $result = curl_exec($handle);
    curl_close($handle);
    return $result;
}

簡単でした。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away