Receive GCM push notification in node.js app

后端 未结 5 1358
慢半拍i
慢半拍i 2021-01-02 12:22

I\'m building a command-line application in node.js and would like to receive GCM push notifications (the command-line app will be interacting with the same set of services

相关标签:
5条回答
  • 2021-01-02 12:45

    I don't think it possible (in a simple way)...

    Android/iOS has an OS behind with a service that communicates with GCM...

    If you are trying to run a CLI tool, you'll need to implement a service on top of the OS (Linux, Windows Mac) so it can receive notifications.

    0 讨论(0)
  • 2021-01-02 12:45

    GCM sends the notifications against the device tokens which are generated from iOS/Android devices when they are registered with push notification servers. If you are thinking of receiving the notifications without devices tokens it is fundamentally incorrect.

    0 讨论(0)
  • 2021-01-02 12:48

    It's not mandatory to depend only on GCM, today there are many packages are available for sending pushNotification.

    Two node packages are listed below.

    • fcm-call - you can find documentation from https://www.npmjs.com/package/fcm-call
    • fcm-node

    fcm-call is used - you can find documentation from 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);
    

    And Your notification will be sent within 2-3 seconds.

    Happy Notification.

    0 讨论(0)
  • 2021-01-02 13:02

    i think if you have to send push notification ,to ios and andriod then fcm is better then gcm use this

    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});
            }
        });
    });
    
    0 讨论(0)
  • 2021-01-02 13:05

    I believe you can using service workers.

    Push is based on service workers because service workers operate in the background. This means the only time code is run for a push notification (in other words, the only time the battery is used) is when the user interacts with a notification by clicking it or closing it. If you're not familiar with them, check out the service worker introduction. We will use service worker code in later sections when we show you how to implement pushes and notifications.

    So basically there is a background service that waits for push and thats what you are going to build.

    Two technologies

    Push and notification use different, but complementary, APIs: push is invoked when a server supplies information to a service worker; a notification is the action of a service worker or web page script showing information to a user.

    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

    0 讨论(0)
提交回复
热议问题