注册Google Cloud Messaging https://code.google.com/apis/console/b/0/?noredirect&pli=1#project:243064666795:access
使用我的本地系统static final String SERVER_URL = "http://142.168.1.213/gcm_server_php/register.php";
更改发件人ID,如static final String SENDER_ID = "243064666795";
在config.php中更改Google密钥,用户名,密码和dbname
然后在模拟器上启动项目后,它在服务器上的设备上成功注册,它也在浏览器上显示一个设备,但是当我从浏览器发送通知时,它将不会在设备上接收。什么错了?
答案 0 :(得分:0)
define('PUSHURL', 'https://go.urbanairship.com/api/push/');
define('GCMAPIKEY','xxxxxxxxxxxxxxxxxxxxxxxxxxx');
define('GCMPUSHURL', 'https://android.googleapis.com/gcm/send');
$ori_msg=substr($message,0,200);
$querydevices="selectdevice_token from device_info where device_type='A'";
$devicesres = mysql_query($querydevices) or die(mysql_error());
$devicecount = mysql_num_rows($devicesres);
if($devicecount > 0)
{
while($fetchdevices = mysql_fetch_array($devicesres))
{
if($fetchdevices['device_token']!="0")
{
if($fetchdevices['device_token']!="")
{
$registrationIDs[]=$fetchdevices['device_token'];
}
}
}
$fields = array(
'registration_ids' => $registrationIDs, //(android device ids[array()])
'data' => array( "message" => $ori_msg )
);
$payload = json_encode($fields);
// echo $payload;
$headers = array(
'Authorization: key='.GCMAPIKEY,
'Content-Type: application/json'
);
$ch = curl_init();
// Set the url, number of POST vars, POST data
curl_setopt( $ch, CURLOPT_URL, GCMPUSHURL);
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $payload );
// Execute post
$result = curl_exec($ch);
curl_close($ch);
}
编辑
这是表结构
CREATE TABLE `device_info` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`device_token` varchar(500) NOT NULL,
`createdon` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;