GCM how to unregister a device with GCM and 3rd pa

2019-02-02 02:07发布

问题:

I've an app that uses GCM push notifications. It works fine and my device registers and receives push messages.

If i uninstall the app from my device i no longer receive the messages as you would expect. The TextBox in which you send messages on the server is still there after i un-install the app, which i'd also expect.

I've looked at the documentation regarding unregistering and you can do it manually or automatically.

The end user uninstalls the application.
The 3rd-party server sends a message to GCM server.
The GCM server sends the message to the device.
The GCM client receives the message and queries Package Manager about whether there are broadcast receivers configured to receive it, which returns false.
The GCM client informs the GCM server that the application was uninstalled.
The GCM server marks the registration ID for deletion.
The 3rd-party server sends a message to GCM.
The GCM returns a NotRegistered error message to the 3rd-party server.
The 3rd-party deletes the registration ID.

I don't understand the next to last statement in the above list.

The GCM returns a NotRegistered error message to the 3rd-party server.

How is this acheived?

Also if the app is uninstalled from the device, how can it do the statement below? Is there an app lifecycle method that execute as an app is removed from a device? If there is, is this the place where code is placed that informs GCM server of the uninstall and the calls a php script on the 3rd party server that removes the regID from the DB?

The GCM client informs the GCM server that the application was uninstalled.

thanks in advance,

Matt

[edit1]

static void unregister(final Context context, final String regId) {
        Log.i(TAG, "unregistering device (regId = " + regId + ")");
        String serverUrl = SERVER_URL + "/unregister.php";
        Map<String, String> params = new HashMap<String, String>();
        params.put("regId", regId);
        try {
            post(serverUrl, params);
            GCMRegistrar.setRegisteredOnServer(context, false);
            String message = context.getString(R.string.server_unregistered);
            CommonUtilities.displayMessage(context, message);
        } catch (IOException e) {
            // At this point the device is unregistered from GCM, but still
            // registered in the server.
            // We could try to unregister again, but it is not necessary:
            // if the server tries to send a message to the device, it will get
            // a "NotRegistered" error message and should unregister the device.
            String message = context.getString(R.string.server_unregister_error,
                    e.getMessage());
            CommonUtilities.displayMessage(context, message);
        }
    }

[EDIT2] The unregister code below is for unregistering the device on the 3rd party server after deleting the app from the phone. The code is in addition to the tutorial below.

tutorial

send_messages.php

<?php
if (isset($_GET["regId"]) && isset($_GET["message"])) {
    $regId = $_GET["regId"];
    $message = $_GET["message"];
    $strRegID = strval($regId);

    include_once './GCM.php';
    include_once './db_functions.php';
    $gcm = new GCM();

    $registatoin_ids = array($regId);
    $message = array("price" => $message);

    $result = $gcm->send_notification($registatoin_ids, $message);
    $db = new db_Functions();

    if (strcasecmp ( strval($result) , 'NotRegistered' )) {
    $db->deleteUser($strRegID);
    }
}
?>

db_functions.php

public function deleteUser($regid) {

    $strRegID = strval($regid);

    $serverName = "LOCALHOST\SQLEXPRESS"; 
        $uid = "gcm";     
        $pwd = "gcm";    
        $databaseName = "gcm";   

        $connectionInfo = array( "UID"=>$uid, "PWD"=>$pwd, "Database"=>$databaseName); 


             $db = sqlsrv_connect($serverName,$connectionInfo) or die("Unable to connect to server");

             $query = "DELETE FROM gcmUser2 WHERE gcuRegID = '$regid'";
             $result = sqlsrv_query($db, $query);


    }

回答1:

When the GCM server attempts to send the message to the device after the app has been uninstalled, the GCM client detects that this app is not longer installed on the device. You don't do it in your application code. The GCM client component of the Android OS does it.

The next time to try to send a message to the app on the device that uninstalled it, the GCM server will already know it has been uninstalled, and send you the NotRegistered error.

There is no lifecycle method called when the app is removed from the device. If there was, you wouldn't need the sequence of events you quoted above in order for the GCM server and the 3rd party server to detect that the app was uninstalled (since you could have used such a method to both unregister your app from the GCM server and to let the 3rd party server know the app was uninstalled from that device).