I am trying to use Google Cloud Messaging (GCM) to push a two-dimensional array to device. However, I found that GCM only able to send a single array at one time.
我正在尝试使用Google Cloud Messaging(GCM)将二维数组推送到设备。但是,我发现GCM一次只能发送一个数组。
I am looking for a solution to overcome this issue. Because I think it is not practical to keep pushing information one by one.
我正在寻找解决这个问题的解决方案。因为我认为一个接一个地推送信息是不切实际的。
Below are two different scenarios
以下是两种不同的场景
Single Dimension Array(Which is successfully to push to device)
单维数组(成功推送到设备)
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"}]}
Two Dimension Array(Which is fail to push to device)
二维数组(无法推送到设备)
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"}]
Codes (I split it into two parts)
代码(我把它分成两部分)
Pump Price Function
泵价格函数
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
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 个解决方案
#1
0
You are sending 2 JSONs and thats the problem. You can combine them into one like this:
您正在发送2个JSON,这就是问题所在。你可以把它们合并成一个这样的:
{
"array1": {
"pump_name":"LEVO 92",
"pump_price":"2.5"
},
"array2": {
"pump_name":"LEVO 95",
"pump_price":"3"
}
}
Then you have to spilt them client side
然后你必须把它们洒到客户端
#2
0
Hi for those people who looking solution for this issue.
嗨,为那些寻找解决此问题的人。
I managed to find a solution after referring to @Ali suggestion.
在提到@Ali建议后,我设法找到了解决方案。
But there are some codes need to change before you can pass two-dimensional data or more. Please read the solution below.
但是,在传递二维数据或更多数据之前,需要更改一些代码。请阅读以下解决方案。
Solution
Convert your array into Object
将数组转换为Object
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);
Please correct me if this is not an ideal solution.
如果这不是一个理想的解决方案,请纠正我。
#1
0
You are sending 2 JSONs and thats the problem. You can combine them into one like this:
您正在发送2个JSON,这就是问题所在。你可以把它们合并成一个这样的:
{
"array1": {
"pump_name":"LEVO 92",
"pump_price":"2.5"
},
"array2": {
"pump_name":"LEVO 95",
"pump_price":"3"
}
}
Then you have to spilt them client side
然后你必须把它们洒到客户端
#2
0
Hi for those people who looking solution for this issue.
嗨,为那些寻找解决此问题的人。
I managed to find a solution after referring to @Ali suggestion.
在提到@Ali建议后,我设法找到了解决方案。
But there are some codes need to change before you can pass two-dimensional data or more. Please read the solution below.
但是,在传递二维数据或更多数据之前,需要更改一些代码。请阅读以下解决方案。
Solution
Convert your array into Object
将数组转换为Object
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);
Please correct me if this is not an ideal solution.
如果这不是一个理想的解决方案,请纠正我。