2017-05-06 178 views
0

我为我的应用程序实现了FCM。我正在使用PHP发送注册ID在我的数据库中的通知。我想为每个用户发送特定的消息。现在,当我推送通知时,我收到了所有用户的相同消息。Android Firebase推送通知使用php

例子: 我有两个注册用户在DB: 1 - 用户名为约翰和他的自定义消息“欢迎” 2-用户名为Marco和他的自定义消息是“快乐诞生之日”

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

这是我的代码:

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

回答

0

您可以使用多个令牌发送FCM一次(1000令牌限制,如果我没有弄错),但是,你不能用多个消息发送

您发送的消息是从你的最后一条记录在查询

$message = $row_users["message"]; 
如果你想用不同的邮件发送

,次消息您需要致电

send_notification($tokens, $message); 

多次。