通过FCM

时间:2016-10-24 06:37:22

标签: php android curl firebase firebase-cloud-messaging

我想通过PHP脚本将PUSH通知发送给Multiple USERS。当我运行我的脚本时,它显示结果成功,但在我的设备上我没有收到任何通知。

但是当我使用FCM控制台时,我在设备上收到了通知。

PHP脚本:

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


        $conn = mysqli_connect("connection set-up");
        $sql = "Select FirebaseID From Coordinates";
        $result = mysqli_query($conn,$sql);
        $tokens = array();
//var_dump(result);
        if(mysqli_num_rows($result) > 0 ){
                while ($row = mysqli_fetch_assoc($result)) {
                        $tokens[] = $row["FirebaseID"];
                }
        }
    var_dump($tokens);

        mysqli_close($conn);
        $message = array("message" => "Hello World");
        $message_status = send_notification($tokens, $message);
        echo $message_status;
 ?> 

我的结果:

{"multicast_id":6386552330832519***,"success":2,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1477******764957%e8a8d907f9fd7ecd"},{"message_id":"0:1477293027764959%e8a8d907f9f***cd"}]}

编辑:有没有办法检查邮件失败的位置,因为它显示了成功结果...但它没有到达设备

4 个答案:

答案 0 :(得分:0)

请检查您是否正确保存了设备ID,并检查您是否使用了与项目安卓密钥相同的服务器密钥,检查您的Google帐户

答案 1 :(得分:0)

使用这个PHP代码,它可以在你的情况下工作

    <?php
    // API access key from Google API's Console
    define( 'API_ACCESS_KEY', 'YOUR-API-ACCESS-KEY-GOES-HERE' );
    $registrationIds = array( $_GET['id'] );
    // prep the bundle
    $msg = array
    (
        'message'   => 'here is a message. message',
        'title'     => 'This is a title. title',
        'subtitle'  => 'This is a subtitle. subtitle',
        'tickerText'    => 'Ticker text here...Ticker text here...Ticker text here',
        'vibrate'   => 1,
        'sound'     => 1,
        'largeIcon' => 'large_icon',
        'smallIcon' => 'small_icon'
    );
    $fields = array
    (
        'registration_ids'  => $registrationIds,
        'data'          => $msg
    );

    $headers = array
    (
        'Authorization: key=' . API_ACCESS_KEY,
        'Content-Type: application/json'
    );

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

答案 2 :(得分:0)

在你的项目中,你正在添加你从那里创建一个google api控制台帐户时获得的注册ID,你将获得服务器密钥和android密钥https://console.developers.google.com/apis/library?project=tabzen-1341,see截图并阅读google guidl enter image description here < / p>

答案 3 :(得分:0)

如果您的用户超过 1k,那么您必须对数组进行分块,因为 FCM registaion_ids 一次不允许超过 1k id :

$value = array_chunk($userList,900);


            foreach ($value as $val)
            {
                $db->sendPushCustomNew("test" . " Posted a Gallery", $createdDate, $val, "1", "Gotcha!", 0, 0, 0);
            }

然后你必须配置你的通知发送功能,如下所示:

function sendPushCustomNew($message, $currentDate, $tokens, $notificationType, $title, $latitude, $longitude, $memberID)
{
    //error_log("IN_PUSH");
    $curl = curl_init();
    $fields = array(
        'data' => array(
            'notificationType' => $notificationType,
            'title' => $title,
            "message" => "" . $message,
            "time" => "" . $currentDate,
            'latitude' => $latitude,
            'longitude' => $longitude
        ),
        'registration_ids' => $tokens
    );
    curl_setopt_array($curl, array(
        CURLOPT_URL => "https://fcm.googleapis.com/fcm/send",
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => "",
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 30,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => "POST",
        CURLOPT_POSTFIELDS => json_encode($fields),
        CURLOPT_HTTPHEADER => array(
            "authorization: ******",
            "cache-control: no-cache",
            "content-type: application/json",
            "postman-token: ***"
        )
    ));

    $response = curl_exec($curl);
   
    $err = curl_error($curl);
    curl_close($curl);
}