I am using GCM services to push information from server. If I use browser key it shows the sucess mesaage as : {"multicast_id":4849013215736515938,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1348742583011905%2adac3a0f9fd7ecd"}]} but i did not get any notifications on device. And if I use server key it shows Unauthorized Error 401. My code is shown below:

$apiKey = "xxxxx";
$registrationIDs = array("xxxxxxxx");
$message = "testing Process";
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
        'registration_ids'  => $registrationIDs,
        'data'              => array("message"=>$message),
        );
$headers = array( 
        'Authorization: key=' . $apiKey,
        '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_SSL_VERIFYHOST, 0);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0); 
curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode($fields) );

$result = curl_exec($ch);
if(curl_errno($ch)){ echo 'Curl error: ' . curl_error($ch); }
curl_close($ch);
echo $result;

Please help me for this issue. Thanks in advance.

推荐答案

您是否将服务器的IP列入白名单?默认情况下,这对于浏览器密钥不是必需的,但对于服务器密钥是必需的.

你可以在这里查看:

https://code.google.com/apis/console/#project:[YOUR PROJECT NUMBER]:access

Json相关问答推荐

PowerShell:将Invoke-WebRequest与变量一起使用

为什么JQ筛选器不将原始输入打印为$var|.';文档?

通过在织女星简化图上裁剪来显示文本

bash用jq获取第二条JSON记录

在 SQL 存储过程中使用参数 Select 值

Jolt 转换数组对象并将某些字段移动到嵌套数组

Jolt 不打印任何东西

颠簸转换问题

阅读 JSON 正文 Firebase 云函数

使用 Jolt 变换将平面 json 转换为具有多个数组的嵌套 Json

如果 JSON 对象包含列表中的子字符串,则丢弃它们

Microsoft GRAPH 查询使用端点 /deviceManagement/deviceHealthScripts 列出了一种不熟悉的检测脚本内容格式

如何将任意 json 对象发布到 webapi

在 Bash 中访问 JSON 对象 - 关联数组/列表/另一个模型

使用 JSON.Net 的 C# 到 JSON 序列化

如何在 Python 中合并两个 json 字符串?

在 JSON.NET 中序列化派生类时的字段顺序

js 中奇怪的 JSON 解析行为,Unexpected token :

jq:按属性分组和键

如何在 postgresql 9.3 中循环 JSON 数组