-
Notifications
You must be signed in to change notification settings - Fork 2
Notification support
- Android Studio must be updated to latest version
- Project must have these requirements:
- Targets API level 19 (KitKat) or higher
- Uses Android 4.4 or higher
- Uses Jetpack (AndroidX), which includes these requirements:
com.android.tools.build:gradle
v7.3.0 or latercompileSdkVersion
28 or later
- Set up a physical device or emulator to run app
- Sign into Firebase
Note: This will involve using the Firebase console and the Android project.
- Create a Firebase project
- Register app with Firebase
- In Firebase console, click on Android icon or 'Add app' to launch setup
- Enter app's package name in 'Android package name' field
- Optional - Add app nickname and debug signing certificate SHA-1
- Click 'Register app'
- Add a Firebase configuration file
- Download and add Firebase Android configuration file (
google-services.json
) to app- Move config file into module root directory of the app
- Ensure Google services Gradle plugin is added as a dependency (usally in
<project>/build.gradle.kts
or<project>/build.gradle
)- In the app-level Gradle file (will usually be in
<project>/<app-module>/build.gradle.kts
or<project>/<app-module>/build.gradle
- Add Firebase SKDs to app
- In the app-level Gradle file, add the appropriate Firebase project dependencies
- After adding the dependencies, sync Android project with Gradle files
- Add the following code to AndroidManifest.xml:
<service
android:name=".java.MyFirebaseMessagingService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
Optional:
- Include metadata elements to set default notification icon and color:
<!-- Set custom default icon. This is used when no icon is set for incoming notification messages.
See README(https://goo.gl/l4GJaQ) for more. -->
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/ic_stat_ic_notification" />
<!-- Set color used with incoming notification messages. This is used when no color is set for the incoming
notification message. See README(https://goo.gl/6BKBk7) for more. -->
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="@color/colorAccent" />
- Set default notification channel - if desired,
set default_notification_channel_id
to the ID of your notification channel:
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="@string/default_notification_channel_id" />
- Request runtime notification permission on Android 13 and up
By default, FCM SDK (version 23.0.6 and higher) includes the
POST_NOTIFICATIONS
permission defined in the manifest. To request the runtime version of this permission, useandroid.permission.POST_NOTIFICATIONS
. To request the new runtime permission, use this code:
// Declare the launcher at the top of your Activity/Fragment:
private val requestPermissionLauncher = registerForActivityResult(
ActivityResultContracts.RequestPermission(),
) { isGranted: Boolean ->
if (isGranted) {
// FCM SDK (and your app) can post notifications.
} else {
// TODO: Inform user that that your app will not show notifications.
}
}
private fun askNotificationPermission() {
// This is only necessary for API level >= 33 (TIRAMISU)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) ==
PackageManager.PERMISSION_GRANTED
) {
// FCM SDK (and your app) can post notifications.
} else if (shouldShowRequestPermissionRationale(Manifest.permission.POST_NOTIFICATIONS)) {
// TODO: display an educational UI explaining to the user the features that will be enabled
// by them granting the POST_NOTIFICATION permission. This UI should provide the user
// "OK" and "No thanks" buttons. If the user selects "OK," directly request the permission.
// If the user selects "No thanks," allow the user to continue without notifications.
} else {
// Directly ask for the permission
requestPermissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS)
}
}
}
- Notification permissions for Android 12L (API level 32) or lower Android will automatically ask the user for permission the first time a notification channel is created as long as the app is in the foreground. Important things to note:
- If the app creates its first notification channel when it is running in the background (which the FCM SDK does when receiving an FCM notification), Android will not allow the notification to be displayed. The user will also not receive a prompt for notification permission, so any notifications received before the app is opened, and the user accepts the permission, will be lost.
- It is recommended to update to Android 13+ in order to utilize the platform's APIs request permission. If that is not possible, a notification channel should be created before any notifications are sent to the app in order to trigger the notification permission dialog.
- Access device registration token A registration token for client app instance will be generated on initial app startup. To retrieve the token and monitor changes to the token, use the following code:
- Retrieve current registration token:
FirebaseMessaging.getInstance().token.addOnCompleteListener(OnCompleteListener { task ->
if (!task.isSuccessful) {
Log.w(TAG, "Fetching FCM registration token failed", task.exception)
return@OnCompleteListener
}
// Get new FCM registration token
val token = task.result
// Log and toast
val msg = getString(R.string.msg_token_fmt, token)
Log.d(TAG, msg)
Toast.makeText(baseContext, msg, Toast.LENGTH_SHORT).show()
})
- Monitor token generation (the token may change when the app is restored on a new device, the app is uninstalled/reinstalled, or the app data is cleared):
/**
* Called if the FCM registration token is updated. This may occur if the security of
* the previous token had been compromised. Note that this is called when the
* FCM registration token is initially generated so this is where you would retrieve the token.
*/
override fun onNewToken(token: String) {
Log.d(TAG, "Refreshed token: $token")
// If you want to send messages to this application instance or
// manage this apps subscriptions on the server side, send the
// FCM registration token to your app server.
sendRegistrationToServer(token)
}
After the token is obtained, it can be sent to the app server using any method.
5. Check for Google Play services
Apps should always check device for a compatible Google Play services APK before accessing Google Play services features. This should be done in two places - in the main activity's onCreate()
method, and in its onResume()
method. The onCreate()
check ensures the app cannot be used without a successful check, and the onResume()
check ensures that if the user returns to the running app through some other means, such as through the back button, the check is still performed.
If the device does not have a compatible version of Google Play services, the app can call GoogleApiAvailability.makeGooglePlayServicesAvailable()
so that users can download Google Play services from the Play Store.
6. Prevent auto initialization
If autogenerating FCM registration tokens is not preferred, the Analytics collection and FCM auto initialization must both be disabled by adding the following metadata values to AndroidManifest.xml
:
<meta-data
android:name="firebase_messaging_auto_init_enabled"
android:value="false" />
<meta-data
android:name="firebase_analytics_collection_enabled"
android:value="false" />
Reenabling FCM auto-init:
Firebase.messaging.isAutoInitEnabled = true
Reenabling Analytics collection:
Call setAnalyticsCollectionEnabled()
method of the FirebaseAnalytics
class