Koldar
Koldar

Reputation: 1407

Notifications from firebase when app is in foreground not called

I'm trying to setup a system where an Android mobile phone receives notification from a server: for this task we have chosen Firebase. While the application is in background everything works fine: after I push a message from the firebase console a notification appears on the system tray and, after the message is clicked by the user, an Intent with extra data is sent to the Activity.

My problem is uniquely when the application is already in foreground. The firebase notification is pretty clear: if the application is in foreground, no matter which type of message is sent by the server, OnMessageReceived is never called... so why it fails with my simple app? Some notes to help you solve this issue:

Below you can find all the source code used:

Main Activity

package it.bagozi.ada.tutorial.firebase;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = MainActivity.class.getSimpleName();

    private TextView notification;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        this.notification = (TextView) this.findViewById(R.id.notification_setter);

        Intent i = this.getIntent();
        Log.e(TAG, "Intent from Activity caller: " + i);

        if (getIntent().getExtras() != null) {
            for (String key : getIntent().getExtras().keySet()) {
                String value = getIntent().getExtras().getString(key);
                Log.d(TAG, "Key: " + key + " Value: " + value);
            }
        }

    }
}

FirebaseNotificationService

package it.bagozi.ada.tutorial.firebase;

import android.util.Log;

import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

public class FirebaseNotificationService extends FirebaseMessagingService {

    private static final String TAG = FirebaseNotificationService.class.getSimpleName();

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        // TODO(developer): Handle FCM messages here.
        Log.e(TAG, "From: " + remoteMessage.getFrom());

        // Check if message contains a data payload.
        if (remoteMessage.getData().size() > 0) {
            Log.e(TAG, "Message data payload: " + remoteMessage.getData());
        }

        // Check if message contains a notification payload.
        if (remoteMessage.getNotification() != null) {
            Log.e(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.
    }


}

Android Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="it.bagozi.ada.tutorial.firebase">

    <service android:name=".FirebaseNotificationService">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Upvotes: 1

Views: 1319

Answers (1)

Uli
Uli

Reputation: 3016

Make the service declaration in your manifest a child of the "application" tag!

Upvotes: 7

Related Questions