Analysis: getting a callback when GCM registration is complete

I would like to send the GCM deviceToken to my server so that I can trigger push notifications using the REST API. All this works, except that I cannot reliably get the deviceToken when it becomes available. When I register an application to receive push notifications on a broadcast channel, I check deviceToken for a done() callback. However, it is often not yet installed. I'm looking for a way to get deviceToken the moment it becomes available, so I can avoid polling or wait for the application to restart to send push notifications.

What i tried

Capture device Register in the channel registration callback

 Parse.initialize(this, applicationId, clientKey) { ParsePush.subscribeInBackground("", new SaveCallback() { @Override public void done(ParseException e) { if (e == null) { String deviceToken = (String) ParseInstallation.getCurrentInstallation().get("deviceToken"); // deviceToken is often still null here. } } }); 

Device Capture Tap in ParseInstallation.saveInBackground ()

 final ParseInstallation parseInstallation = ParseInstallation.getCurrentInstallation(); parseInstallation.saveInBackground(new SaveCallback() { @Override public void done(ParseException e) { String deviceToken = (String) parseInstallation.get("deviceToken"); // deviceToken is often still null here. } }); 

Listening to the GCM registration event yourself by subclassing com.parse.GcmBroadcastReceiver

 // Which I can't do, because it declared final. public final void onReceive(Context context, Intent intent) { PushService.runGcmIntentInService(context, intent); } 
+5
source share
1 answer

The purpose of the deviceToken field is to save the subscription ID needed to deliver messages to this device through GCM. Intercepting GCM registration calls to capture the registration ID for the sole purpose of targeting push notifications to this device may not be the best approach.

While you can use deviceToken in a request to send push notifications to this device, you may need to use any of the other fields of the installation object itself. Keep in mind that the ParseInstallation object extends ParseObject , so you can add any fields to the installation object that can help you configure push notifications on this device.

If you don’t have any specific criteria that will help you identify this single user of your application and you are looking for a unique identifier, can I suggest storing objectId on your server for targeting purposes? objectId is available as soon as the installation callback is executed, regardless of the current state of the GCM registration process.

+2
source

Source: https://habr.com/ru/post/1214693/


All Articles