FCM Push Notif脚本

时间:2017-04-06 13:33:18

标签: php android

我按照教程使用Android Studio执行Firebase云消息传递推送通知。 这是tuto:https://www.youtube.com/watch?v=MYZVhs6T_W8 所以我的push_notification.php出了问题:

<?php 
    function send_notification ($tokens, $message)
    {
        $url = 'https://fcm.googleapis.com/fcm/send';
        $fields = array(
             'registration_ids' => $tokens,
             'data' => $message
            );
        $headers = array(
            'Authorization:key = AAAALXOnumg:APA91bGI7Qfrl0XVX52KOfjfKfzFcezbD6mwwIti7oswMAvGn3Z9Q1toxJ9IiXiXRFc_vB21NRnwNaI-vYPxcEMO5JH53RrfAtnjxyA5fzaiwONSnLY987ephLapy0ijTyW9VLwwoPBJSq',
            '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;
    }

    $conn = mysqli_connect("localhost","root","toor","gcm");
    $sql = " Select Token From users";
    $result = mysqli_query($conn,$sql);
    $tokens = array();
    if(mysqli_num_rows($result) > 0 ){
        while ($row = mysqli_fetch_assoc($result)) {
            $tokens[] = $row["Token"];
        }
    }
    mysqli_close($conn);
    $message = array("message" => " FCM PUSH NOTIFICATION TEST MESSAGE");
    $message_status = send_notification($tokens, $message);
    echo $message_status;
 ?>

当我在浏览器上执行它时会出现结果:[HTTP / 1.0 500内部服务器错误1ms]

我的数据库ID是正确的,也是Auth Key,所以问题来自哪里?

1 个答案:

答案 0 :(得分:0)

$conn = mysqli_connect("localhost","root","toor","gcm");

请检查这些详细信息(主机,dbname,用户名,密码)。

相关问题