curl发送消息

时间:2016-09-10 06:56:39

标签: php curl google-cloud-messaging web-push

我想了解什么是网络推送以及我如何使用我的项目...... 找到了这个例子https://mobiforge.com/design-development/web-push-notifications 但尝试通过Firebase Cloud Messaging发送通知时总是出错(FCM是GCM的新版本)

{"multicast_id":6440031216763605980,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"InvalidRegistration"}]}

“InvalidRegistration”是什么意思?我做错了什么?

我的php curl,但我相信这里没有问题

$link = "https://gcm-http.googleapis.com/gcm/send";

$header = array();
// $header[] = "Content-length: 0";
$header[] = "Content-type: application/json";
$header[] = "Authorization: key=AIzaSy...";

$contentArray = array(
    "collapse_key" => "All",
    "registration_ids" => array(
        "gAAAAABX06BLKhA4n1yHNlsyzu02wxsDjZf89oxIljwM4ZdLpMZU7ty64TFEYahPQZaTmCeYlJo-WDWnfFHOKXzKURhNtRWmN0OgBgn9hJdmgatSGoiTkt69TeJpiD8F034WOr5HMEG2",
    ),
    "data" => array(
        "title" => "This is a Title",
        "message" => "This is a GCM Topic Message!"
    )
);
$jsonData = json_encode($contentArray);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $link);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
$string = curl_exec($ch);
echo $string;

$data['curl'] = curl_errno($ch);

if(!curl_errno($ch) && !strpos($string, "503"))
    $data = array_merge($data, explode("\n", $string));

curl_close($ch);

?><pre><? print_r($data); ?></pre><?

一些来自Cosole.log

ServiceWorker registration successful with scope: https://.../app/
PushSubscription { endpoint="https://updates.push.ser...rjYvTTapou7WcEDgu3V7IOY",  options=PushSubscriptionOptions,  getKey=getKey(),  ...}
PushSubscription { endpoint="https://updates.push.ser...rjYvTTapou7WcEDgu3V7IOY",  options=PushSubscriptionOptions,  getKey=getKey(),  ...}
gAAAAABX06OYvBIk4q2rRF3AsE6UwRYUpzpZ0jpuiWz6TRrSptb8_cBKjy8Ci-_u5UtAyiGfAYJ_ycYnJjoukSuez7BN6UnSX-GL_EWNAWzEpAVMhCT2wrjYvTTapou7WcEDgu3V7IOY

4 个答案:

答案 0 :(得分:0)

请尝试检查您使用的订阅ID。

Check the response中所述,

  

如果响应显示无效的注册错误,请检查您使用的订阅ID。

正如向GCM发出请求时所述,请确保在运行cURL命令时使用自己的API密钥和订阅ID。

有关详细信息,请查看有关如何send a request from the command line for GCM to push a message

的文档

答案 1 :(得分:0)

它不起作用。我一直在尝试很多次。下面是我在POSTMan中尝试的方式

enter image description here

答案 2 :(得分:0)

根据我的经验,您使用的registration_id似乎来自Firefox浏览器上的订阅。但是,您正在尝试将其发送到Chrome推送服务器。

Chrome registration_id应如下所示:

  

APA91bGdUldXgd4Eu9MD0qNmGd0K6fu0UvhhNGL9FipYzisrRWbc-qsXpKbxocgSXm7lQuaEOwsJcEWWadNYTyqN8OTMrvNA94shns_BfgFH14wmYw67KZGHsAg74sm1_H7MF2qoyRCwr6AsbTf5n7Cgp7ZqsBZwl8IXGovAuknubr5gaJWBnDc

这是一项非常新的技术,早期版本的代码仍然可以在Google开发者平台上使用,因此要理解该做什么并不容易。我还在尝试它。

选中此codelab,这是了解基础知识的一个很好的例子。

答案 3 :(得分:0)

我也想在这篇文章中分享我的答案。

检查https://stackoverflow.com/a/40447040/4677062是否存在相同的无效注册ID问题。

它解决了。按预期工作。

相关问题