Skip to contentSkip to navigationSkip to topbar
Page toolsOn this page
Looking for more inspiration?Visit the

Generate a device token for Android


(new)

Beta

The Push Notifications API is currently available as a Private Beta product. The information contained in this document is subject to change. You acknowledge and agree that your use of the Twilio Push Notifications API is subject to the terms of the Services in Private Beta(link takes you to an external page). This means that some features are not yet implemented and others may be changed before the product is declared as Generally Available. Private Beta products are not covered by the Twilio Support Terms or Twilio Service Level Agreement.

To send push notifications to Android devices with Twilio, your app needs a Firebase Cloud Messaging (FCM) registration token. This token uniquely identifies the app instance on a device. You then send this token to Twilio by including it in the token field when sending a notification or registering a device.


Prerequisites

prerequisites page anchor
  • An Android project targeting Android 6.0 (API level 23) or higher.
  • Firebase added to your Android project. Follow the Add Firebase to your Android project(link takes you to an external page) guide if you haven't already.

Add the Firebase Cloud Messaging dependency to your module-level build.gradle file:

1
dependencies {
2
implementation 'com.google.firebase:firebase-messaging'
3
}

Request notification permission on Android 13+

request-notification-permission-on-android-13 page anchor

Android 13 (API level 33) and higher require a runtime permission to display notifications. The FCM SDK includes the POST_NOTIFICATIONS permission in its manifest, but your app must also request it at runtime.

KotlinJava
1
private val requestPermissionLauncher = registerForActivityResult(
2
ActivityResultContracts.RequestPermission(),
3
) { isGranted: Boolean ->
4
if (isGranted) {
5
// FCM SDK (and your app) can post notifications.
6
} else {
7
// Inform the user that notifications are disabled.
8
}
9
}
10
11
private fun askNotificationPermission() {
12
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
13
if (ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) ==
14
PackageManager.PERMISSION_GRANTED
15
) {
16
// Permission already granted.
17
} else {
18
requestPermissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS)
19
}
20
}
21
}

Retrieve the FCM registration token

retrieve-the-fcm-registration-token page anchor

Call FirebaseMessaging.getInstance().getToken() to retrieve the current registration token. This makes a network call on the first request and returns a cached token on subsequent calls.

KotlinJava
1
FirebaseMessaging.getInstance().token.addOnCompleteListener { task ->
2
if (!task.isSuccessful) {
3
Log.w(TAG, "Fetching FCM registration token failed", task.exception)
4
return@addOnCompleteListener
5
}
6
7
val token = task.result
8
// Send this token to Twilio using the DeviceRegistrations API
9
// or include it directly in a PushNotifications request.
10
}

FCM tokens can rotate at any time. To ensure your app always has a valid token, extend FirebaseMessagingService and override onNewToken. Send the updated token to your server so Twilio can continue delivering notifications.

KotlinJava
1
class MyFirebaseMessagingService : FirebaseMessagingService() {
2
override fun onNewToken(token: String) {
3
// Send the updated token to your server.
4
sendRegistrationToServer(token)
5
}
6
}

Register the service in your AndroidManifest.xml:

1
<service
2
android:name=".MyFirebaseMessagingService"
3
android:exported="false">
4
<intent-filter>
5
<action android:name="com.google.firebase.MESSAGING_EVENT" />
6
</intent-filter>
7
</service>

The token may change when:

  • The app is restored on a new device.
  • The user uninstalls and reinstalls the app.
  • The user clears app data.

Send the token to Twilio

send-the-token-to-twilio page anchor

After you retrieve the token, include it in Twilio Push Notifications API requests using "provider": "FCM". You can either: