GCM PHP - 传递二维数组

5

我正在尝试使用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;
}
2个回答

0
你正在发送两个JSON,这就是问题所在。你可以像这样将它们合并成一个:
{  
    "array1": {
               "pump_name":"LEVO 92",
               "pump_price":"2.5"
              },
    "array2": {
               "pump_name":"LEVO 95",
               "pump_price":"3"
              }
}

然后你需要在客户端将它们分割


谢谢回复!让我试一试。稍后再联系您。 - Wee Hong
嗨@Ali,谢谢你的建议。我通过在我的代码中添加一点东西来实现它。 - Wee Hong

0

嗨,对于那些正在寻找此问题解决方案的人。

在参考@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);

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接