我遵循了本教程,我设法使用设备在cgm服务器中注册了该应用程序但我无法接收从服务器到设备的推送通知我在 genymotion 模拟器和真实设备上进行了测试,但设备上始终没有收到通知我不知道错误,也不知道如何解决这个问题
以下是发送推送通知的方法:
/**
* Sending Push Notification
*/
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;
}
我已经设置了 api 密钥和项目 ID,一切都完成了。
试试这个:
$apiKey = "your api key goes here (use browser api)";
$headers = array("Content-Type:" . "application/json", "Authorization:" . "key=" . $apiKey);