Home » Android » Does Firebase Cloud Messaging work well on unreliable networks?

Does Firebase Cloud Messaging work well on unreliable networks?

Posted by: admin November 1, 2017 Leave a comment

Questions:

I am currently using Firebase to send alerts from my server to android devices. It works when the user just connects to some networks for the first time and then stops working after. Until the user disconnects and reconnects again. Sometimes it doesn’t. At other times, some devices receive the notifications and some don’t.

Server Code

$url = 'https://fcm.googleapis.com/fcm/send';
$restKey = '';
$target_device = $token_array;  // using object Id of target Installation.
$push_payload = json_encode(array(
        "registration_ids" => $target_device,
        "priority" => 'high',
        "data" => array(
                "type" => $type,
                "is_background" => false
        )
));
$rest = curl_init();
curl_setopt($rest,CURLOPT_URL,$url);
curl_setopt($rest,CURLOPT_RETURNTRANSFER, true);
curl_setopt($rest,CURLOPT_POST,1);
curl_setopt($rest,CURLOPT_SSL_VERIFYPEER, false );
curl_setopt($rest,CURLOPT_POSTFIELDS,$push_payload);
curl_setopt($rest,CURLOPT_HTTPHEADER,
        array("Authorization:key=" . $restKey,
                "Content-Type: application/json"));
$response = curl_exec($rest);

Android Code

    if ("request_response".equals(type)) {

        sendNotification("You have a response to your request", "Response Available", HomeActivity.class);

                    if (!BaseApp.isActivityVisible()){

                        sendNotification("You have a response to your request", "Response Available", HomeActivity.class);

                    } else {
    //process data
    }

Answers: