# Getting the registration identifier

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.

**Example receiver implementation**

{% tabs %}
{% tab title="Kotlin" %}

```kotlin
class MyTokenReceiver: BroadcastReceiver() {
    override fun onReceive(context: Context?, intent: Intent?) {
        if (intent != null) {
            val provider = intent.getStringExtra(Batch.EXTRA_REGISTRATION_PROVIDER_NAME)
            val identifier = intent.getStringExtra(Batch.EXTRA_REGISTRATION_IDENTIFIER)
            Log.i("MyTokenReceiver", "Got registration (from provider $provider): $identifier")
        }
    }
}
```

{% endtab %}

{% tab title="Java" %}

```java
public class MyTokenReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        String provider = intent.getStringExtra(Batch.EXTRA_REGISTRATION_PROVIDER_NAME);
        String identifier = intent.getStringExtra(Batch.EXTRA_REGISTRATION_IDENTIFIER);
        Log.i("MyTokenReceiver", "Got registration (from provider " + provider + "): " + identifier);
    }
}
```

{% endtab %}
{% endtabs %}

**Manifest registration**

{% code title="AndroidManifest.xml" %}

```xml
<receiver android:name=".MyTokenReceiver"
    android:permission="${applicationId}.batch.permission.INTERNAL_BROADCAST">
    <intent-filter>
        <action android:name="com.batch.android.intent.action.push.REGISTRATION_IDENTIFIER_OBTAINED" />
    </intent-filter>
</receiver>
```

{% endcode %}

{% hint style="info" %}
`${applicationId}` is a Gradle macro, which will automatically be replaced when building
{% endhint %}

**Runtime registration**

{% tabs %}
{% tab title="Kotlin" %}

```kotlin
class MyApplication: Application() {
    var tokenReceiver = MyTokenReceiver()

    override fun onCreate() {
        super.onCreate()
        // [...]
        registerReceiver(tokenReceiver,
            IntentFilter(Batch.ACTION_REGISTRATION_IDENTIFIER_OBTAINED),
            Batch.getBroadcastPermissionName(this), null)
    }
}
```

{% endtab %}

{% tab title="Java" %}

```java
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);
    }
}

```

{% endtab %}
{% endtabs %}

{% hint style="info" %}
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.
{% endhint %}

### Pulling the value

If you do not want to setup a listener, but want to simply fetch the registration identifier, you can simply call:

{% tabs %}
{% tab title="Kotlin" %}

```kotlin
Batch.Push.getRegistration()?.token
```

{% endtab %}

{% tab title="Java" %}

```java
BatchPushRegistration registration = Batch.Push.getRegistration();
if (registration != null) {
    String registrationToken = registration.getToken()
}
```

{% endtab %}
{% endtabs %}

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 *after* `Batch.onStart()`. It is useful if you want to show the token in your UI, for example in a debug view.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://doc.batch.com/developer/sdk/android/advanced/getting-registration.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
