Receive Messages in an Android App | Firebase
original source : https://firebase.google.com/docs/cloud-messaging/android/receive
Receive Messages in an Android App
Firebase notifications behave differently depending on the foreground/background state of the receiving app. If you want foregrounded apps to receive notification messages or data messages, you’ll need to write code to handle the onMessageReceived
callback. For an explanation of the difference between notification and data messages, see Message types.
Handling messages
To receive messages, use a service that extends FirebaseMessagingService. Your service should override the onMessageReceived
and onDeletedMessages
callbacks. It should handle any message within 10 seconds of receipt. After that, Android does not guarantee execution, and could terminate your process at any time. If your app needs more time to process a message, use the Firebase Job Dispatcher.
onMessageReceived
is provided for most message types, with the following exceptions:
- Notification messages delivered when your app is in the background. In this case, the notification is delivered to the device’s system tray. A user tap on a notification opens the app launcher by default.
- Messages with both notification and data payload, both background and foreground. In this case, the notification is delivered to the device’s system tray, and the data payload is delivered in the extras of the intent of your launcher Activity.
{
"to": “/topics/journal”,
"notification": {
"title" : “title”,
"text": “data!”,
"icon": “ic_notification”
}
}
{
"to": “/topics/dev_journal”,
“data”: {
“text”:“text”,
“title”:“”,
“line1”:“Journal”,
“line2”:“刊物”
}
}
위와 같이 data와 notification가 하나만 있는게 보통이지만 둘다 있는 경우는 특별한 경우가 되며 이경우에는 위에서 언급한 것처럼
the notification is delivered to the device’s system tray, and the data payload is delivered in the extras of the intent of your launcher Activity. 가 수행된다.

For more information about message types, see
Notifications and data messages
.For all messages where
onMessageReceived
is provided, your service should handle any message within 10 seconds of receipt. If your app needs more time to process a message, use the
.
Edit the app manifest
To use FirebaseMessagingService
, you need to add the following in your app manifest:
<service
android:name=".MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>AndroidManifest.xml
Also, you’re recommended to set default values to customize the appearance of notifications. You can specify a custom default icon and a custom default color that are applied whenever equivalent values are not set in the notification payload.
Add these lines inside the application
tag to set the custom default icon and custom 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" />AndroidManifest.xml
Android displays the custom default icon for
- All notification messages sent from the Notifications composer.
- Any notification message that does not explicitly set the icon in the notification payload.
Android uses the custom default color for
- All notification messages sent from the Notifications composer.
- Any notification message that does not explicitly set the color in the notification payload.
If no custom default icon is set and no icon is set in the notification payload, Android displays the application icon rendered in white.
Override
onMessageReceived
By overriding the method FirebaseMessagingService.onMessageReceived
, you can perform actions based on the received RemoteMessage object and get the message data:
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// ...
// TODO(developer): Handle FCM messages here.
// Not getting messages here? See why this may be: https://goo.gl/39bRNJ
Log.d(TAG, "From: " + remoteMessage.getFrom());
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
if (/* Check if data needs to be processed by long running job */ true) {
// For long-running tasks (10 seconds or more) use Firebase Job Dispatcher.
scheduleJob();
} else {
// Handle message within 10 seconds
handleNow();
}
}
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
}
// Also if you intend on generating your own notifications as a result of a received FCM
// message, here is where that should be initiated. See sendNotification method below.
}MyFirebaseMessagingService.java
Override
onDeletedMessages
In some situations, FCM may not deliver a message. This occurs when there are too many messages (>100) pending for your app on a particular device at the time it connects or if the device hasn’t connected to FCM in more than one month. In these cases, you may receive a callback to
FirebaseMessagingService.onDeletedMessages()
When the app instance receives this callback, it should perform a full sync with your app server. If you haven’t sent a message to the app on that device within the last 4 weeks, FCM won’t call
onDeletedMessages()
.Handle notification messages in a backgrounded app
When your app is in the background, Android directs notification messages to the system tray. A user tap on the notification opens the app launcher by default.
This includes messages that contain both notification and data payload (and all messages sent from the Notifications console). In these cases, the notification is delivered to the device’s system tray, and the data payload is delivered in the extras of the intent of your launcher Activity.
FCM을 통해 notification 이 전달되었는데 앱이 background에 있는 경우. onMessageReceived를 거치지 않으며 바로 system tray에 들어 가게 되며 이를 클릭하게 되면 launcher activity가 열리게 된다. FCM에서 전달된 JSON안에 data와 notification이 둘다 있는경우 data는 intent의 extras에 저장되어 launcher activity가 열리는 intent가 된다.