Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Receive GCM push notification in node.js app

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.

like image 832
skb Avatar asked Dec 12 '16 17:12

skb


4 Answers

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});
        }
    });
});
like image 65
Shekhar Tyagi Avatar answered Nov 12 '22 18:11

Shekhar Tyagi


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

like image 37
flakerimi Avatar answered Nov 12 '22 20:11

flakerimi


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.

like image 42
Lucas Katayama Avatar answered Nov 12 '22 20:11

Lucas Katayama


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.

like image 1
Gurdev Singh Avatar answered Nov 12 '22 18:11

Gurdev Singh