Confirmed users
93
edits
| Line 97: | Line 97: | ||
var emailEndpoint, imEndpoint; | var emailEndpoint, imEndpoint; | ||
// | function setupAppRegistrations() { | ||
// | // (re)issue register() calls | ||
// to register to listen for a notification, | |||
// you simply call push.register | |||
var reqEmail = navigator.pushNotifications.register(); | |||
reqEmail.onsuccess = function(e) { | |||
emailEndpoint = e.result.pushEndpoint; | |||
storeOnAppServer("email", emailEndpoint); | |||
} | |||
var reqIm = navigator.pushNotifications.register(); | |||
reqIm.onsuccess = function(e) { | |||
imEndpoint = e.result.pushEndpoint; | |||
storeOnAppServer("im", imEndpoint); | |||
} | } | ||
} | } | ||
// all notifications will be sent to the system message handler. | |||
// which is up to the U/A to implement | |||
navigator.mozSetMessageHandler('push', { | navigator.mozSetMessageHandler('push', { | ||
handleMessage: function(e) { | handleMessage: function(e) { | ||
e.target.pushEndpoint == | if (e.target.pushEndpoint == emailEndpoint) | ||
e.target. | getNewEmailMessagesFromAppServer(e.target.version); | ||
else if (e.target.pushEndpoint == imEndpoint) | |||
getNewChatMessagesFromAppServer(e.target.version); | |||
} | } | ||
}); | }); | ||
AppFramework.addEventListener('user-login', function() { | |||
setupAppRegistrations(); | |||
}); | |||
// to unregister, you simply call.. | // to unregister, you simply call.. | ||
AppFramework.addEventListener('user-logout', function() { | |||
AppFramework.addEventListener('logout', function() { | |||
navigator.pushNotifications.unregister(emailEndpoint); | navigator.pushNotifications.unregister(emailEndpoint); | ||
navigator.pushNotifications.unregister(imEndpoint); | navigator.pushNotifications.unregister(imEndpoint); | ||
}); | }); | ||
// error recovery mechanism | |||
// will be called very rarely, but application | |||
// should register again when it is called | |||
navigator.mozSetMessageHandler('push-register', { | |||
handleMessage: function(e) { | |||
if (AppFramework.isUserLoggedIn) { | |||
setupAppRegistrations(); | |||
} | |||
else { | |||
// since user isn't logged in, when he logs in | |||
// registrations will be setup due to event handler | |||
} | |||
} | |||
}); | |||
</pre> | </pre> | ||