When should a request for an instance and registration token be requested in GCM?

Background

I am developing an Android application that requires notifications to be pushed from a web application server to this application. To do this, the application has to query Google Instance ID service for a unique instance ID using

String iid = InstanceID.getInstance().getID();

      

Question

  • Should "21" be called inside the onCreate()

    main action that is triggered by the user? This would mean that a new instance of the instance will be returned every time, so this doesn't seem right.
  • Am getId()

    I getting it right that every time I get called I get the same instance ID?
  • Should it be called getId()

    from a background thread or is it okay to call it from the main UI thread? The documentation doesn't say to call it from a background thread, but because this method involves connecting to a cloud service, it might block the UI thread.
+3


source to share


2 answers


1 and 2. The ID must be stable (unchanged) for your app to live on the device, but there is a bug , so I wouldn't rely on that yet. Since it has no other use yet, I wouldn't bother calling getID (). If this bug is fixed, it might be useful as a unique identifier for your applications, or use it in the future.

  1. getID () does not make a server request, so it can be called from the main thread, however I am assuming it does flash read / write, so the best practice would be to call it a background thread.


Note that getToken () appears to be resistant to application reloads and must be called on a background thread as it makes a server request.

+1


source


They don't use getId () in the example because they don't really need to communicate with gcm.



What you really need to communicate with GCM is the token obtained by the getToken (Project_Id, "GCM") method and the GCMHandler (broadcast receiver) which will receive data from the gcm api.

0


source







All Articles