簡體   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