Deploying Firebase Cloud Messaging on a Web Server

I have a server running a website. I need this server in order to be able to administer (only downstream) notifications for three separate device groups: Android, iOS, and the web client application.

I am trying to use firebase cloud messaging. With FCM, I plan to use the http protocol to send json messages.

Also, I'm pretty confused about where to go. I know that GCM textbooks should pretty much be the same as in FCM textbooks, but it's hard for me to find a tutorial to figure out what I need to do, since each tutorial seems to mix server and client applications together, it confuses me.

I went through

https://firebase.google.com/docs/cloud-messaging/server#choose

quite carefully, but it seems to obscure some of the necessary knowledge that I do not have yet. Can anyone suggest a good starting place on how to implement FCM in the manner I'm looking for? I am generally very new to web developer, less than 2 months (using node, mongo and scss) and a bit overloaded with how to get started with FCM.

I appreciate any feedback you guys can offer.

+3
source share
1 answer

, , , iOS -. firebase , , , . , . ...

firebase https://console.firebase.google.com/.

firebase, . https://console.firebase.google.com/project/project-[your_project_number]/overview.

, , https://console.firebase.google.com/project/project-[your_project_number]/database/rules https://console.firebase.google.com/project/project-[your_project_number]/storage/rules.

...

{
  "rules": {
    ".read": true,
    ".write": true
  }
}

...

service firebase.storage {
  match /b/project-[your_project_number].appspot.com/o {
    match /{allPaths=**} {
      allow read, write;
    }
  }
}

, , firebase. - , https://console.firebase.google.com/project/project-[your_project_number]/authentication/providers.

SERVER to WEB APPLICATION. , , FCM ( ) Android, iOS - ( Google Chrome). firebase. js. - , .

<script src="https://www.gstatic.com/firebasejs/3.1.0/firebase.js"></script>
<script>
  // Initialize Firebase
  var config = {
    apiKey: "your_api_key",
    authDomain: "project-[your_project_number].firebaseapp.com",
    databaseURL: "https://project-[your_project_number].firebaseio.com",
    storageBucket: "project-[your_project_number].appspot.com",
  };
  firebase.initializeApp(config);
</script>

CURL. , Node.js, https://firebase.google.com/docs/server/setup#add_firebase_to_your_app . , CURL PHP CodeIgniter. . CURL https://firebase.google.com/docs/database/rest/save-data#section-put

<?php 
if (!defined('BASEPATH')) exit('No direct script access allowed');

//PHP CURL FOR NOTIFICATION
function booking_notification($notif_arr, $notif_type)
{
    //GET CI 
    $CI =& get_instance();

    $url = 'put_your_firebase_database_url_here';
    $key = 'put_firebase_key';
    $notif_arr = {'Key':'values'};//THIS IS THE DATA WHAT YOU WILL NEED TO SHOW ON FRONT END TO NOTIFY
    $notif_type = 'notification'; //THIS IS THE NAME JSON WHICH WILL BE CREATED IF NOT EXIST AT FIREBASE DATABASE
    $headers = array(
       'Authorization: key=' . $key,
       'Content-Type: application/json'
   );

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url."/".$notif_type.".json");
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

   // Disabling SSL Certificate support temporarly
   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

   curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($notif_arr));

   // Execute post
   $result = curl_exec($ch);

   if ($result === FALSE) {
       die('Curl failed: ' . curl_error($ch));
       // Close connection
       curl_close($ch);

       return FALSE;
   }
   else
   {
       // Close connection
       curl_close($ch);
       return TRUE;
   }
}

, , .

JAVASCRIPT

var fireBaseJSONref = firebase.database().ref().child("notification"); //BY THIS YOU CAN GET THE JSON OF FIREBASE DATABASE
fireBaseJSONref.on('child_added', function(snapshot) {
        if (!ignoreItems) {
            console.log(snapshot.val());//THIS WILL PRINT THE DATA WHAT YOU HAVE TRIGGERD FROM SERVER
        }
});
/* WHEN FIRST TIME ANY DATA SENT TO DATABASE OF FIREBASE AFTER PAGE LOAD */


fireBaseJSONref.once('value', function(snapshot) {ignoreItems = false}); //THIS WILL HELP YOU TO NOT TO CALL FOR PREVIOUSLY ADDED ITEM IN FIREBASE DATABASE. WHEN NEW DATA WILL BE ONLY THEN IT WILL CALL.

, . , , Android iOS.

+7

All Articles