这个问题来自服务器端。通知消息正文应该是这样的。
{
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"content_available" : "true"
"notification" : {
"body" : "great match!",
"title" : "Portugal vs. Denmark"
}
}
这是我已经实现并且运行良好的代码。
<?php
$notificationTitle = "Notification title";
$notificationtBody = "Notification arrived";
$registrationId = 'GCM_DEVICE_TOKEN';
$apiKey = "GCM_SERVER_KEY";
$response = sendNotification(
$apiKey,
$registrationId,
array(
'sound' => "default",
'badge' => 1,
'body' => $notificationtBody,
'title' => $notificationTitle
)
);
echo $response;
function sendNotification( $apiKey, $registrationId, $messageData )
{
$headers = array("Content-Type:" . "application/json", "Authorization:" . "key=" . $apiKey);
$data = array(
'to' => $registrationId,
'content_available' => true,
'notification' => $messageData
);
$ch = curl_init();
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch, CURLOPT_URL, "https://android.googleapis.com/gcm/send" );
curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0 );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0 );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode($data) );
$response = curl_exec($ch);
curl_close($ch);
return $response;
}