Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which fcm registration id has failed when targeted for multiple registration_ids

I'm sending notifications to several registration ids from server side in php. This is the request:

public function androidPushNotification($registration_ids, $title, $message) {
    $msg = array (
            'message' => $message,
            'title' => $title 
    );

    $fields = array (
            'registration_ids' => $registration_ids,
            'data' => $msg 
    );
    
    $headers = array (
            'Authorization: key=' . $this->API_ACCESS_KEY,
            'Content-Type: application/json' 
    );
    
    $ch = curl_init ();
    curl_setopt ( $ch, CURLOPT_URL, $this->GCM_URL );
    curl_setopt ( $ch, CURLOPT_POST, true );
    curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers );
    curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
    curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, false );
    curl_setopt ( $ch, CURLOPT_POSTFIELDS, json_encode ( $fields ) );
    $result = curl_exec ( $ch );
    curl_close ( $ch );
    
    return $result;
}

The registration_ids variable has two registration ids in an array, one of them is from an old installation of the client app and the other is the current one.

I'm getting this response from fcm:

{
  "multicast_id": 7860323906688398625,
  "success": 1,
  "failure": 1,
  "canonical_ids": 0,
  "results": [
    {
      "error": "NotRegistered"
    },
    {
      "message_id": "0:1478735313889582%1b153de0f9fd7ecd"
    }
  ]
}

How can I know which of the registration ids has failed?

Is there another option to get this info?

like image 785
Javier Vargas Avatar asked Nov 10 '16 00:11

Javier Vargas


People also ask

What is registration ID in FCM?

The registration_ids parameter refers to the Registration Tokens you want to add in that specific Device Group. Described as: An ID generated by the FCM SDK for each client app instance. Required for single device and device group messaging. Note that registration tokens must be kept secret.

Where can I find FCM registration token?

Retrieve and store registration tokens As noted in our client setup guides, your app should retrieve this token at initial startup and save it to your app server alongside a timestamp. This timestamp must be implemented by your code and your servers, as it is not provided for you by FCM SDKs.

Is FCM token device specific?

It's a token for app (uniq from firebase configs) and device. For example if you create 2 app with two different firebase configs but you request the token from the same device, the service will give you two different tokens.

How can I get FCM registration token in Android?

On initial startup of your app, the FCM SDK generates a registration token for the client app instance. If you want to target single devices or create device groups, you'll need to access this token by extending FirebaseMessagingService and overriding onNewToken .


1 Answers

I have found the answer:

The result array is in the same order that the registration ids. For example, if the request is:

$fields = array (
            'registration_ids' => array('123456','987654'),
            'data' => array ('message' => 'This is the message','title' => 'Hi there!')
);

The example response:

{
  "multicast_id": 7860323906688398625,
  "success": 1,
  "failure": 1,
  "canonical_ids": 0,
  "results": [
    {
      "error": "NotRegistered"
    },
    {
      "message_id": "0:1478735313889582%1b153de0f9fd7ecd"
    }
  ]
}

The registration id has failed is the 123456 one.

From the gcm documentation:

Here are JSON results for 6 recipients (IDs 4, 8, 15, 16, 23, and 42 respectively) with 3 messages successfully processed, 1 canonical registration token returned, and 3 errors:

{ 
  "multicast_id": 216,
  "success": 3,
  "failure": 3,
  "canonical_ids": 1,
  "results": [
    { "message_id": "1:0408" },
    { "error": "Unavailable" },
    { "error": "InvalidRegistration" },
    { "message_id": "1:1516" },
    { "message_id": "1:2342", "registration_id": "32" },
    { "error": "NotRegistered"}
  ]
}

In this example:

  • First message: success, not required.
  • Second message: should be resent (to registration token 8).
  • Third message: had an unrecoverable error (maybe the value got corrupted in the database).
  • Fourth message: success, nothing required.
  • Fifth message: success, but the registration token should be updated in the server database (from 23 to 32).
  • Sixth message: registration token (42) should be removed from the server database because the application was uninstalled from the
    device.

I hope it helps, regards.

like image 180
Javier Vargas Avatar answered Oct 05 '22 14:10

Javier Vargas