【问题标题】:Receive GCM push notification in node.js app在 node.js 应用程序中接收 GCM 推送通知
【发布时间】:2017-04-27 15:44:09
【问题描述】:

我正在 node.js 中构建一个命令行应用程序并希望接收 GCM 推送通知(命令行应用程序将与 iOS/Android 应用程序使用的同一组服务进行交互,因此希望使用相同的通知服务)。

鉴于 GCM 可以在 iOS 上使用(因此不是特定于 Android 的),我希望它也可以在 node.js 中使用。

我看过很多关于发送从 node.js 推送通知的文章,但是一直没能找到关于在接收端使用 node.js 的任何内容。

【问题讨论】:

标签: node.js google-cloud-messaging


【解决方案1】:

我认为不可能(以简单的方式)......

Android/iOS 背后有一个操作系统,带有与 GCM 通信的服务......

如果您尝试运行 CLI 工具,则需要在操作系统(Linux、Windows Mac)上实现服务,以便接收通知。

【讨论】:

  • 谢谢,@Lucas Katayama。真的有一个内置的 GCM 服务存在于所有 iOS 设备上吗?我假设它不是来自 Apple,所以它必须仅在安装了带有 GCM sdk 的应用程序时才会出现。
  • 大概是这样的……我是一名Android开发者……对ios了解不多……但假设它们相似……我认为ios有服务它以类似 GCM 的方式与服务器通信......
【解决方案2】:

GCM 根据设备令牌发送通知,这些设备令牌是在 iOS/Android 设备向推送通知服务器注册时生成的。如果您想在没有设备令牌的情况下接收通知,那基本上是不正确的。

【讨论】:

  • 我并没有说我想在没有设备令牌的情况下这样做。 :) 我认为这将是必要过程的一部分。
【解决方案3】:

我认为如果你必须向 ios 和 andriod 发送推送通知,那么 fcm 比 gcm 使用这个更好

router.post('/pushmessage', function (req, res) {
    var serverKey = '';//put server key here
    var fcm = new FCM(serverKey);
    var token = "";// put token here which user you have to send push notification
    var message = {
        to: token,
        collapse_key: 'your_collapse_key',
        notification: {title: 'hello', body: 'test'},
        data: {my_key: 'my value', contents: "abcv/"}
    };
    fcm.send(message, function (err, response) {
        if (err) {
            res.json({status: 0, message: err});
        } else {
            res.json({status: 1, message: response});
        }
    });
});

【讨论】:

  • 您在这段代码中使用了哪个库或节点模块?
  • @KrishnaKarki var FCM = require('fcm-node');
【解决方案4】:

我相信您可以使用服务人员。

推送基于 Service Worker,因为 Service Worker 在后台运行。这意味着推送通知运行的唯一时间代码(换句话说,电池使用的唯一时间)是用户通过单击或关闭通知与通知进行交互时。如果您不熟悉它们,请查看 Service Worker 介绍。当我们向您展示如何实现推送和通知时,我们将在后面的部分中使用 Service Worker 代码。

所以基本上有一个等待推送的后台服务,这就是你要构建的。

两种技术

推送和通知使用不同但互补的 API:当服务器向服务工作者提供信息时调用推送;通知是服务工作者或网页脚本向用户显示信息的操作。

self.addEventListener('push', function(event) {
  const promiseChain = getData(event.data)
  .then(data => {
    return self.registration.getNotifications({tag: data.tag});
  })
  .then(notifications => {
    //Do something with the notifications.
  });
  event.waitUntil(promiseChain);
});

https://developers.google.com/web/fundamentals/engage-and-retain/push-notifications/handling-messages

【讨论】:

    【解决方案5】:

    不强制只依赖GCM,现在有很多包可以发送pushNotification

    下面列出了两个节点包。

    fcm-call 被使用 - 你可以从https://www.npmjs.com/package/fcm-node/找到文档

    let FCM = require('fcm-call');
    const serverKey = '<Your Server Key>'; 
    const referenceKey = '<Your reference key>'; //Device Key
    let title = '<Your notification title here.>';
    let message = '<Your message here>';
    
    FCM.FCM(serverKey, referenceKey, title, message);
    

    您的通知将在 2-3 秒内发送。

    快乐通知。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-27
      • 1970-01-01
      • 1970-01-01
      • 2015-01-11
      相关资源
      最近更新 更多