Skip to content

Instantly share code, notes, and snippets.

@nkcmr
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nkcmr/81e37636d6b0a55788f1 to your computer and use it in GitHub Desktop.
Save nkcmr/81e37636d6b0a55788f1 to your computer and use it in GitHub Desktop.
push api zumba blog
// the service worker api entry point is navigator.serviceWorker
navigator.serviceWorker
.getRegistration()
.then(function (registration) {
// 'registration' is an instance of ServiceWorkerRegistration, see here:
// http://www.w3.org/TR/service-workers/#service-worker-registration-interface
if (!registration) {
// there is no service worker installed!
} else {
// there is one installed, and it is either 'waiting'
// or 'active', so no need to re-register
return registration
}
})
curl -X "POST" "https://android.googleapis.com/gcm/send" \
-H "Authorization: key=[INSERT_PRIVATE_SERVER_KEY_HERE]" \
-H "Content-Type: application/json" \
-d "{\"registration_ids\":[\"USERS_SUBSCRIPTION_ID\"],\"data\":{\"foo\":\"bar\"}}"
{
"name": "My Pushy App",
"short_name": "Pushy",
"icons": [{
"src": "/img/apple-touch-icon-144x144-precomposed.png",
"sizes": "144x144",
"type": "image/png"
}],
"start_url": "/",
"display": "standalone",
"gcm_sender_id": [application_project_number],
"gcm_user_visible_only": true // very important
}
// assuming 'registration' is an install service worker registration
registration.pushManager
.getSubscription()
.then(function (pushSubscription) {
if (!pushSubscription) {
return registration.pushManager.subscribe();
} else {
return pushSubscription;
}
})
.then(function (pushSubscription) {
// yay! a push subscription
})
.catch(function () {
// hmm, something went wrong
})
navigator.serviceWorker
.register('/path/to/service-worker.js')
.then(function (registration) {
// another 'ServiceWorkerRegistration' object
})
'use strict';
self.onpush = function receivePush (event) {
event.waitUntil(
self.registration.showNotification(event.data.myNotificationTitle, {
body: event.data.myNotificationMessage,
tag: event.data.myNotificationTag
})
)
}
var reg;
navigator.serviceWorker
.getRegistration()
.then(function (registration) {
if (!registration) {
return navigator.serviceWorker.register('/path/to/service-worker.js')
} else {
return registration
}
})
.then(function (registration) {
reg = registration;
return reg.pushManager.getSubscription()
})
.then(function (pushSubscription) {
if (!pushSubscription) {
return reg.pushManager.subscribe();
} else {
return pushSubscription;
}
})
.then(function (pushSubscription) {
// save the pushSubscription in your database for later use
return $http.post('/push/registration', pushSubscription)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment