firebase - How to know which registration_id is ba

2019-07-18 12:49发布

问题:

So I am pushining notifications in a batch to multiple devices. I want to delete device GFCM tokens from my databse if they are not in firebase.

Test request:

{"registration_ids": ["fznIum-OIL0:APA91bE9n6gxqVEB3hR0On_TZqH_cOOO7rFyLE5MYbvKXUX3737IonXquZRoHeY1UJf20G4nrahz35p80X8PAPouviYvXgtekAzeDFuwaWyiPI1sFlN1d1wGp5OulXv9wUzHyegeZiih","dsasadadsasdasdasdasdefs","fjb0qTB9ACI:APA91bHvx0pSlq-NuQz-bU70DhL7BXAcd-DqT9YT0ZjYw7R_pkBmr8oe6XdNw8IPlU67d-rXMfRY3mybaO7K5GHo_NH7G-Ya1Ae2xxJP1JIbuuSYomz31y_MPQijFK8w-bZXQ26SWE09"],
"notification":{
    "body":"Yoou have a new message from Alice"}}

And response:

{
  "multicast_id": 7971310728176484096,
  "success": 1,
  "failure": 2,
  "canonical_ids": 0,
  "results": [
    {
      "error": "NotRegistered"
    },
    {
      "error": "InvalidRegistration"
    },
    {
      "message_id": "0:1481547993513035%4f9c20964f9c2096"
    }
  ]
}

How to detect which registration_ids are bad/Not registered?

Thanks!

回答1:

Okey, i think i figured it out.

"results": Returns responses/errors in the order you specified it in registration_ids



回答2:

when you are sending push notification to any mobile,you can find in err that registartion_ids/token isbad /not registered/
        var serverKey = 'sdasd';//server key
        var fcm = new FCM(serverKey);
        var token = "token";//token here
        var message = {
            to: token,
            collapse_key: 'your_collapse_key',
            notification: {title: 'hello', body: 'test'},
            data: {my_key: 'my value', contents: "stackoverflow.com"}
        };
        fcm.send(message, function (err, response) {
            if (err) {
               console.log(err);//find here registration_ids are bad/Not registered? 
            } else {
                console.log(response);
            }
        });
    });