我正在尝试为Android实现推送通知。我想将相同的消息发送到多个电话。目前它可以发送到一部手机。我想从一个mysql数据库中获取注册密钥,并向所有这些手机发送一条消息。
问题是我对json和数组以及POST感到困惑。我可以通过另一个JSON对象发送JSON数组吗?如何在params.add(new BasicNameValuePair("regId", regkeys));
中输入多个注册码?我试图让regkeys成为一个数组,但它没有用。我知道它必须以某种形式出现
{ "message":"some message here",
"regIds": [
"dsdfsfet366767547",
"63567356366reygh",
"sgdgtwetwetsdgsdg",
"sdgsdgsdgwet24t"
]
}
但我不知道如何创建它。
Php代码
$regId = $_POST["regId"];
$message = $_POST["message"];
...
$registatoin_ids = array($regId);
$message = array("price" => $message);
$result = $gcm->send_notification($registatoin_ids, $message);
...
public function send_notification($registatoin_ids, $message) {...}
Android代码
String regkeys = regkey.getText().toString();
String message = messg.getText().toString();
...
List<NameValuePair> params = new ArrayList<NameValuePair> ();
params.add(new BasicNameValuePair("regId", regkeys));
params.add(new BasicNameValuePair("message", message));
JSONObject json = parser2.makeHttpRequest(url, params);
...
public class jParser2 {
static InputStream is = null;
static JSONObject jObject = null;
static String json = "";
public jParser2(){
}
public JSONObject makeHttpRequest(String url, List<NameValuePair> params){
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader (new InputStreamReader (is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine())!=null){
sb.append(line+"\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
jObject = new JSONObject(json);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return jObject;
}
}
我的服务器到GCM代码。
public function send_notification($registatoin_ids, $message) {
// include config
include_once './config.php';
// Set POST variables
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => $registatoin_ids,
'data' => $message,
);
$headers = array(
'Authorization: key=' . GOOGLE_API_KEY,
'Content-Type: application/json'
);
// Open connection
$ch = curl_init();
// Set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Disabling SSL Certificate support temporarly
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
// Execute post
$result = curl_exec($ch);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}
// Close connection
curl_close($ch);
echo $result;
}
答案 0 :(得分:1)
我不知道您的$gcm->send_notification
函数是什么样的,但它应该是这样的:
$data = array( 'message' => 'some message' );
$ids = array( 'reg-id1', 'reg-id2' );
$payload = array(
'registration_ids' => $ids,
'data' => $data,
);
$headers = array(
'Authorization: key=' . $apiKey,
'Content-Type: application/json'
);
请注意,为了将相同的消息发送到多个注册ID,请求的内容类型必须是JSON。注册ID在包含数组的registration_ids
参数中发送,通知数据在包含字典的data
参数内。
答案 1 :(得分:1)
是的,您可以将带有regIds的数组放入JSON对象中并将JSON Objet放入Post:
List<NameValuePair> params = new ArrayList<NameValuePair> ();
params.add(new BasicNameValuePair("json", JsonObject));
httpPost.setEntity(new UrlEncodedFormEntity(params));
希望这会对你有所帮助。
答案 2 :(得分:0)
我建议使用GSON。
现在,使用,创建一个类:
Class Messages {
public String message;
public ArrayList<String> regIDs;
public void setMessage(String message) {
this.message = message;
}
public ArrayList<String> getRegIDs() {
return regIDs;
}
}
现在,当您想要向服务器发送一些数据时,请创建此类的新对象。
Messages messages = new Messages();
messages.setMessage("Some message here");
//Add all the regn IDs to the regIDs List.
ArrayList<String> regIDs = messages.getRegIDs()
regIDs.add("regnID1");
regIDs.add("regnID2");
regIDs.add("regnID3");
regIDs.add("regnID4");
//Add as many IDs as you want.
//then create a new GSON Object.
Gson gson = new Gson();
String jsonString = gson.toJSON(messages);
现在jsonString
将自动包含您指定格式的JSON字符串。因此,请使用jsonString
变量的内容向您的服务器发送POST请求。然后在Php端,使用json_decode解码成一个数组并遍历regIDs数组以发送多个gcm消息。