-1

I try to implement FCM in my app but I have a problem. Whenever I send the notification, instead of calling my custom code, it just show the default notification. What am I doing wrong?

Those are the services:

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

And this is my Java code:

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Log.d("MyFirebaseMessaging", "GotMessage");
        WSListener.sendEvent(this, "Push");
    }

}

And I see this in the logcat when I get the notification on the device:

I/SendBroadcastPermission: action:com.google.android.gms.gcm.ACTION_SCHEDULE, mPermissionType:0
I/SendBroadcastPermission: action:com.google.android.gms.gcm.ACTION_SCHEDULE, mPermissionType:0

This is how I send the notifications:

function send_gcm_notify($reg_id, $message, $url, $key) {
        $fields = array(

            'to'                   => $reg_id ,
            'priority'             => "high",
            "notification"         => array("body"=>$message),
            'data'                 => array( "url"=> $url)
        );

        $headers = array(
            'Content-Type: application/json',
            'Authorization: key=' . $key
        );

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, "https://fcm.googleapis.com/fcm/send");
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));

        $result = curl_exec($ch);
        if ($result === FALSE) {
            //die('Problem occurred: ' . curl_error($ch));
        }
        curl_close($ch);
        return $result;

    }

Of course I tested and the path to the service is correct.

Erez.info
  • 125
  • 1
  • 2
  • 11

1 Answers1

2

If you want your server's content with notification in both modes(Background and Foreground), You have to send payload from server inside 'data' only. For more you can refer here How to handle Android FCM Messages when application is in Background with data payload?

Ankit Mehta
  • 4,251
  • 4
  • 19
  • 27