如何从php从android推送通知
您好,我正在尝试从我的php web服务器向android mobile发送通知……但我得到的响应如下:如何从php从android推送通知,php,android,google-cloud-messaging,Php,Android,Google Cloud Messaging,您好,我正在尝试从我的php web服务器向android mobile发送通知……但我得到的响应如下: Unauthorized Error 401 下面是我的代码 <?php $url = 'https://android.googleapis.com/gcm/send'; $device_ids = array('devise ID' ); $headers = array('Authorization: key=api key', 'Content-Type: applicati
Unauthorized Error 401
下面是我的代码
<?php $url = 'https://android.googleapis.com/gcm/send';
$device_ids = array('devise ID' );
$headers = array('Authorization: key=api key',
'Content-Type: application/json');
$t_data = array();
$t_data['message'] = array('Someone commented on your business.');
$t_json = array( 'registration_ids' => $device_ids , 'data' => $t_data );
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER,$headers );
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode( $t_json ) );
curl_setopt($ch, CURLOPT_URL, $url);
$result = curl_exec($ch);
if ($result === FALSE)
{
die('Curl failed: ' . curl_error($ch));
}
curl_close($ch);
echo $result;
?>
如何解决此错误这是我在自己的GCM项目中实现的工作脚本。试着运行这个。 确保对“api_密钥”和“注册ID”进行更改
仔细检查API密钥
。是的,我检查了它,但没有发现您定义了API密钥。。“授权:密钥=api密钥”。此api密钥在应用程序在Google上注册为GCM服务时发布。检查这个,也检查这个
<?php
$api_key = "AIzBpx4XbXkhkjwKd8P-v2d1Jk";
$registrationIDs = array("APA91bjkj3Rjlo8T_URx15NqvxY2mRyQ");
$message = "congratulations";
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => $registrationIDs,
'data' => array( "message" => $message ),
);
$headers = array(
'Authorization: key=' . $api_key,
'Content-Type: application/json');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $fields ) );
$result = curl_exec($ch);
curl_close($ch);
echo $result;
?>