Friday, 23 December 2016

FCM push notification example

FCM push notification example

Question: What is FCM Messages?
Full form of FCM is Firebase Cloud Messaging.
FCM is a cross-platform messaging solution that lets you reliably deliver messages to android and IOS devices at no cost.
you can notify a client app that new message has been arrived.


Question: How does FCM works?
App server interacts with FCM (HTTP or XMPP protocol) and a client app. You can compose and send messages.
FCM built on Firebase Cloud Messaging.


Question: What is FCM Server?
FCM servers take messages from an app server and send to cient app.
FCM connection servers provided by Google.


Question: What is App Server?
App Server sends data to a client app (via the chosen FCM connection server) using XMPP or HTTP protocol.



Question: What are role of App Server?
  • Communicate with your client
  • Send properly formatted requests to the FCM connection server
  • Securely store the Server key and client registration tokens



Question:How to send notification to android/IOS device with FCM server?
$jsonData='{
  "to": "dhkC5_jTnck:APA91bF7L_KN7GFOPMiVsykjMGqydlftItfapC4Va63hRBuhFCrRKMdUonqhO1qjKT5_OYPMRAZFMA2UDVA1spAw9SpCsLE7B05vGtNttvuykIdxL7jKWWvJkA51T98O8ZZkubhXwQyRx",
  "notification": {
    "body": "New Gr sent you a message",
    "title": "domain.com ",
    "icon": "main_ve_logo",
    "click_action": "com.domain.fcm"
  },
  "data": {
    "type": "message",    
    "avatar": "http://img.domain.com/upload/default.jpg",
    "message_id": 1
  }
}';
$curl = curl_init();
curl_setopt_array($curl, array(
    CURLOPT_URL => "https://fcm.googleapis.com/fcm/send",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "POST",
    CURLOPT_POSTFIELDS => json_encode($fields),
    CURLOPT_HTTPHEADER => array(
      "authorization: key=FCM_SERVER_KEY",
      "cache-control: no-cache",
      "content-type: application/json"
    ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);



2 comments :

Unknown said...

Where can i find the registration id, value of the "to" field??

taki khondoker said...

Thanks you for Post. it was very useful for me.keep sharing such ideas in the future as well. Thanks for giving me the useful information. I think I need it!
XMPP

Post a Comment