Firebase onMessageReceived not called when app in foreground

8,220 views
Skip to first unread message

Livin Lawrence

unread,
Sep 11, 2016, 11:14:00 AM9/11/16
to Firebase Google Group
I have read much documents regarding firebase push notification behaviour clearly. I'm able to see notification on the system tray when my app is in background but not triggering onMessageReceivedcall back when app is in foreground. I can see the logs that the message is received on my app. I have tried calling with data payload and without data payload from app server. My app server is using php script to trigger the fcm apis. Following is the php script I'm using
 
  $fields = array(
            'registration_ids' => $registatoin_ids,
     'notification' => array( "title"=>$title, "body" => $notification_message ,"click_action"  => $click_action,'color' => "#74BCEE",'icon' => "school_logo"));
      $headers = array(
            'Authorization: key='xxxxxxxxx',
            'Content-Type: application/json'
        );

        // Open connection
        $ch = curl_init();

        //echo "here";exit;
        // Set the url, number of POST vars, POST data
        curl_setopt($ch, CURLOPT_URL, $url);

        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        // Disabling SSL Certificate support temporarly
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));

        // Execute post
        $result = curl_exec($ch);
        if ($result === FALSE) {
            //die('Curl failed: ' . curl_error($ch));
            curl_close($ch);
            return;

        }
        print_r($result);

Following are the services i'm using in my Android app

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

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

Eric Fung

unread,
Sep 11, 2016, 8:47:18 PM9/11/16
to Firebase Google Group

It looks like there might be a typo in the names of your services in the manifest. If `service` is a package under your main application package, you'll need the leading dot when naming the services. For example:

    android:name=".service.MyFirebaseMessagingService"
    android:name=".service.MyFirebaseInstanceIdService"

Can you try changing that and see if that helps?

Júlio Cesar Bueno Cotta

unread,
Sep 11, 2016, 8:47:18 PM9/11/16
to Firebase Google Group
As the docs state, you should use "data" body to this callback to be called. I send the same "notification" content in the "data" body and it works. 

Eric Fung

unread,
Sep 22, 2016, 11:27:00 AM9/22/16
to Firebase Google Group
If your app is in the foreground though, you should always get a call to onMessageReceived() whether you send a notification or data message. See the table in the "Handling Messages" section in https://firebase.google.com/docs/cloud-messaging/android/receive
Reply all
Reply to author
Forward
0 new messages