gpt4 book ai didi

php - GCM PHP - 传递二维数组

转载 作者:可可西里 更新时间:2023-10-31 23:46:41 25 4
gpt4 key购买 nike

我正在尝试使用 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 个 JSON,这就是问题所在。您可以像这样将它们组合成一个:

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

然后你必须将它们泄露给客户端

关于php - GCM PHP - 传递二维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33272950/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com