Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement firebase cloud messaging in server side?

After migrating to Firebase, i tested sending notification by using the firebase console it works fine, but i need a daily notification on a specific time so instead of using the firebase console i use my former cron job to send notification daily. I changed https://android.googleapis.com/gcm/send to https://fcm.googleapis.com/fcm/send but my device doesn't receive any notification.

Is there any way to solve this? Or did I miss anything? my cron job is working fine for my devices that is still using GCM.

Here's my code

function sendNotificationFCM($apiKey, $registrationIDs, $messageText,$id) {


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

    $message = array(
            'registration_ids' => $registrationIDs,
            'data' => array(
                    "message" => $messageText,
                    "id" => $id,
            ),
    );


    $ch = curl_init();

    curl_setopt_array($ch, array(
            CURLOPT_URL => 'https://fcm.googleapis.com/fcm/send',
            CURLOPT_HTTPHEADER => $headers,
            CURLOPT_POST => true,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_POSTFIELDS => json_encode($message)
    ));

    $response = curl_exec($ch);
    curl_close($ch);

    return $response;
}
like image 594
natsumiyu Avatar asked May 26 '16 07:05

natsumiyu


People also ask

Where is the server key for Firebase Cloud Messaging?

In the Firebase console, open Settings > Service Accounts. Click Generate New Private Key, then confirm by clicking Generate Key. Securely store the JSON file containing the key.

How do I send data to Firebase Cloud Messaging?

Go to Firebase console — →Project Settings — →Cloud Messaging. To send the message select Body — →Raw — →JSON(application/json). You can send Notification Payload , Data Payload and even both using POSTMAN service.


2 Answers

I am not able to comment directly in the selected answer. Take in consideration that the json message changes according to what you want/need to receive in the recipient os (android/ios).

e.g for android to get the notifications when the app is in background you need to add the data json in your request.

For iOS the data key is not needed, but you must have the notification key along with isContentAvailble flag set to true and priority set to high.

In the selected answer as valid, you use registration_ids key should be only used when you are sending the notification to more than one device, maybe you should take a look to the topics notifications.

like image 80
vicco Avatar answered Sep 30 '22 09:09

vicco


Apart from changing the url to following:

https://fcm.googleapis.com/fcm/send

You also have to change the way you send request data:

 Content-Type:application/json
 Authorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA

 {
   "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...", // "to" replaces "registration_ids" of gcm in fcm
   "data" : {
   ...
  },
}

Check out this complete guide.

like image 42
uniruddh Avatar answered Sep 30 '22 11:09

uniruddh