Shreya
Shreya

Reputation: 11

Android-Firebase Push Notification when app is in background

I am developing Firebase push notification using Server API call in Android application.it works perfectly when application is in foreground but when application is not in foreground i am not able to get the push notification.

I am sending JSON data in which header contains Server API key and content type and the value contains data which have body as array. Appreciate any help.

PHP code:

 $url = 'https://fcm.googleapis.com/fcm/send';
 $fields =array('registration_ids' => $tokens,'data' => $message); 
 $headers = array('Authorization:key = value','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);?>

Upvotes: 0

Views: 2641

Answers (2)

Efra Espada
Efra Espada

Reputation: 19

Look at my code. Maybe it will help you:

$token = getTokenDevice($dn);

  $path_to_firebase_cm = 'https://fcm.googleapis.com/fcm/send';

  $fields = array(
      'to' => $token,
      'priority' => 'normal',
      'content_available' => true,
      'data' => array('type' => $type, 'title' => $title, 'body' => $message)
  );

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

  curl_setopt($ch, CURLOPT_URL, $path_to_firebase_cm);
  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_IPRESOLVE, CURL_IPRESOLVE_V4 );
  curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));

  $result = curl_exec($ch);

  curl_close($ch);

I use content_available for iOS devices. That code works for me in Android and iOS when apps are in background.

So you should check your FirebaseMessaging service. Mine looks like this:

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    Auth auth = new Auth(getApplicationContext());

    Map<String, String> data = remoteMessage.getData();

    if (auth.isNotificationEnabled(data.get("type"))) {
        Log.e(TAG, "data: " + remoteMessage.getData());
        sendNotification(data.get("title"), data.get("body"));
    }
}

Upvotes: 1

P Wilson
P Wilson

Reputation: 41

I too felt this was happening in the beginning but then discovered that my Android App was indeed waking up. Unfortunately it was not waking up enough to do what I wanted (send a heartbeat REST message to my Server). So I will answer your question with a question - why did you decide you did not get a push notification ? For me, when I logged messages from within the onMessageReceived ....

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {  
    Log.d(TAG, "From: " + remoteMessage.getFrom());
    Log.d(TAG, "Notification Message Body: " + remoteMessage.getData());
 }

It proved the App got woken up each time the Fire Cloud Messaging service sent a message, regardless if the App was in foreground or background.

Other mistakes, I made initially I shall share with you:

I had to make some changes to my Server side to ensure that the Android App onMessageReceived() method was always called even in background mode. My messages were JSON and HTTP and Upstream. My server is written in C#.

  1. In Firebase there are 2 types of payload downstream messaging. (a) Notification and (b) Data. The type depicts how the App behave when receiving messages depend on whether the app is in background or the foreground. Hence I needed (b) Data type payload to meet my requirement. Just add the “data”

  2. I then had to make my messages High Priority rather than normal. I did a load of tests with both normal and high. Normal did not work once the Android App went into Doze mode. I found this reference useful. https://developer.android.com/training/monitoring-device-state/doze-standby.html#support_for_other_use_cases

Upvotes: 0

Related Questions