简体   繁体   English

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

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

I'm building an android application with a notification feature. 我正在构建具有通知功能的android应用程序。 How to be (Push Notifications - topics messaging) using FCM and php ..? 如何使用FCM和php ..(推送通知-主题消息传递)?

As it is clear from the documentation 从文档中可以明显看出
1. In Your Android App, subscribe to the topics say "news" with 1.在您的Android应用中,订阅主题为“新闻”的

FirebaseMessaging.getInstance().subscribeToTopic("news");

2. Send to a single topic: 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. Send to devices subscribed to topics "news" or "cats": 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!",
   }
}

Read the documentation at https://firebase.google.com/docs/cloud-messaging/android/topic-messaging 阅读https://firebase.google.com/docs/cloud-messaging/android/topic-messaging中的文档

EDIT:- PHP Curl 编辑: -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);

Note:- use your Authorization:key from the Firebase project settings. 注意:-使用Firebase项目设置中的Authorization:key。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM