Created
November 5, 2013 21:06
-
-
Save okiess/7326320 to your computer and use it in GitHub Desktop.
App Service Backend - Safari Push Notification Setup
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
<html> | |
<head> | |
<meta http-equiv="Content-type" content="text/html; charset=utf-8"> | |
<title>Safari Website Push</title> | |
<script src="https://service.apphoshies.com/javascripts/jquery.js" type="text/javascript"></script> | |
<script src="https://service.apphoshies.com/javascripts/apphoshies.js" type="text/javascript"></script> | |
<!-- https://developer.apple.com/library/mac/documentation/NetworkingInternet/Conceptual/NotificationProgrammingGuideForWebsites/PushNotifications/PushNotifications.html#//apple_ref/doc/uid/TP40013225-CH3-SW7 --> | |
<script type="text/javascript"> | |
var apiKey = "YOUR API KEY"; // Fill in your App Service Backend API KEY | |
function askForPermission() { | |
// Ensure that the user can receive Safari Push Notifications. | |
if ('safari' in window && 'pushNotification' in window.safari) { | |
var permissionData = window.safari.pushNotification.permission('web.com.your.pushid'); // The Website Push ID. | |
checkRemotePermission(permissionData); | |
} | |
return false; | |
}; | |
var checkRemotePermission = function (permissionData) { | |
if (permissionData.permission === 'default') { | |
// This is a new web service URL and its validity is unknown. | |
window.safari.pushNotification.requestPermission( | |
'https://service.apphoshies.com', // The web service URL. | |
'web.com.your.pushid', // The Website Push ID. | |
{ api_key: apiKey }, // Your App Service Backend API KEY | |
checkRemotePermission // The callback function. | |
); | |
} else if (permissionData.permission === 'denied') { | |
// The user said no. | |
alert("Denied!"); | |
} else if (permissionData.permission === 'granted') { | |
// The web service URL is a valid push provider, and the user said yes. | |
// permissionData.deviceToken is now available to use. | |
// console.log("Device token: " + permissionData.deviceToken); | |
alert("Granted!"); | |
// Retrieve the universal "application_client_key" from the App Service Backend | |
// The device token can't be used directly to send messages. Always use the application_client_key! | |
AppHoshies.getApplicationClientKey(apiKey, permissionData.deviceToken, function(applicationClientKey) { | |
console.log("Application client key: " + applicationClientKey); | |
if (applicationClientKey != null) { | |
// Associate the currently logged in user with the application client key | |
// You can pass in any key/value pairs you need | |
AppHoshies.associate(apiKey, applicationClientKey, { email: "[email protected]" }, function(status) { | |
// success or failure | |
console.log("Status: " + status); | |
}); | |
// this is how the disassociation works... | |
/*AppHoshies.disassociate(apiKey, applicationClientKey, function(status) { | |
// success or failure | |
console.log("Status: " + status); | |
});*/ | |
} else { | |
console.log("No application_client_key found!"); | |
} | |
}); | |
} | |
}; | |
</script> | |
</head> | |
<body> | |
<a href="#" onclick="askForPermission();">Activate Website Push</a> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment