【问题标题】:send the PUSH notification to multiple users Through FCM通过 FCM 向多个用户发送 PUSH 通知
【发布时间】:2017-03-05 20:32:24
【问题描述】:

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

但是当我使用 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"}]}

编辑:有什么方法可以检查消息失败的位置,因为它显示成功的结果......但它没有到达设备

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

    • 两者都是正确的,我也在我的数据库中定期更新我的设备ID
    【解决方案2】:

    在你的情况下使用这个 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;
    

    【讨论】:

    • 然后从您的数据库中获取注册ID,然后在代码中使用
    • 我做到了,并检查是否使用 var_dump 工作
    • 当我硬编码注册 ID 数组时,也会出现同样的问题
    • @NiteshPareek 注册 ID 是否等于设备令牌?
    【解决方案3】:

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

    【讨论】:

    • 我的代码一切正常,结果也显示成功,但问题是我没有收到任何消息....帮帮我
    • @techDigi - 你解决过这个问题吗?我遇到了同样的问题,我在响应中获得了成功,但从未收到通知。我是否必须将运行它的服务器添加到白名单服务器列表中以允许它发送?
    【解决方案4】:

    如果您的用户超过 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);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-17
      • 2017-11-08
      相关资源
      最近更新 更多