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.