Generate a device token for Android
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. 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.
- 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 guide if you haven't already.
Add the Firebase Cloud Messaging dependency to your module-level build.gradle file:
1dependencies {2implementation 'com.google.firebase:firebase-messaging'3}
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.
1private val requestPermissionLauncher = registerForActivityResult(2ActivityResultContracts.RequestPermission(),3) { isGranted: Boolean ->4if (isGranted) {5// FCM SDK (and your app) can post notifications.6} else {7// Inform the user that notifications are disabled.8}9}1011private fun askNotificationPermission() {12if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {13if (ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) ==14PackageManager.PERMISSION_GRANTED15) {16// Permission already granted.17} else {18requestPermissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS)19}20}21}
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.
1FirebaseMessaging.getInstance().token.addOnCompleteListener { task ->2if (!task.isSuccessful) {3Log.w(TAG, "Fetching FCM registration token failed", task.exception)4return@addOnCompleteListener5}67val token = task.result8// Send this token to Twilio using the DeviceRegistrations API9// 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.
1class MyFirebaseMessagingService : FirebaseMessagingService() {2override fun onNewToken(token: String) {3// Send the updated token to your server.4sendRegistrationToServer(token)5}6}
Register the service in your AndroidManifest.xml:
1<service2android:name=".MyFirebaseMessagingService"3android: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.
After you retrieve the token, include it in Twilio Push Notifications API requests using "provider": "FCM". You can either:
- Send directly: Include the token in the
toarray when sending a notification. - Register the device: Store the token as a DeviceRegistration so you can send to Users by
userId.
- Send a push notification using the token.
- Obtain FCM credentials to upload to Twilio.
- Review the Firebase Cloud Messaging Android setup guide for additional configuration options.