使用PHP从Db获取多个设备令牌并发送FCm

时间:2017-07-23 09:01:27

标签: php mysql firebase firebase-cloud-messaging

我必须从Db获取设备令牌并将其发送到多个设备 现在我有查询从DB

获取多个设备令牌
$queryname1 = "SELECT device_id FROM  register where id IN ($particpants)";
    $resultname1 = mysql_query($queryname1) or die('Errant query:  ' . $queryname1);
          $idarray = array();
               while ($idrows = mysql_fetch_assoc($resultname1))
              {
                  $idarray = $idrows['device_id'];
              }

我的FCm代码在下面我无法发送fcm通知这里我们的代码

    define("API_ACCESS_KEY", "********"); 
$registrationIds=$idarray;
         $msg = array
              (
            'body'  => 'Body  Of Notification',
            'title' => 'Title Of Notification',
                    'icon'  => 'myicon',/*Default Icon*/
                    'sound' => 'mySound'/*Default sound*/
              );
        $fields = array
                (
                    'to'        => $registrationIds,
                    'notification'  => $msg
                );
                //print_r($fields);
                //exit;

        //'registration_ids'  => array($registrationIDs),
        $headers = array
                (
                    'Authorization: key=' . API_ACCESS_KEY,
                    'Content-Type: application/json'
                );
    #Send Reponse To FireBase Server    
            $ch = curl_init();
            curl_setopt( $ch,CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send' );
            curl_setopt( $ch,CURLOPT_POST, true );
            curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
            curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
            curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
            curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
            $result = curl_exec($ch );
            curl_close( $ch );

            echo $result;

1 个答案:

答案 0 :(得分:0)

为FCM代码创建一个函数并在循环中调用该函数。

EX:

 while ($idrows = mysql_fetch_assoc($resultname1)){
     $idarray = $idrows['device_id'];
     sendNotification($idrows['device_id']);
 }

insde sendNotification()放入你的FCM代码。