Ok so i have looked everywhere and I can't find an answer to this. I have implemented the push notification in my Android app and everything works fine while the app is alive (Foreground or background), however if I close the app I stop receiving notifications. Here is the php code where I send the notification.
public static function sendNotification($token){
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array(
'notification' => array("title" => "Test","body" => "Test Message","icon" => "default","sound" => "default"),
"data" => "test message",
'to' => $token
);
$headers = array(
'Authorization:key = AIzaSyAKHM3MoMACjmeVK46TDg8-rTj1KoVjzWs',
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,true);
curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,0);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch,CURLOPT_POSTFIELDS,json_encode($fields));
$result = curl_exec($ch);
if($result === FALSE) {
throw new errorSendingNotification();
}
curl_close($ch);
// Result returns this
// {"multicast_id":8978533958735781479,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1468627796530714%7c0e4bee7c0e4bee"}]}
return $result;
}
I had the same problem. And this helped me.
Please remove "notification" key from payload and provide only "data" key.
Application handles notification messages only if the app is in foreground but it handles data messages even if the application is in background or closed.
If the application is in the foreground onMessageReceived handle both data and notification messages. If the application is closed or in the background only data messages are delivered to onMessageReceived . notification messages are not delivered to onMessageReceived . So you can't customize your messages.
It would be better to remove notification key from payload.