Last active
August 29, 2015 14:20
-
-
Save nkcmr/81e37636d6b0a55788f1 to your computer and use it in GitHub Desktop.
push api zumba blog
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 | |
} | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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\"}}" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
navigator.serviceWorker | |
.register('/path/to/service-worker.js') | |
.then(function (registration) { | |
// another 'ServiceWorkerRegistration' object | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
self.onpush = function receivePush (event) { | |
event.waitUntil( | |
self.registration.showNotification(event.data.myNotificationTitle, { | |
body: event.data.myNotificationMessage, | |
tag: event.data.myNotificationTag | |
}) | |
) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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