PHP を使用して Android で Google Cloud Messaging をセットアップしようとしています (このチュートリアルに従ってください: http://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-および-mysql/ )。私のデバイスは GCM に正常に登録され、次にサーバーに登録されます (regId はデータベースに保存されます)。
デバイスに送信しようとすると、何も受信されませんが、次の応答が返されます。
{"multicast_id":4701392840778619841,"成功":1,"失敗":0,"canonical_ids":0,"結果":[{"message_id":"0:1355907831651938%978fee92f9fd7ecd"}]}
Google API は、リクエストが行われていないことを報告しています。
これは、PHP の send_notification 関数です。
public function send_notification($registration_ids, $message) {
// include config
include_once './config.php';
// Set POST variables
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => $registration_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;
}
調査の結果、同様の質問が寄せられているのを見てきましたが、それらはわずかに異なります。たとえば、プログラミング言語が異なり、この問題を解決する解決策が見つかりませんでした。
追加情報:
1. cURL が有効になっていることを確認しました。
2.私の onMessage メソッド
protected void onMessage(Context context, Intent intent) {
Log.i(TAG, "Received message");
String message = intent.getExtras().getString("price");
displayMessage(context, message);
// notifies user
generateNotification(context, message);
}
(上記の price は、サーバー上の send_notification 関数にパラメーターとして渡される前に、次のように保存されているメッセージを取得するだけです: $message = array("price" => $message);
LogCat を確認しましたが、「受信メッセージ」が表示されません。