I am facing one problem while implementing FCM in Android application.
Everything works fine, my service onMessageReceived()
is not calling. But I am ge
First, follow instructions from this guide, they are very clear and easy, or you can clone sample code from here and see for yourself what is wrong.
Secondly have you read these comments regarding onMessageReceived()
// [START_EXCLUDE]
// There are two types of messages data messages and notification messages. Data messages are handled
// here in onMessageReceived whether the app is in the foreground or background. Data messages are the type
// traditionally used with GCM. Notification messages are only received here in onMessageReceived when the app
// is in the foreground. When the app is in the background an automatically generated notification is displayed.
// When the user taps on the notification they are returned to the app. Messages containing both notification
// and data payloads are treated as notification messages. The Firebase console always sends notification
// messages. For more see: https://firebase.google.com/docs/cloud-messaging/concept-options
// [END_EXCLUDE]
Look for following comment in this class
// 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.
For more see: https://firebase.google.com/docs/cloud-messaging/concept-options
Firebase will not call your onMessageReceived()
when your app is in the background or killed, and you can't customise your notification. It will show notifications managed by Firebase it self.
to make Firebase library to call your onMessageReceived()
in every case
a) Foreground
b) Background
c) Killed
Do not put JSON key "notification" in your request to Firebase API, use "data" instead, see below.
For example, following message will not call onMessageReceived()
{
"to": "/topics/test",
"notification": {
"title" : "title",
"message": "data!"
}
}
but this will work
{
"to": "/topics/test",
"data": {
"title":"title",
"message":"data!"
}
}
see this it has a detailed description of firebase message type For example:
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
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());
sendNotification(remoteMessage.getData().get("message").toString(),
remoteMessage.getData().get("title").toString());
}
}
private void sendNotification(String message, String title) {
int requestID = (int) System.currentTimeMillis();
Intent intent = new Intent(this, activityCompat);
PendingIntent pendingIntent = PendingIntent.getActivity(this, requestID, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.small_logo)
.setContentTitle(title)
.setContentText(message).setContentIntent(pendingIntent)
.setAutoCancel(true)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(messageBody))
.setTicker(messageBody);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationBuilder.getNotification().flags |= Notification.FLAG_AUTO_CANCEL;
Notification notification = notificationBuilder.build();
notificationManager.notify(requestID, notification);
}