Web推送通知错误与curl发送消息 [英] Web push notification error with curl send message
问题描述
我想了解什么是web推送,我如何使用我的项目...
已找到此示例 https://mobiforge.com/design-development/web-push-notifications
但是总是在尝试通过Firebase云消息传递发送通知时收到错误FCM是新版本的GCM)
{multicast_id:6440031216763605980,success:0,failure:1 ,canonical_ids:0,results:[{error:InvalidRegistration}]}
什么意思无效注册?我做错了什么?
我的php卷曲,但我确定这里没有问题
$ link =https://gcm-http.googleapis.com/gcm/send;
$ header = array();
// $ header [] =Content-length:0;
$ header [] =Content-type:application / json;
$ header [] =授权:key = AIzaSy ...;
$ contentArray =阵列(
collapse_key的=>中所有,
registration_ids=>阵列(
gAAAAABX06BLKhA4n1yHNlsyzu02wxsDjZf89oxIljwM4ZdLpMZU7ty64TFEYahPQZaTmCeYlJo-WDWnfFHOKXzKURhNtRWmN0OgBgn9hJdmgatSGoiTkt69TeJpiD8F034WOr5HMEG2,
),
data=> array(
title=>这是一个标题,
message=>这是一个GCM主题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(\\\
,$ string));
curl_close($ ch);
?>< pre>< ;? print_r($ data); ?>< / pre>< ;?
一些来自Cosole.log
ServiceWorker注册成功,范围为: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
我会喜欢在此信息中分享我的回答。
检查 http:// stackoverflow.com/a/40447040/4677062 针对相同的无效注册ID问题。
已解决。按预期工作。
I want to understand what is web-push and how can i use for my projects... Have found this example https://mobiforge.com/design-development/web-push-notifications But always getting an error when try to send notification via Firebase Cloud Messaging (FCM is the new version of GCM)
{"multicast_id":6440031216763605980,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"InvalidRegistration"}]}
What it means "InvalidRegistration"? What i'm doing wrong?
My php curl, but i am sure that there is no problem here
$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><?
some from 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
I would like to share my answer in this post too.
Check http://stackoverflow.com/a/40447040/4677062 for the same invalid registration id issue.
Its resolved. Works as expected.
这篇关于Web推送通知错误与curl发送消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!