Firebase Push Notifications

Firebase push notification android ios implementation

https://firebase.google.com/docs/View the android ios front-end docking process, which is relatively simple

  • This article mainly explains the background implementation
    1. Firebase creates a project in the background, and obtains the relevant parameters and configurations as follows

    (1), click Project Overview -> Project Settings

    (2), obtain the server key in cloud messaging (recommended, of course, the old server key can also be used)
    write picture description here
    (3), service account -> click to generate a new private key , Obtain the private key configuration (json file, it can be regenerated if lost, and the old configuration needs to be replaced after regeneration)
    write picture description here
    (4), database url
    write picture description here
    2, server nodejs
    (1), node install firebase-tools tool

> #命令行: npm install -g firebase-tools --save (全局安装)
> #命令行: firebase login

可能提示错误: Authentication Error: Your credentials are no longer valid.(原因VPN)

解决办法:在firebase-tools文件夹下(全局安装路径(shift+cmd+g):/usr/local/lib/node_modules/firebase-tools/
找到文件 node_modules/firebase/node_modules/faye-websocket/lib/faye/websocket/client.js 
修改如下
var Client = function(_url, protocols, options) {
    options = options || {};
    // 添加proxy配置
    options.proxy = {
        origin:  'http://localhost:1087',
    };
}

> #重新命令行
export http_proxy=http://localhost:1087
export NODE_TLS_REJECT_UNAUTHORIZED=0
firebase login --no-localhost
> #命令行: 
export http_proxy=http://localhost:1087
export NODE_TLS_REJECT_UNAUTHORIZED=0
firebase login --no-localhost
前面两行不能少不然又会报错
> #命令行:
firebase deploy
回车就行

(2), js script

> #命令行:
node install firebase

书写脚本server.js(demo自行修改定义)
-------------------------

var firebase = require('firebase');
var request = require('request');

var API_KEY = '服务器密钥';(上述得到)

var token = '前段app打印';

firebase.initializeApp({
    serviceAccount: "私钥配置路径./xxxxx.json",
    databaseURL: "数据库url--https://xxxxx.firebaseio.com/"
});

var payload = {
    to: "/topics/前段主题key 或者 上面定义的token",
    notification: {
        body: "this is test body",
        title: "this this this"
    }
}

setTimeout(function (args) {
    sendNotificationToUser(payload);
}, 200);

function sendNotificationToUser(message, onSuccess) {
    request({
        url: 'https://fcm.googleapis.com/fcm/send',
        method: 'POST',
        headers: {
            'Content-Type' :'application/json',
            'Authorization': 'key='+API_KEY
        },
        body: JSON.stringify(message)
    }, function(error, response, body) {
        if (error) {
            console.log('---message send error--', error);
        }
        else if (response.statusCode >= 400) {
            console.log('HTTP Error: '+response.statusCode+' - '+response.statusMessage);
        }
        else {
            console.log('--message send success---')
            onSuccess && onSuccess();
        }
    });
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324880166&siteId=291194637