我正在嘗試使用Google Clouding Messaging API與Android應用程序進行集成。對於後端,我使用的是Laravel 5.2。我在Google API中生成了3個API關鍵字。這些是服務器API密鑰,Android API密鑰和瀏覽器API密鑰。我參考tutorial。發送請求時出錯PHP中的Google Cloud Messaging
這是從服務器推我的請求,GCM服務器:
private function sendNotification($registatoin_ids,$message)
{
$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;
}
function register(Request $request)
{
$name = $request->name;
$email = $request->email;
$gcm_regid = $request->reg_ids;
$registatoin_ids = array($gcm_regid);
$message = array("product" => "shirt");
$result = $this->sendNotification($registatoin_ids,$message);
echo $result;
}
我叫寄存器功能。 email
,name
和reg_ids
是模擬值。我分別通過了myemail
,myname
和隨機串。寄存器功能是控制器的動作。對於GOOGLE_API_KEY
,我通過了服務器API密鑰。但是,當我要求時,它會給我下面的錯誤。
{"multicast_id":5065519232839143946,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"InvalidRegistration"}]}
是我的推送請求是否正確?我可以通過任何獨特的價值reg_ids
。而且,我的GOOGLE_API_KEY
應該是Android,服務器還是瀏覽器的關鍵?