Nodejs Firebase云消息传递插件node-fcm的使用

Nodejs Firebase云消息传递插件node-fcm的使用
node-fcm 是一个用于 Node.js 应用程序与 Firebase Cloud Messaging (FCM) 交互的库。通过这个库,你可以轻松地向 Android、iOS 和 Web 客户端发送通知。下面是如何安装和使用 node-fcm 的基本步骤。

1. 安装 node-fcm

首先,你需要在你的 Node.js 项目中安装 node-fcm。可以通过 npm 安装:

npm install node-fcm --save

2. 初始化 node-fcm

安装完成后,你需要创建一个 FCM 实例,并提供你的服务器密钥(可以从 Firebase 控制台获取):

const FCM = require('fcm-node');
const serverKey = 'YOUR_SERVER_KEY_HERE'; // 获取自Firebase控制台
const fcm = new FCM(serverKey);

3. 发送消息

接下来,你可以使用 fcm.send() 方法来发送消息。以下是一个简单的示例,展示了如何向单个设备发送消息:

// 目标设备的注册令牌
const message = {
    to: 'DEVICE_REGISTRATION_TOKEN',
    notification: {
        title: 'Hello!',
        body: 'This is a notification from your Node.js app using Firebase Cloud Messaging!'
    },
    data: {  // 可选的数据字段
        my_key: 'my value',
        my_another_key: 'my another value'
    }
};

fcm.send(message, function(err, response){
    if (err) {
        console.log("Something has gone wrong!", err);
    } else {
        console.log("Successfully sent with response: ", response);
    }
});

注意:DEVICE_REGISTRATION_TOKEN 需要替换为实际的设备注册令牌。

4. 向多个设备发送消息

如果你想向多个设备发送消息,可以使用 registration_ids 字段替换 to 字段:

const tokens = [
    'DEVICE_REGISTRATION_TOKEN_1',
    'DEVICE_REGISTRATION_TOKEN_2',
    // 更多设备...
];

const message = {
    registration_ids: tokens,
    notification: {
        title: 'Hello!',
        body: 'This is a notification sent to multiple devices!'
    }
};

fcm.send(message, function(err, response){
    if (err) {
        console.log("Something has gone wrong!", err);
    } else {
        console.log("Successfully sent with response: ", response);
    }
});

5. 使用数据消息

除了通知消息外,你还可以发送纯数据消息,这些消息不会自动显示通知,而是作为应用程序的事件处理:

const message = {
    to: 'DEVICE_REGISTRATION_TOKEN',
    data: {
        my_key: 'my value',
        my_another_key: 'my another value'
    }
};

fcm.send(message, function(err, response){
    if (err) {
        console.log("Something has gone wrong!", err);
    } else {
        console.log("Successfully sent with response: ", response);
    }
});

确保在客户端应用中正确处理接收到的数据消息。

以上就是使用 node-fcm 库的基本方法。希望这对你有所帮助!


3 回复

当然,没问题!Node-FCM 是一个用于 Node.js 的 Firebase Cloud Messaging (FCM) 客户端库。它可以帮助你向 Android、iOS 和 Web 应用程序发送消息。

首先,你需要安装这个库,可以通过 npm 来安装:

npm install node-fcm

然后你可以创建一个新的 FCM 服务实例,并配置你的服务器 API 密钥:

var FCM = require('fcm-node');
var serverKey = 'YOUR_SERVER_KEY_HERE'; // 这是你从 Firebase 控制台获取的服务器密钥
var fcm = new FCM(serverKey);

接下来,你可以构建你要发送的消息内容,例如:

var message = {
    to: 'DEVICE_REGISTRATION_TOKEN', 
    collapse_key: 'demo-collapse-key',
    data: {
        title: 'Hello World!',
        body: 'This is a test message!'
    }
};

最后,发送消息:

fcm.send(message, function(err, response){
    if (err) {
        console.log("Something has gone wrong!");
    } else {
        console.log("Successfully sent with response: ", response);
    }
});

确保替换 DEVICE_REGISTRATION_TOKEN 为实际设备的注册令牌。希望这能帮到你!如果你有任何其他问题或需要进一步的帮助,随时告诉我!


node-fcm 是一个用于 Node.js 应用程序与 Firebase Cloud Messaging (FCM) 交互的库。它可以帮助你向 Android 和 iOS 设备发送消息。下面是如何安装和使用 node-fcm 的步骤:

安装

首先,你需要通过 npm 安装 node-fcm

npm install node-fcm --save

使用

  1. 初始化 FCM

你需要从 Firebase 控制台下载你的 serverKey.json 文件,并将其路径作为参数传入到 FCM 对象中。

  1. 创建消息

你可以设置消息的标题、内容以及其他属性。

  1. 发送消息

你可以指定一个或多个设备来接收消息。

以下是一个简单的例子:

const FCM = require('fcm-node');
const serverKey = require('./path/to/your/server-key.json'); // 替换为你的服务密钥文件路径
const fcm = new FCM(serverKey);

// 标题、内容、数据字段等
const message = {
    to: 'device_token',  // 替换为设备的注册令牌
    notification: {
        title: 'Hello, World!',  // 消息标题
        body: 'This is a test message'  // 消息内容
    },
    data: {  // 自定义数据字段
        message: 'This is a custom message object passed to payload'
    }
};

// 发送消息
fcm.send(message, function(err, response){
    if (err) {
        console.log("Something has gone wrong!");
    } else {
        console.log("Successfully sent with response: ", response);
    }
});

注意事项

  • 确保你的设备已经在 Firebase 控制台注册,并获取了注册令牌(device_token)。
  • 在实际应用中,你可能需要处理错误和重试机制。
  • 如果你想给多个设备发送消息,可以将 to 改为 registration_tokens 并传入一个包含多个设备令牌的数组。

以上就是使用 node-fcm 发送消息的基本流程。希望对你有所帮助!

node-fcm 是一个用于 Node.js 的 Firebase Cloud Messaging (FCM) 的库。使用时,首先安装 node-fcm

npm install fcm-node

然后可以创建一个 FCM 服务实例,并发送消息:

const FCM = require('fcm-node');
let serverKey = '你的Firebase服务器密钥';
let fcm = new FCM(serverKey);

let message = {
    to: '接收者的注册令牌',
    collapse_key: 'collapse_key',
    data: {
        title: '通知标题',
        body: '通知内容'
    }
};

fcm.send(message, function(err, response){
    if (err) {
        console.log("发生错误", err);
    } else {
        console.log("发送成功", response);
    }
});

确保替换 '你的Firebase服务器密钥''接收者的注册令牌' 为实际值。

回到顶部