Welcome Guest ( Log In | Register )

Outline · [ Standard ] · Linear+

 Android fcm receive notification from backend(php)

views
     
TSivanfwc
post Aug 20 2017, 01:45 PM, updated 7y ago

Getting Started
**
Junior Member
94 posts

Joined: Jul 2007


Hi guys! I'm currently developing an android app. Right now i want to add this fcm notification feature on my android app. Which mean i want it to automatically receive notification from backend(php) when certain condition is met. Let say i create a column call total_credit , when i set the total_credit to 1.00, it will automatically send a notification to my android app to tell the user that remaining total_credit is 1.00. I never done this fcm notification before, so i would like to ask, is it possible to that? I hope that someone can teach me of how do i implement this. Much appreciated!
malleus
post Aug 20 2017, 01:57 PM

Look at all my stars!!
*******
Senior Member
2,096 posts

Joined: Dec 2011
1) You'll need to configure messaging accordingly in your android app
2) You'll need to set up the trigger mechanism in your backend
3) Your backend upon that trigger will call the FCM API to deliver the notification: https://firebase.google.com/docs/cloud-messaging/server

that's about the top level details on this.
donald88
post Aug 20 2017, 02:01 PM

Casual
***
Junior Member
357 posts

Joined: Sep 2008


https://code.tutsplus.com/tutorials/how-to-...roid--cms-25870
TSivanfwc
post Aug 20 2017, 02:32 PM

Getting Started
**
Junior Member
94 posts

Joined: Jul 2007


QUOTE(malleus @ Aug 20 2017, 01:57 PM)
1) You'll need to configure messaging accordingly in your android app
2) You'll need to set up the trigger mechanism in your backend
3) Your backend upon that trigger will call the FCM API to deliver the notification: https://firebase.google.com/docs/cloud-messaging/server

that's about the top level details on this.
*
QUOTE(donald88 @ Aug 20 2017, 02:01 PM)
actually now i can send notification from firebase console to my android app, so now i need to know how to configure the backend part
malleus
post Aug 20 2017, 04:05 PM

Look at all my stars!!
*******
Senior Member
2,096 posts

Joined: Dec 2011
QUOTE(ivanfwc @ Aug 20 2017, 02:32 PM)
actually now i can send notification from firebase console to my android app, so now i need to know how to configure the backend part
*
then this is what you need to read through: https://firebase.google.com/docs/cloud-messaging/server
Andrew Lim
post Aug 20 2017, 09:21 PM

Just keep on keeping on
*******
Senior Member
2,839 posts

Joined: Nov 2007


In general, if you want to send to only 1 device, you need to get the Firebase token while the app is running and save it to your server. Then your server will send that token to Firebase to push.

I have a table named "fcm" that maps users to device tokens:

Attached Image

This is the code I'm using to doing Firebase pushing. The "registration_ids" is a list of Firebase device tokens. I'm using Fat Free Framework for some of the lines that contain "$f3". Note that Firebase supports 3 types of push messages (notification, data, and both). You can read more about it here. The code below only uses "notification" type.

CODE
public static function fcmpush( $title, $text, $registration_ids ) {
   $errors = [];
   $f3 = Base::instance();
   $data = [
       "registration_ids" => $registration_ids,
       "notification" => [
           "title" => $title,
           "text" => $text,
           "sound" => "default"
       ],
       "priority" => "high"
   ];

   $fcmserverkey = $f3->get('fcmserverkey');
   if ( empty($fcmserverkey) ) {
       $errors[] = 'Invalid fcmserverkey in config.';
       return $errors;
   }

   $data_string = json_encode( $data );
   $curl = curl_init();
   if(curl_errno($curl)){
       $errors[] = 'Curl error: ' . curl_error($curl);
       return $errors;
   }
   curl_setopt($curl, CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send');
   curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
   curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);
   curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
   curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
   curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
   curl_setopt($curl, CURLOPT_HTTPHEADER, array(
       "Content-Type:application/json",
       'Authorization:key=' . $fcmserverkey,

   ));
   if(curl_errno($curl)){
       $errors[] = 'Curl error: ' . curl_error($curl);
       return $errors;
   }

   // curl_exec can crash for some reason, probably something to with SSL
   $result = curl_exec($curl);
   if (!$result) {
       $errors[] = 'fcmpush: curl_exec returned ' . $result;
   }
   curl_close($curl);
   return $errors;
}


This post has been edited by Andrew Lim: Aug 20 2017, 09:24 PM

 

Change to:
| Lo-Fi Version
0.0147sec    0.56    6 queries    GZIP Disabled
Time is now: 28th March 2024 - 08:19 PM