-->

Web push notification error with curl send message

2019-07-08 11:08发布

问题:

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

回答1:

Please try checking the subscription ID that you used.

As mentioned in Check the response,

If the response shows an invalid registration error, check the subscription ID you used.

As discussed further in making a request to GCM, make sure to use your own API key and subscription ID when you run the cURL command.

For more information, please check the documentation on how to send a request from the command line for GCM to push a message.



回答2:

It is not working. I have been trying a lot times. here below is how I tried in POSTMan



回答3:

From my experience, the registration_id you are using seems to be from a subscription on a Firefox browser. But yet you're trying to send it to the Chrome push server.

A Chrome registration_id should look like that:

APA91bGdUldXgd4Eu9MD0qNmGd0K6fu0UvhhNGL9FipYzisrRWbc-qsXpKbxocgSXm7lQuaEOwsJcEWWadNYTyqN8OTMrvNA94shns_BfgFH14wmYw67KZGHsAg74sm1_H7MF2qoyRCwr6AsbTf5n7Cgp7ZqsBZwl8IXGovAuknubr5gaJWBnDc

It's a pretty new technology and earlier versions codes are still available on the google developer platform, so it's not really easy to understand what to do. I'm still experimenting with it.

Check this codelab it's a good example to understand the basics.



回答4:

I would like to share my answer in this post too.

Check https://stackoverflow.com/a/40447040/4677062 for the same invalid registration id issue.

Its resolved. Works as expected.