如何使用FCM和PHP创建推送通知(主题消息)?

时间:2017-01-25 06:37:24

标签: php android firebase firebase-cloud-messaging

我正在构建一个带有通知功能的Android应用程序。如何(推送通知 - 主题消息)使用FCM和PHP ..?

1 个答案:

答案 0 :(得分:0)

从文件中可以清楚地看出 1.在您的Android应用程序中,使用

订阅主题“新闻”
FirebaseMessaging.getInstance().subscribeToTopic("news");

2。发送到一个主题:

https://fcm.googleapis.com/fcm/send
Content-Type:application/json
Authorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA

{
  "to": "/topics/news",
  "data": {
    "message": "This is a Firebase Cloud Messaging Topic Message!",
   }
}

3。发送到订阅主题“新闻”或“猫”的设备:

https://fcm.googleapis.com/fcm/send
Content-Type:application/json
Authorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA

{
  "condition": "'news' in topics || 'cats' in topics",
  "data": {
    "message": "This is a Firebase Cloud Messaging Topic Message!",
   }
}

阅读https://firebase.google.com/docs/cloud-messaging/android/topic-messaging

上的文档

编辑: - PHP Curl

  $data = array(
           "message" => "This is a Firebase Cloud Messaging Topic Message!"
          );
  $final = array(
            "condition" => "'news' in topics",
            "data" => $data
           );

  $url = 'https://fcm.googleapis.com/fcm/send';

  $headers = array(
              'Authorization: key=YOUR_API_KEY',
              'Content-Type: application/json'
             );
  // Open connection
  $ch = curl_init();

  // Set the url, number of POST vars, POST data
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_POST, true);
  curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  // Disabling SSL Certificate support temporarly
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($final));
  // Execute post
  $result = curl_exec($ch);

  if ($result === FALSE) {
      // curl failed
  }

  // Close connection
  curl_close($ch);

注意: - 使用Firebase项目设置中的授权:密钥。