简体   繁体   中英

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

I'm building an android application with a notification feature. How to be (Push Notifications - topics messaging) using FCM and php ..?

As it is clear from the documentation
1. In Your Android App, subscribe to the topics say "news" with

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

2. Send to a single topic:

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":

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

EDIT:- 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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