When will InstanceIDListenerService be called and how to test it?

Eylen picture Eylen · Jun 4, 2015 · Viewed 10.3k times · Source

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?

Answer

Hermit picture Hermit · Aug 9, 2015

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.