簡體   English   中英

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

[英]How do I create push notifications (Topics Messaging) using FCM and PHP?

我正在構建具有通知功能的android應用程序。 如何使用FCM和php ..(推送通知-主題消息傳遞)?

從文檔中可以明顯看出
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項目設置中的Authorization:key。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM