A Registration Identifier (also called Registration Token or Push Token) is, by default, automatically fetched by Batch when starting.
For various reasons, you might want to get this identifier: you might be using another library which requires it, or simply want to handle it on your own.
There are two ways to accomplish this.
Listening to the broadcast
Batch will broadcast a message to your application when a registration identifier is fetched, whether it changed or not.
As any standard broadcast receiver, you can either register it in your manifest, or at runtime.
${applicationId} is a Gradle macro, which will automatically be replaced when building
Runtime registration
class MyApplication: Application() {
var tokenReceiver = MyTokenReceiver()
override fun onCreate() {
super.onCreate()
// [...]
registerReceiver(tokenReceiver,
IntentFilter(Batch.ACTION_REGISTRATION_IDENTIFIER_OBTAINED),
Batch.getBroadcastPermissionName(this), null)
}
}
public class MyApplication extends Application {
private MyTokenReceiver tokenReceiver = new MyTokenReceiver();
@Override
public void onCreate() {
super.onCreate();
// [...]
registerReceiver(tokenReceiver,
new IntentFilter(Batch.ACTION_REGISTRATION_IDENTIFIER_OBTAINED),
Batch.getBroadcastPermissionName(this), null);
}
}
Even though the broadcast is scoped to the current package, it can be protected by a permission so that other apps cannot inject a broadcast into your app.
Pulling the value
If you do not want to setup a listener, but want to simply fetch the registration identifier, you can simply call:
The getRegistration() method will return null if no registration identifier has been fetched or if Batch isn't started, meaning that you have to call this method afterBatch.onStart(). It is useful if you want to show the token in your UI, for example in a debug view.