Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When will InstanceIDListenerService be called and how to test it?

With the last changes to Android GCM now a InstanceIDListenerService is provided to be able to listen to token refreshes by overriding the onTokenRefresh method.

But when will this method be called? And is there any way to test it manually?

like image 540
Eylen Avatar asked Jun 04 '15 07:06

Eylen


2 Answers

To test it manually from the command line run:

adb shell am startservice -a com.google.android.gms.iid.InstanceID --es "CMD" "RST" -n your.package.name/your.own.MyInstanceIDListenerService 

where:

  • Your app's package is your.package.name
  • The class name of your InstanceIDListenerService implementation is your.own.MyInstanceIDListenerService

This will send an intent to the service with the extras expected by the base class.

For this to work, the service needs to be set to exported temporarily:

<service     android:name="your.own.MyInstanceIDListenerService"     android:exported="true">     <intent-filter>         <action android:name="com.google.android.gms.iid.InstanceID"/>     </intent-filter> </service> 

Note: this should only be done temporarily and never be exported in production or else other apps could access your service.

like image 62
Hermit Avatar answered Sep 22 '22 08:09

Hermit


onTokenRefresh() would be called if the token for your application has been updated by the Instance ID service. The main reason for onTokenRefresh() being called is to allow you to update your app server with the new token so it can send messages to your app.

You should not have to test the token value manually. The token is used mainly by your application server to send messages to your app. Thus when your application first runs you should call InstanceID.getToken() and send the token to your server. Then later if the token is updated you call InstanceID.getToken() again and send the new value to your server again.

Check here for an example.

like image 44
Arthur Thompson Avatar answered Sep 22 '22 08:09

Arthur Thompson