【发布时间】:2014-01-01 11:49:34
【问题描述】:
我已经使用 Google 的 OAuth 2.0(来自开发者控制台)创建了一个 Android 应用程序,并有一个 Android 应用程序的 API 密钥(它使用 Google 地图,顺便说一下)。
但是,我的问题是我正在使用推送通知,它可以在 android 应用程序上运行(我得到了注册 ID),但在服务器上它根本不起作用。
我正在使用 PHP,问题似乎是 API 密钥与我为 Android 应用程序使用的密钥相同。
那么我应该使用什么键?以及如何找回它?
代码:
<?php
function send_push_notification($registatoin_ids, $message) {
// Set POST variables
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => $registatoin_ids,
'data' => $message,
);
$headers = array(
'Authorization: key=An_Api_Key',
'Content-Type: application/json'
);
//print_r($headers);
// 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;
}?>
【问题讨论】:
-
上面的代码是从那个代码中提取出来的,这是作为概念证明的,所以我不需要数据库代码。所以代码没有错,只有关键。
标签: php android push-notification