8

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 that iOS/Android apps use, hence wanted to use the same notification service).

Given that GCM can be used on iOS (and thus is not Android-specific) I am hoping it can be used from node.js as well.

I've seen many articles about sending push notifications from node.js, but haven't been able to find anything about using node.js on the receiving end.

skb
  • 30,624
  • 33
  • 94
  • 146
  • Are you in control of the "source" application server? – Ryan Wheale Dec 21 '16 at 20:41
  • I had to do this too, turned out to be far far more complicated than I thought. This library turned out to be the key: https://github.com/MatthieuLemoine/push-receiver – Jpod Nov 26 '21 at 01:26

5 Answers5

13

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});
        }
    });
});
Shekhar Tyagi
  • 1,644
  • 13
  • 18
2

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.

Lucas Katayama
  • 4,445
  • 27
  • 34
  • Thanks, @Lucas Katayama. Is there really a built-in GCM service that exists on all iOS devices? I was assuming since it wasn't from Apple that it must come only when an app with GCM sdk is installed. – skb Dec 15 '16 at 15:05
  • It is probably something like... I'm a Android developer... don't know much about ios... But assuming that they are similar... I think ios have a service which communicates with a server in a way like GCM... – Lucas Katayama Dec 15 '16 at 15:07
2

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

flakerimi
  • 2,580
  • 3
  • 29
  • 49
1

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.

Gurdev Singh
  • 1,996
  • 13
  • 11
  • I didn't say I wanted to do it without device tokens. :) I assumed that would be part of the necessary process. – skb Dec 15 '16 at 14:48
-1

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 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.

Mayur
  • 4,345
  • 3
  • 26
  • 40