繁体   English   中英

使用PHP的Android Firebase推送通知

[英]Android Firebase Push notification using php

我为我的应用程序实施了FCM。 我正在使用php发送有关DB中已注册ID的通知。 我想为每个用户发送一条特定的消息。 现在,当我推送通知时,所有用户都会收到相同的消息。

示例:我在数据库中有两个注册用户:1-名为John的用户,他的自定义消息为“ Welcome” 2-名为Marco的用户,他的自定义消息为“生日快乐”

现在,当我推送通知时,所有两个用户都收到相同的消息“ Welcome”。

这是我的代码:

<?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;
?>

您可以一次发送带有多个令牌的fcm(如果我没记错的话,可以发送1000个令牌),但是,您不能发送多个消息

您发送的消息是查询中最后一条记录中的消息

 $message = $row_users["message"];

如果您想发送其他信息,则需要致电

send_notification($tokens, $message);

多次。

暂无
暂无

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

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