We have about 900 Android device tokens in our database for GCM push notifications.
I use this PHP function to send the push instruction to the GCM server:
function send_android_push($android_gcm_reg_ids, $title, $message, $activityToLaunch) {
global $ANDROID_GCM_API_KEY, $TESTING, $TEST_ANDROID_DEVICE_REG_ID;
if ($TESTING) {
$android_gcm_reg_ids = [
$TEST_ANDROID_DEVICE_REG_ID
];
}
$url = "https://android.googleapis.com/gcm/send";
$data = array(
"title" => $title,
"message" => $message,
"activityToLaunch" => $activityToLaunch,
);
$post = array(
'registration_ids' => $android_gcm_reg_ids,
'data' => $data
);
$headers = array(
'Authorization: key=' . $ANDROID_GCM_API_KEY,
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post));
$result = curl_exec($ch);
if (curl_errno($ch)) {
$result .= "<br />GCM error: " . curl_error($ch);
}
curl_close($ch);
return $result;
}
Here is a snippet of the JSON content returned by that function/GCM:
{
"multicast_id":5609284883833846123,
"success":493,
"failure":401,
"canonical_ids":20,
"results":[
{
"registration_id":"BPA91bHP29l2j6NPOldcpAvJCJBGk8oQFErfwuh93cxF8ajJJqBlVGqFcxMDEAc2LL2GKwmP86of49UgBTMycB5IdYergWRBETDNUrnzXX_55FgstCZiauPeD7MokIIPOFoOyW9vCRiBqZhlzLCuSnJ1ENFYtIh_PQ",
"message_id":"0:1462275569015808%814cce86f9fd7ece"
},
{
"registration_id":"BPA91bHP29l2j6NPOldcpAvJCJBGk8oQFErfwuh93cxF8ajJJqBlVGqFcxMDEAc2LL2GKwmP86of49UgBTMycB5IdYergWRBETDNUrnzXX_55FgstCZiauPeD7MokIIPOFoOyW9vCRiBqZhlzLCuSnJ1ENFYtIh_PQ",
"message_id":"0:1462275569015815%814cce86f9fd7ece"
},
{
"message_id":"0:1462275569014983%814cce86f9fd7ece"
},
{
"error":"NotRegistered"
},
{
"message_id":"0:1462275569017976%814cce86f9fd7ece"
},
etc., etc.
It seems to be saying I have device tokens in my database that are NotRegistered
but, as you can see, it doesn't tell me which device token isn't registered so I am wondering how I can mark it as obsolete in my database?
I thought that maybe the elements of the JSON array returned by the GCM server might correspond to the $android_gcm_reg_ids
array I pass into my send_android_push(...)
function but that can't be the case because the BPA91bHP29l2j6NPOldcpAvJCJBGk8oQFErfwuh93cxF8ajJJqBlVGqFcxMDEAc2LL2GKwmP86of49UgBTMycB5IdYergWRBETDNUrnzXX_55FgstCZiauPeD7MokIIPOFoOyW9vCRiBqZhlzLCuSnJ1ENFYtIh_PQ registration_id
appears numerous times in the GCM JSON array - despite that same token only appearing once in my database.
So presumably there must be some other way of removing obsolete device tokens from my database??
And the reason I am concerned about this is because I have had the situation before (with iOS) where sending bad device tokens (to APNS) resulted in the push notification not being sent to valid devices, and I would obviously like to keep my database in a good state anyway.
I can't find anything to help on the web, so maybe I am missing or getting wrong something obvious?
To know which registration token is causing this, you have to map to the same index in your
registration_ids
array of your request to GCM.Here from the docs: