npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

cordova-plugin-service-worker-push

v1.0.0

Published

Push plugin for Cordova Service Worker

Downloads

7

Readme

Push for Cordova Service Worker

Enable your app to receive and handle push messages with a service worker. This plugin is built on top of the existing popular push plugin and matches the API laid out in this spec. If your server has new content, it can send a silent push to wake up your app (even when it is in the background) and update its conent accordingly using the persistent service worker. The next time a user opens your app, the content is ready to go.

Supported Platforms

  • iOS

Installation

To add this plugin to your project using cordova cli

cordova plugin add https://github.com/MobileChromeApps/cordova-plugin-service-worker-push.git

or, to install from npm:

cordova plugin add cordova-plugin-service-worker-push

To uninstall this plugin

cordova plugin rm cordova-plugin-service-worker-push

Setting up Push Notifications

After your service worker is ready, you can use the service worker's pushManager to subscribe for push notifications. subscribe will prompt the user, asking for permission to send push notifications. When the user agrees, subscribe will return a promise that resolves with a PushSubscription that contains a unique token which you will provide to your server for sending notifications to that device.

navigator.serviceWorker.ready.then(function (swReg) {
    swReg.pushManager.subscribe().then(function (pushSubscription) {
        myTokenToServerPostingFunction(pushSubscription.endpoint);
        /* or */
        myTokenToServerPostingFunction(pushSubscription.deviceToken);
    });
});

Note: To accommodate iOS's push API, the endpoint provided in the PushSubscription is simply the APNS device token. For convenience and code clarity, this implementation includes a non-spec property deviceToken for PushSubscription which has the same value as endpoint.

Handling Push Events

When a device receives a push message a push event is dispatched in the service worker context. You can set an handler for this event by setting the service worker's onpush.

// In your service worker script
this.onpush = function (event) {
    event.waitUntil(new Promise(function (resolve, reject) {
        // Do some async update process here
        myAsyncFunction(event.data.text());
        ...
    }));
};

The event object received by the onpush handler has a data property and a non-spec APNSData property. data contains the bytes provided in the data property of a received push message's payload. These bytes can be accessed as an ArrayBuffer, Blob, JSON, or text using the data.arrayBuffer(), data.blob(), data.json(), and data.text() functions respectively.

For convenience, event.APNSData is a JSON object containing the entire APNS payload that was received. APNSData does not require any specific formatting or property names on the server side, however it is not part of the spec.

Sending a Silent Push to Service Worker

Using the following payload template, you can send messages to your service worker app while it is in the background without creating an alert that notifies the user.

{
    "aps" : {
	"content-available" : 1,
	"priority" : 5
    },
    "data" : "{\"data0\":\"myData0\", \"data1\":\"myData1\"}"
}

Replace the contents of data with your own data. This data will be provided within the event object given to the push event handler in your service worker script.

1.0.0 (April 29, 2015)

  • Initial release