Ololoking Ololoking - 1 year ago 202
Android Question

Push notification works incorrect when app is on background or not running

I am using Firebase Cloud Messaging to send push notifications .

Here is my FirebaseMessageService:

public class FireBaseMessageService extends FirebaseMessagingService {

public void onMessageReceived(RemoteMessage remoteMessage) {
Log.e("TAG", "From: " + remoteMessage.getFrom());
Log.e("TAG", "Notification Message Body: " + remoteMessage.getData().get("CardName")+" : "+remoteMessage.getData().get("CardCode"));

private void sendNotification(String messageBody) {
Intent intent = new Intent(this, StartActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,

Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)

NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());

and FirebaseInstanceServer:

public class FirebaseInstanceService extends FirebaseInstanceIdService {

public void onTokenRefresh() {
// Get updated InstanceID token.
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.e("TAG", "Refreshed token: " + refreshedToken);

// TODO: Implement this method to send any registration to your app's servers.


private void sendRegistrationToServer(String token) {

// Add custom implementation, as needed.
Log.e("TAG", "Refreshed token2: " + token);

which a declared in AndroidManifest:

<action android:name="com.google.firebase.MESSAGING_EVENT"/>

<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>

So issue is that when App is running
is shown well and notification come with defauld sound, but when App is in foreground or is not running notification come without any sound and
is not shown in statusBar. How can repair this issue or where i make mistake?

Answer Source

With FCM you specify a POST payload to send to https://fcm.googleapis.com/fcm/send. In that payload you can specify a data or a notification key, or both.

If your payload contains only a data key, your app will handle all push messages itself. E.g. they are all delivered to your onMessageReceived handler.

If your payload contains a notification key, your app will handle push messages itself only if your app is active/in the foreground. If it is not (so it's in the background, or closed entirely), FCM handles showing the notification for you by using the values you put into the notification key payload.

Looks like you want to be handling the FCM messages yourself so you can customize the notification a bit more etc, so it would be better to not include the notification key in the POST payload, so all push messages are delivered to your onMessageReceived.

You can read more about it here:
Advanced messagaging options
Downstream message syntax

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download