0

my problem has to do with android push notification using PHP curl. The thing is that I send one push notifications to my android devices, but the problem is that a few of my devices get that notification more than once, sometimes 20 times, while some get them only once, hence ruling out fault with the app. I really don't know what the problem could be. Thanks for the anticipated response/solution. Please see push code below

function googlePush($DevID, $Message , $id)
{
// Set POST variables
$url = 'https://android.googleapis.com/gcm/send';
//  static $a=1;
    $DeviceID = array($DevID);
$Message = array("MSG" => $Message, "MSGCNT" => $Message, "message" => $Message, "id" => $id);

$fields = array(
    'registration_ids' => $DeviceID,
    'data' => $Message,
);

$headers = array(
    'Authorization: key=' . 'xxxxxxxxxxxxxxxxxxxxxxxx',
    'Content-Type: application/json'
);
// Open connection
$ch = curl_init();

// Set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    //additions
    curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
    curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);

// Disabling SSL Certificate support temporarly
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));

// Execute post
$result = curl_exec($ch);

if ($result === FALSE) {
    die('Curl failed: ' . curl_error($ch));
}

    unset($DeviceID);

// Close connection
    var_dump($result);
curl_close($ch);

}

var dump of result is shown below, I also noticed that some registration_id have the same values

    **********string** '{"multicast_id":8672106836162339205,"success":1,"failure":0,"canonical_ids":1,"results":[{"registration_id":"xxxxxxxxxxxxxxxxxx","message_id":"0:1393854839483681%eee44707f9fd7ecd"}]}' (length=326)

    **string** '{"multicast_id":8498502225166328169,"success":1,"failure":0,"canonical_ids":1,"results":[{"registration_id":"xxxxxxxxxxxxxxx","message_id":"0:1393854840250166%eee44707f9fd7ecd"}]}' (length=326)


    **string** '{"multicast_id":7302855159132255364,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1393854840859779%eee44707f9fd7ecd"}]}' (length=143)


    **string** '{"multicast_id":6882780021901998797,"success":1,"failure":0,"canonical_ids":1,"results":[{"registration_id":"xxxxxxxxxx","message_id":"0:1393854841511331%eee44707f9fd7ecd"}]}' (length=326)

    **string** '{"multicast_id":8381589117174890484,"success":1,"failure":0,"canonical_ids":1,"results":[{"registration_id":"xxxxxxxx","message_id":"0:1393854842128271%eee44707f9fd7ecd"}]}' (length=326)

    **string** '{"multicast_id":6847925128400811155,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1393854842760280%eee44707f9fd7ecd"}]}' (length=143)

    **string** '{"multicast_id":7903592410326185172,"success":1,"failure":0,"canonical_ids":1,"results":[{"registration_id":"xxxxxxxxxxx","message_id":"0:1393854843468783%eee44707f9fd7ecd"}]}' (length=326)

    **string** '{"multicast_id":5431281358827461125,"success":1,"failure":0,"canonical_ids":1,"results":[{"registration_id":"xxxxxxxx","message_id":"0:1393854844108856%eee44707f9fd7ecd"}]}' (length=326)

    **string** '{"multicast_id":6242320368959177697,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1393854844708476%eee44707f9fd7ecd"}]}' (length=143)

    **string** '{"multicast_id":6289135300645883957,"success":1,"failure":0,"canonical_ids":1,"results":[{"registration_id":"xxxxx","message_id":"0:1393854845368752%eee44707f9fd7ecd"}]}' (length=326)

    **string** '{"multicast_id":8065775041092982406,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1393854846007304%eee44707f9fd7ecd"}]}' (length=143)

    **string** '{"multicast_id":5796390289718372097,"success":1,"failure":0,"canonical_ids":1,"results":[{"registration_id":"xxxxx","message_id":"0:1393854846610566%eee44707f9fd7ecd"}]}' (length=326)

    **string** '{"multicast_id":8805706816500517785,"success":1,"failure":0,"canonical_ids":1,"results":[{"registration_id":"xxxxxxxx","message_id":"0:1393854847269971%eee44707f9fd7ecd"}]}' (length=326)

    **string** '{"multicast_id":7983336428405472483,"success":1,"failure":0,"canonical_ids":1,"results":[{"registration_id":"xxxxxxx","message_id":"0:1393854847888947%eee44707f9fd7ecd"}]}' (length=326)

    **string** '{"multicast_id":6228808030640727875,"success":1,"failure":0,"canonical_ids":1,"results":[{"registration_id":"xxxxxxxxx","message_id":"0:1393854848530967%eee44707f9fd7ecd"}]}' (length=326)

    **string** '{"multicast_id":5551712224296927538,"success":1,"failure":0,"canonical_ids":1,"results":[{"registration_id":"xxxxx","message_id":"0:1393854849250561%eee44707f9fd7ecd"}]}' (length=326)

    **string** '{"multicast_id":7677859595454701121,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1393854849828922%eee44707f9fd7ecd"}]}' (length=143)

    **string** '{"multicast_id":4797333817540136724,"success":1,"failure":0,"canonical_ids":1,"results":[{"registration_id":"xxxxxx","message_id":"0:1393854850529846%eee44707f9fd7ecd"}]}' (length=326)

    **string** '{"multicast_id":6508223386232436892,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1393854851209242%eee44707f9fd7ecd"}]}' (length=143)

    **string** '{"multicast_id":4773533719835572015,"success":1,"failure":0,"canonical_ids":1,"results":[{"registration_id":"xxxxxx","message_id":"0:1393854851828127%eee44707f9fd7ecd"}]}' (length=326)

    **string** '{"multicast_id":5409507279769055411,"success":1,"failure":0,"canonical_ids":1,"results":[{"registration_id":"xxxxxx","message_id":"0:1393854852488533%eee44707f9fd7ecd"}]}' (length=326)

    **string** '{"multicast_id":5694677433969852727,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1393854853131499%eee44707f9fd7ecd"}]}' (length=143)

    **string** '{"multicast_id":6750411477469821632,"success":1,"failure":0,"canonical_ids":1,"results":[{"registration_id":"xxxxxxx","message_id":"0:1393854853749992%eee44707f9fd7ecd"}]}' (length=326)

    **string** '{"multicast_id":4725663028894725199,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1393854854358521%eee44707f9fd7ecd"}]}' (length=143)

    **string** '{"multicast_id":6714161557242774959,"success":1,"failure":0,"canonical_ids":1,"results":[{"registration_id":"xxxxx","message_id":"0:1393854854967476%eee44707f9fd7ecd"}]}' (length=326)

    **string** '{"multicast_id":7546061721250672776,"success":1,"failure":0,"canonical_ids":1,"results":[{"registration_id":"xxxxx","message_id":"0:1393854855589308%eee44707f9fd7ecd"}]}' (length=326)

    **string** '{"multicast_id":4889453422658966187,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1393854856248256%eee44707f9fd7ecd"}]}' (length=143)

    **string** '{"multicast_id":6177254519903698994,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1393854856849541%eee44707f9fd7ecd"}]}' (length=143)

    **string** '{"multicast_id":5057079171295055438,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1393854857489730%eee44707f9fd7ecd"}]}' (length=143)

    **string** '{"multicast_id":9157325687626306713,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1393854858127816%eee44707f9fd7ecd"}]}' (length=143)

    **string** '{"multicast_id":7796618069469674709,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1393854858748602%eee44707f9fd7ecd"}]}' (length=143)

    **string** '{"multicast_id":8051599406708181530,"success":1,"failure":0,"canonical_ids":1,"results":[{"registration_id":"xxxxxxxx","message_id":"0:1393854859437798%eee44707f9fd7ecd"}]}' (length=326)

    **string** '{"multicast_id":8240272655734048325,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1393854860088449%eee44707f9fd7ecd"}]}' (length=143)

    **string** '{"multicast_id":7266159540819274423,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1393854860707600%eee44707f9fd7ecd"}]}' (length=143)

    **string** '{"multicast_id":4888209323517106512,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1393854861327433%eee44707f9fd7ecd"}]}' (length=143)

    **string** 
'{"multicast_id":8322636158677230100,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1393854861950247%eee44707f9fd7ecd"}]}' (length=143)

    **string** '{"multicast_id":5078649048024494474,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1393854862548844%eee44707f9fd7ecd"}]}' (length=143)********

2 Answers 2

5

You should handle the returned canonical registration IDs.

For example, consider the following response you got (and your question contains 19 such responses with "canonical_ids":1) :

string '{"multicast_id":7546061721250672776,"success":1,"failure":0,"canonical_ids":1,"results":[{"registration_id":"xxxxx","message_id":"0:1393854855589308%eee44707f9fd7ecd"}]}' (length=326)

It means that you used an old registration ID to send a notification to a device that already has a newer registration ID. If you also sent a notification using the new registration ID (the one specified in the registration_id parameter of the response, that would explain the device getting that notification twice.

Whenever you get a response with a canonical registration ID, you should replace in your DB the old registration ID (the one you used to send the notification) with the canonical registration ID specified in the response (or simply delete the old registration ID if you already have the canonical registration ID in your DB).

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you sir the gcm lord.
but I have one more question, look at this scenario. I push msg to reg-id 1, then it gives me a result with canonical-id = 2, I do as you said and delete/replace old reg-id 1 with 2 but that reg-id 2 is already registered in my DB, that means I am sending push notification to that app twice or more times, once for reg-id 1 (which I take care of by replacing with reg-id 2), then I send for reg-id 2.
@Ifetayo You're welcome. You should check if you already have reg-id 2 in your DB. If you do, don't replace old reg-id 1, just delete it.
Thank you sir, you have been of tremendous help. All the best.
0

As far as i can see your code doesn't send the notification more then once. You could perhaps try to log every send push notification to see if curl is really giving you the headache.

if ($result === FALSE) {
    die('Curl failed: ' . curl_error($ch));
} else {
    // Store the exact time and $deviceID in a table or file for logging purpose
}

1 Comment

Hi, I just added the var dump maybe that would help, thanks a lot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.