在 Web 服务器上实现 Firebase 云消息传递 [英] Implementing Firebase Cloud Messaging on Web server

查看:34
本文介绍了在 Web 服务器上实现 Firebase 云消息传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一台运行网站的服务器.我需要此服务器能够管理(仅限下游)三个独立设备组(Android、iOS 和客户端 Web 应用)的通知.

I have a server running a website. I need this server to be able to administer (only downstream) notifications to three separate device groups, Android, iOS, and a client-side web app.

我正在尝试使用 firebase 云消息传递.使用FCM,我打算使用http协议发送json消息.

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

除此之外,我对去哪里很困惑.我知道 GCM 教程应该与 FCM 教程几乎完全相同,但是我很难找到一个教程来弄清楚我需要做什么,因为每个教程似乎都将服务器端和客户端应用程序混合在一起,这让我很困惑.

Aside from this, I am pretty confused about where to go. I know that GCM tutorials should pretty much be exactly the same as FCM tutorials, but I am having trouble finding a tutorial to figure out what I need to be doing, as each tutorial seems to mix up the server and client side applications together, which is confusing me.

我经历过

https://firebase.google.com/docs/cloud-messaging/服务器#选择

相当彻底,但它似乎掩盖了一些我还没有的必要知识.任何人都可以就如何以我正在寻找的方式实施 FCM 提出一个好的起点吗?我对 Web 开发人员来说总体上非常陌生,不到 2 个月,(使用 node、mongo 和 scss)并且对如何开始使用 FCM 感到有点不知所措.

pretty thoroughly but it seems to gloss over some requisite knowledge that I don't have yet. Can anyone suggest a good starting place on how to implement FCM in the manner I am looking for? I am overall very new to web dev, less than 2 months, (using node, mongo, and scss) and feel a bit overwhelmed on how to get started on FCM.

感谢你们提供的任何反馈.

I appreciate any feedback you guys can offer.

推荐答案

正如您所写的,您计划在 android、iOS 和客户端 Web 应用程序这三个平台上接收通知.您可以使用 firebase 来获取实时通知或所需的好的数据,但您必须牢记这一点,因为与彼此相比,每个人都有一些不同的过程来实现这一点.首先,您必须选择其中任何一个并开始实施.让我们从头开始...

as you have written that you are planning to get notifications on all three platforms that are android, iOS, and a client-side web app. You can use firebase to get real time notification or desired data that's good but you have to keep this in your mind that each one has a little different process to achieve this compared to each other. Very first you have to choose any of one and start the implementation. Let's start from the begin...

https://console.firebase.google 上使用 Firebase 到您的应用程序中以创建项目的常见过程.com/ .

创建项目后,您将能够看到将在您的应用程序中使用的 firebase 密钥.您可以在 https://console.firebase.google.com/上看到这一点项目/项目-[your_project_number]/概览.

After creating project you will be able to see the firebase key that will be used in your application. You can see this at https://console.firebase.google.com/project/project-[your_project_number]/overview.

我希望您已经这样做了,但不要忘记在 https://console.firebase.google.com/project/project-[your_project_number]/database/ruleshttps://console.firebase.google.com/project/project-[your_project_number]/storage/rules.

I hope you have done this already but don't forget to give permission to Database and Storage at https://console.firebase.google.com/project/project-[your_project_number]/database/rules and https://console.firebase.google.com/project/project-[your_project_number]/storage/rules.

对于数据库,只保留这个...

For database just leave this...

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

为了存储,就留下这个...

For storage just leave this...

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 .

It will define that you can use it publicly no need to of any verifaction at firebase end. If you want to add any kind of verification you can go through https://console.firebase.google.com/project/project-[your_project_number]/authentication/providers .

现在从 SERVER to WEB APPLICATION 通知开始.首先,我想让您知道 FCM(firebase 云消息传递)只能通过 Android、iOS 和网络(指定的 Google Chrome)实现.所以为了在所有浏览器上使用它,我们必须实现 firebase 数据库.在js端.你必须把你在概述中看到的东西放进去.

Now start with the SERVER to WEB APPLICATION notification. First of all I want to let you know FCM (firebase cloud messaging) can be implemented with Android, iOS and web(specified Google Chrome) only. So for using it on all browser we have to implement the firebase database. At js side. You have to put something like, what you have seen in Overview.

<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 可以帮助您.我通过使用 CodeIgniter 在 PHP 的服务器端应用 CURL 来实现它.我附上下面的代码.您可以在此处查看 CURL 示例 https://firebase.google.com/docs/database/rest/save-data#section-put

On server side for triggering an event or notification you can use CURL. As you have mentioned that you are using Node.js so this https://firebase.google.com/docs/server/setup#add_firebase_to_your_app can help you. I have achieved it by applying CURL at server side on PHP with CodeIgniter. I am attaching the code below. You can see the CURL example here 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;
   }
}

现在让我们看看如何从服务器端触发通知.

Now let's see how we can get the notification when it will be fired from server side.

JAVASCRIPT

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 改进它.

This will help you and figure out where you were confusing. Let me know if this will work for you, I will improve it for android and iOS also.

这篇关于在 Web 服务器上实现 Firebase 云消息传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆