LvN
LvN

Reputation: 711

Firebase onMessageReceived not called when app in foreground

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

  $url = 'https://fcm.googleapis.com/fcm/send';
  $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>

Upvotes: 1

Views: 2866

Answers (1)

Daniel
Daniel

Reputation: 41

I had the same problem. My problem was that the <service></service> tags were outside the <application></application> tag in the manifest file. When I put in the <application></application> tag, the problem was solved.

Upvotes: 4

Related Questions