Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FCM push notification not working when app is closed Android

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;
}
like image 524
Alan Ortega Avatar asked Jul 16 '16 00:07

Alan Ortega


People also ask

Do push notifications work when app is open?

App publishers can send them at any time, since users don't have to be in the app or using their devices to receive them. Push notifications look like SMS text messages and mobile alerts, but they only reach users who have installed your app.

Do local notifications work when app is closed?

A local notification is schedule at a given time. The notification only happens if the app is open or in the foreground. If the app is closed the notification does not happen (no notification card).

Why am I not receiving push notifications on my Android?

Do Not Disturb or Airplane Mode is on. Either system or app notifications are disabled. Power or data settings are preventing apps from retrieving notification alerts. Outdated apps or OS software can cause apps to freeze or crash and not deliver notifications.

Why might push notification stop working?

One of the main reasons why your phone's notifications aren't working could be due to broken app updates. If your Android device is not getting notifications from one app in particular, it's possible that the developers have accidentally rolled out a buggy update.


1 Answers

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.

like image 121
AmAnDroid Avatar answered Oct 27 '22 17:10

AmAnDroid