Using cloud messaging for push notifications has minimal impact on app frame rate and UI smoothness because notifications are handled by the system outside your app's main thread. However, frequent or heavy payloads can increase battery use and network data consumption. Keep notification payloads small and avoid waking the app unnecessarily to preserve battery life.
Cloud Messaging (push notifications) in Android Kotlin - Build, Publish & Deploy
To keep your app running smoothly at 60fps, avoid processing heavy tasks directly in the notification handler. Use WorkManager or background threads for any data syncing triggered by notifications. Limit the frequency of notifications and batch updates when possible. Also, use data-only messages sparingly to reduce wake-ups and conserve battery.
Integrating Firebase Cloud Messaging (FCM) adds a small increase to your app size, typically a few megabytes due to Firebase libraries. This is considered medium impact. The startup time impact is minimal since FCM runs as a background service managed by Google Play Services, not your app process. Keep dependencies minimal to avoid unnecessary size growth.
On Android, Firebase Cloud Messaging uses Google Play Services to receive and display notifications. On iOS, Apple Push Notification service (APNs) is used, and Firebase acts as a wrapper. iOS requires explicit user permission to show notifications, while Android grants permission by default (starting from Android 13, explicit permission is required). Also, iOS limits background processing time after receiving notifications more strictly than Android.
- Ensure your app requests notification permissions clearly and respectfully, explaining why notifications are needed.
- Do not send spammy or irrelevant notifications; follow Apple App Store and Google Play policies on user experience.
- Handle user opt-out gracefully and do not bypass system notification settings.
- Comply with privacy policies regarding user data used in notifications.
Your app takes 5 seconds to load this screen after receiving a push notification. What's likely wrong?
Answer: The notification handler is doing heavy work on the main thread, blocking UI rendering. Move processing to background threads or schedule work asynchronously to fix this.
Practice
Solution
Step 1: Understand FCM's role
Firebase Cloud Messaging is designed to send messages or notifications to users' devices.Step 2: Identify the correct purpose
It works even if the app is closed, keeping users updated with push notifications.Final Answer:
To send push notifications to users even when the app is closed -> Option CQuick Check:
FCM purpose = push notifications [OK]
- Confusing FCM with local data storage
- Thinking FCM manages UI elements
- Assuming FCM controls app permissions
Solution
Step 1: Identify the service for message handling
FirebaseMessagingService is the class designed to receive and handle push notifications.Step 2: Exclude unrelated Firebase classes
FirebaseDatabase, FirebaseAuth, and FirebaseStorage serve different purposes like data storage, authentication, and file storage.Final Answer:
FirebaseMessagingService -> Option AQuick Check:
Message handler class = FirebaseMessagingService [OK]
- Choosing FirebaseDatabase for message handling
- Confusing FirebaseAuth with messaging
- Using FirebaseStorage instead of messaging service
override fun onMessageReceived(remoteMessage: RemoteMessage) {
val title = remoteMessage.notification?.title ?: "No Title"
val body = remoteMessage.notification?.body ?: "No Body"
Log.d("FCM", "Title: $title, Body: $body")
}Solution
Step 1: Analyze null safety with Elvis operator
The code uses ?: to provide default strings if notification or its fields are null.Step 2: Understand logging behavior
If notification is null, title and body become "No Title" and "No Body" respectively, so the log shows these defaults.Final Answer:
Title: No Title, Body: No Body if notification is null -> Option AQuick Check:
Elvis operator handles nulls = default text logged [OK]
- Assuming app crashes on null notification
- Thinking title/body become empty strings
- Believing only body is logged
FirebaseMessaging.getInstance().token.addOnCompleteListener {
if (!it.isSuccessful) return@addOnCompleteListener
val token = it.result
Log.d("FCM", "Token: $token")
}Solution
Step 1: Check the API for token retrieval
FirebaseMessaging.getInstance().getToken() is the correct method; there is no .token property.Step 2: Verify other options
Internet permission is needed to run, but the code won't compile due to undefined token property. getInstance() without context works, and addOnCompleteListener is correct.Final Answer:
You should use getToken() instead of token property -> Option DQuick Check:
No token property = use getToken() [OK]
- Forgetting internet permission
- Thinking context is required for getInstance()
- Using wrong listener type for token retrieval
Solution
Step 1: Understand device token role
Device tokens uniquely identify devices to receive push notifications.Step 2: Implement server-side filtering
Send tokens to your server and filter them by user preferences before sending notifications.Final Answer:
Send the device token to your server and filter tokens based on user preferences -> Option BQuick Check:
Token + server filtering = targeted notifications [OK]
- Sending notifications to all devices without filtering
- Using FirebaseDatabase instead of server filtering
- Confusing authentication with notification targeting
Ready to prove it?
Pass the test and this pattern moves unknown
