【问题标题】:GCM PHP - Passing Two-Dimensional ArrayGCM PHP - 传递二维数组
【发布时间】:2015-10-22 03:33:37
【问题描述】:

我正在尝试使用 Google Cloud Messaging (GCM) 将二维数组推送到设备。但是,我发现 GCM 一次只能发送一个数组。

我正在寻找解决此问题的解决方案。因为我觉得一直一个一个推送信息是不切实际的。

以下是两种不同的场景

单维数组(成功推送到设备)

Array
(
    [pump_name] => LEVO 92
    [pump_price] => 2.5
)
1
{"multicast_id":8959934119853137719,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1445482576757470%9dde29c5f9fd7ecd"}]}
Array
(
    [pump_name] => LEVO 95
    [pump_price] => 3
)
1
{"multicast_id":6988128903201803494,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1445482576797158%9dde29c5f9fd7ecd"}]}

二维数组(无法推送到设备)

Array
(
    [0] => Array
        (
            [pump_name] => LEVO 92
            [pump_price] => 2.5
        )

    [1] => Array
        (
            [pump_name] => LEVO 95
            [pump_price] => 3
        )

)
1
Field "data" must be a JSON array: [{"pump_name":"LEVO 92","pump_price":"2.5"},{"pump_name":"LEVO 95","pump_price":"3"}]

代码(我把它分成两部分)

拉高价格函数

function pump_price() {

    global $wpdb;

    $result = array();
    $temp_result = array();
    $counter = 1;

    $rows = $wpdb->get_results($wpdb->prepare("SELECT DISTINCT meta_value FROM postmeta WHERE meta_key LIKE %s", 'pump_price_list_%', 'pump_%'));

    if($rows) :
        foreach($rows AS $row) :
            if($counter % 2 == 0) :

                // Store Value Into Temporary Array
                $temp_result["pump_price"] = $row->meta_value;

                array_push($result, $temp_result);

                // Unset Temporary Array
                unset($temp_result);
                $temp_result = array();

            else :

                $temp_result['pump_name'] = $row->meta_value;

            endif;
            $counter++;
        endforeach;
    endif;

    sendGoogleCloudMessage($result);

}

GCM 函数

function sendGoogleCloudMessage($result) {

    define( 'API_ACCESS_KEY', 'MY-API-KEY' );

    $registrationIds = array('MY-DEVICE-ID');

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

    $fields = array (
        'registration_ids'  => $registrationIds,
        'data'              => $result
    );


    $ch = curl_init();
    curl_setopt( $ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/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 ) );
    $results = curl_exec($ch );
    curl_close( $ch );

    echo $results;
}

【问题讨论】:

    标签: php wordpress google-cloud-messaging


    【解决方案1】:

    您正在发送 2 个 JSON,这就是问题所在。您可以像这样将它们组合成一个:

    {  
        "array1": {
                   "pump_name":"LEVO 92",
                   "pump_price":"2.5"
                  },
        "array2": {
                   "pump_name":"LEVO 95",
                   "pump_price":"3"
                  }
    }
    

    那么你必须把它们洒在客户端

    【讨论】:

    • 感谢您的回复!让我试一试。稍后再联系您。
    • 嗨@Ali,谢谢你的建议。我设法通过在我的代码中添加一个小东西来做到这一点。
    【解决方案2】:

    嗨,那些正在寻找解决这个问题的人。

    在参考@Ali 的建议后,我设法找到了解决方案。

    但是有一些代码需要更改才能传递二维或更多数据。请阅读下面的解决方案。

    解决方案

    将你的数组转换成对象

    if($rows) :
        foreach($rows AS $row) :
            if($counter % 2 == 0) :
    
                // Store Value Into Temporary Array
                $temp_result["pump_price"] = $row->meta_value;
    
                array_push($result, (object)$temp_result);
    
                // Unset Temporary Array
                unset($temp_result);
                $temp_result = array();
    
            else :
    
                $temp_result['pump_name'] = $row->meta_value;
    
            endif;
            $counter++;
        endforeach;
    endif;
    
    sendGoogleCloudMessage((object)$result);
    

    如果这不是一个理想的解决方案,请纠正我。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-07
      • 2012-04-28
      • 1970-01-01
      相关资源
      最近更新 更多