
Security News
The Changelog Podcast: Practical Steps to Stay Safe on npm
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.
cordova-plugin-service-worker-push
Advanced tools
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.
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
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.
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.
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.
FAQs
Push plugin for Cordova Service Worker
We found that cordova-plugin-service-worker-push demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.

Security News
Experts push back on new claims about AI-driven ransomware, warning that hype and sponsored research are distorting how the threat is understood.

Security News
Ruby's creator Matz assumes control of RubyGems and Bundler repositories while former maintainers agree to step back and transfer all rights to end the dispute.