简体   繁体   中英

Android Firebase Push notification using php

I implemented FCM for my app. I'm using php to send the notification for the registered ids in my DB . I want to send a specific message for each user. Now when I push the notification, I'm getting the same message for all the users.

Example: I have two registered users in the DB: 1- User called John and his custom message is "Welcome" 2- User called Marco and his custom message is "Happy birth day"

Now when I push the notification all the two users are getting the same message "Welcome".

This is my code:

<?php 
function send_notification ($tokens, $message)
{
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array(
   'registration_ids' => $tokens,
   'data' => $message,
   'priority'             => "high",

  );
$headers = array(
  'Authorization:key =  FCM Server Key',
  'Content-Type: application/json'
  );
 $ch = curl_init();
   curl_setopt($ch, CURLOPT_URL, $url);
   curl_setopt($ch, CURLOPT_POST, true);
   curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
   curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);  
   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
   curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
   $result = curl_exec($ch);           
   if ($result === FALSE) {
       die('Curl failed: ' . curl_error($ch));
   }
   curl_close($ch);
   return $result;
   }

   include("conn.php");
   mysql_select_db($db, $conn);
   $query_users = sprintf("Select token, message From users");
   $users = mysql_query($query_users, $conn) or die(mysql_error());
   $row_users = mysql_fetch_assoc($users);
   $totalRows_users = mysql_num_rows($users);

   if($totalRows_users > 0 ) {
    do { 

      $tokens[] = $row_users["token"];

      $message = $row_users["message"];
   } while ($row_users = mysql_fetch_assoc($users));
   }

   $message_status = send_notification($tokens, $message);
   echo $message_status;
?>

you can send fcm with multiple token at once (1000 token limit if i'm not mistake), but, you cannot send with multiple messages

the message that you send is the message from your last record in your queries

 $message = $row_users["message"];

if you want to send with different message, then you need to call

send_notification($tokens, $message);

multiple times.

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