1

I want to register my app for GCM services just after the user installs my app(i.e. before launch). Is this possible? Or else is there a hack available.

Best Geek
  • 11
  • 2

1 Answers1

3

If you want follow the standard GCM client app registration process, your app has to launch in order register, because the app server and the client app must complete a client/server "handshake."

From the GCM documentation, it states that the client obtains a unique registration token and passes it to the app server, which stores the token and sends an acknowledgement back to the client app. The registration token exchanged in the handshake process is the same client app instance identifier used subsequently to send messages to the client.

so, if your app is not launched, you can not pass the registration token to the app server. For android, you need to use the Instance ID API to complete the process.

Sample code:

InstanceID instanceID = InstanceID.getInstance(this);
String token = instanceID.getToken(getString(R.string.gcm_defaultSenderId),
        GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);

(notice the R.string.gcm_defaultSenderID is a project number you acquire from the API console. )

There was a hack in this post, if you want to start a service after an app is installed. But it might not be a good practice for you to complete the registration process, because you hard code the Sender ID, Application ID and Sender Auth Token listed in this page.

Community
  • 1
  • 1
ztan
  • 6,861
  • 2
  • 24
  • 44