Receive GCM push notification in node.js app

后端 未结 5 1385
慢半拍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 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

提交回复
热议问题