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?
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:
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With