FCM Message Prioritization and De-Prioritization
Understanding High-Priority Messages
Firebase Cloud Messaging (FCM) offers a high-priority delivery option for messages that require immediate attention, like notifications for critical events. This priority setting ensures the messages bypass the device’s battery saving mechanisms and are delivered promptly.
Conditions for De-Prioritization
While high-priority messages are prioritized, FCM does have mechanisms to de-prioritize them under certain conditions:
- Excessive Usage: If an app sends a high volume of high-priority messages, FCM might start de-prioritizing messages to prevent abuse and ensure fairness for all apps.
- Network Conditions: In situations where the device is experiencing poor network connectivity, FCM might delay or de-prioritize high-priority messages to avoid unnecessary battery consumption and network strain.
- Device Power Settings: Users have the option to enable or disable Do Not Disturb (DND) mode on their devices. If DND is active, FCM may suppress or delay high-priority notifications to ensure a quiet and uninterrupted experience for the user.
- App Idle State: If an app is inactive for a long period, FCM might de-prioritize messages to minimize battery usage and prevent unnecessary background activity.
Retrieving Cached FCM Messages
Why Cached Messages Exist
FCM messages, both high-priority and normal priority, are cached on the device when it’s offline. These messages are stored locally and are delivered once the device re-establishes an internet connection.
Methods for Fetching Cached Messages
- Android: Android developers can leverage the FCM API’s
FirebaseMessagingService.onMessageReceived()
method. This method triggers whenever a message is received, including messages retrieved from the cache. - iOS: In iOS, the
FIRMessagingDelegate
protocol provides methods likemessaging:didReceiveRegistrationToken:
andmessaging:didReceiveRemoteNotification:fetchCompletionHandler:
for handling both new and cached messages.
Within these methods, developers can differentiate between newly received and cached messages based on metadata provided by the FCM API.
Code Example (Android)
Java
import com.google.firebase.messaging.FirebaseMessagingService; import com.google.firebase.messaging.RemoteMessage; public class MyFirebaseMessagingService extends FirebaseMessagingService { @Override public void onMessageReceived(RemoteMessage remoteMessage) { // Check if the message is cached if (remoteMessage.getFrom().contains("/topics")) { // This indicates the message is cached } else { // This indicates the message is newly received } } }