Last active
August 29, 2015 13:58
-
-
Save sebastienblanc/9970129 to your computer and use it in GitHub Desktop.
This file contains 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
(function() { | |
var mailEndpoint, mailRequest, newsEndpoint, newsRequest, SPClient, UPClient; | |
// config params for UnifiedPush server | |
var variantId = "5ff0de96-0f50-4e1f-8e35-902ca843beba"; | |
var variantSecret = "4d84742a-1e00-42dd-9973-7b72391b2c8b"; | |
var simplePushUrl = "https://hackergartenups-sblanc.rhcloud.com:8443/simplepush"; | |
var unifiedPushUrl = "https://hackergartenups-sblanc.rhcloud.com"; | |
// create the 'UnifiedPush' client object: | |
UPClient = AeroGear.UnifiedPushClient(variantId, variantSecret, unifiedPushUrl + "/rest/registry/device"); | |
// onConnect callback function called when SimplePush | |
// successfully establishes connection to the server | |
function spConnect() { | |
appendTextArea("Connection established with SimplePush server!"); | |
// use 'PushManager' to request a new PushServer URL endpoint for 'Mail' notifications: | |
mailRequest = navigator.push.register(); | |
// the DOMRequest returns 'successfully': | |
mailRequest.onsuccess = function( event ) { | |
// extract the endpoint object from the event: | |
mailEndpoint = event.target.result; | |
appendTextArea("Subscribed to 'Mail' messages on channel " + mailEndpoint.channelID); | |
// if it is the first registration, need to register | |
// the 'pushEndpoint' with the UnifiedPush server. | |
if ( mailEndpoint.pushEndpoint ) { | |
// assemble the metadata for registration with the UnifiedPush server | |
var metadata = { | |
deviceToken: mailEndpoint.channelID, | |
simplePushEndpoint: mailEndpoint.pushEndpoint, | |
alias: "john", | |
categories: ["mail"] | |
}; | |
var settings = { | |
success: function() { | |
localStorage.setItem(mailEndpoint.channelID,"mail"); | |
appendTextArea("Registered 'Mail' endpoint with UnifiedPush server!"); | |
}, | |
error: function() { | |
appendTextArea("Error when registering with UnifiedPush server!"); | |
} | |
}; | |
settings.metadata = metadata; | |
// register with the server | |
UPClient.registerWithPushServer(settings); | |
} else { | |
appendTextArea("'Mail' was already registered!"); | |
} | |
}; | |
// use 'PushManager' to request a new PushServer URL endpoint for 'Mail' notifications: | |
newsRequest = navigator.push.register(); | |
// the DOMRequest returns 'successfully': | |
newsRequest.onsuccess = function( event ) { | |
// extract the endpoint object from the event: | |
newsEndpoint = event.target.result; | |
appendTextArea("Subscribed to 'News' messages on channel " + newsEndpoint.channelID); | |
// if it is the first registration, need to register | |
// the 'pushEndpoint' with the UnifiedPush server. | |
if ( newsEndpoint.pushEndpoint ) { | |
// assemble the metadata for registration with the UnifiedPush server | |
var metadata = { | |
deviceToken: newsEndpoint.channelID, | |
simplePushEndpoint: newsEndpoint.pushEndpoint, | |
alias: "john", | |
categories: ["news"] | |
}; | |
var settings = { | |
success: function() { | |
localStorage.setItem(newsEndpoint.channelID,"news"); | |
appendTextArea("Registered 'News' endpoint with UnifiedPush server!"); | |
}, | |
error: function() { | |
appendTextArea("Error when registering with UnifiedPush server!"); | |
} | |
}; | |
settings.metadata = metadata; | |
// register with the server | |
UPClient.registerWithPushServer(settings); | |
} else { | |
appendTextArea("'News' was already registered!"); | |
} | |
}; | |
// set the notification handler: | |
navigator.setMessageHandler( "push", function( message ) { | |
var service = localStorage.getItem(message.channelID); | |
if ( service === "mail" ) { | |
// let's react on the 'mail' endpoint | |
appendTextArea("Mail Notification - " + message.version); | |
} | |
if ( service === "news" ) { | |
// let's react on the 'mail' endpoint | |
appendTextArea("News Notification - " + message.version); | |
} | |
}); | |
} | |
function appendTextArea(newData) { | |
var el = getTextAreaElement(); | |
el.value = el.value + '\n' + newData; | |
} | |
function getTextAreaElement() { | |
return document.getElementById('responseText'); | |
} | |
// custom..... | |
$("#reconnect").on("click", function(event) { | |
// AeroGear add-on to allow a reconnect, if the WebSocket/SockJS connection is lost | |
navigator.push.reconnect(); | |
}); | |
// onClose callback function: | |
function spClose() { | |
$("#reconnect").show(); | |
appendTextArea("\nConnection Lost!\n"); | |
} | |
SPClient = AeroGear.SimplePushClient({ | |
simplePushServerURL: simplePushUrl, | |
onConnect: spConnect, | |
onClose: spClose | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment