0

I created some PHP code that gets a GCM register from the database and sends a push to an Android device. This code works ok, but when the user deletes the app on Android for example the GCM register in the database gets invalid. How do I know when the GCM register is invalid in order to delete it from the database using PHP?

This is the code that sends push:

//Getting api key 
$api_key = "....";

//Getting registration token we have to make it as array 
$reg_token = array($token);

//Getting the message 
$message = $versiculo;

//Creating a message array 
$msg = array
(
    'message' => $message,
    'title' => $capitulo,
    'id_versiculo' => $id,
);

//Creating a new array fileds and adding the msg array and registration token array here 
$fields = array
(
        'registration_ids'  => $reg_token,
        'data'      => $msg
);

//Adding the api key in one more array header 
$headers = array
(
        'Authorization: key=' . $api_key,
        'Content-Type: application/json'
); 

//Using curl to perform http request 
$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/send' );
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 ) );

//Getting the result 
$result = curl_exec($ch );
curl_close( $ch );
Daniel Serodio
  • 4,229
  • 5
  • 37
  • 33
  • 2
    You should migrate your app to Firebase (FCM). GCM is depreciated. "As of April 10, 2018, Google has deprecated GCM. The GCM server and client APIs are deprecated and will be removed as soon as April 11, 2019." – Barns Apr 11 '18 at 13:23
  • @Barns nailed it. Dont use GCM. FCM.... or another provider... I use OneSignal. – letsCode Apr 11 '18 at 13:43
  • Hello guys. Yes de GCM is depreciated and the team is work to change, but in this moment the app is work and we need to delete invalid GCM register of ower database. Is it possible ? There is a way? – Alexandre C. do Carmo Apr 11 '18 at 14:11
  • For my app I can send messages to individual devices. In the message I have a custom tag -- something like "type_of_message" for which I send "update_request" (for example). If the message type does not correspond to one of the "types" it is supposed to handle, then the message is ignored. If you had something like that in your app, you could send the devices a message and wait to see if the returned response result contained an error like "Unregistered Device" or "Invalid Registration Token" . Then you would know that it is not valid – Barns Apr 11 '18 at 14:56
  • Barns, tsk, very google suggestion. I will do it – Alexandre C. do Carmo Apr 13 '18 at 13:50

1 Answers1

0

When the client app is uninstalled, the corresponding token will be invalidated, where if you send a message to that specific token, it would return a NotRegistered error, in which case you can delete (or archive) that token.

As mentioned in the comments, please proceed to migrate your app to FCM, as Google already provided a notice of deprecation (see my answer here).

For FCM, the behavior is actually the same. See this similar/possibly duplicate post.

AL.
  • 36,815
  • 10
  • 142
  • 281