Tuesday, April 10, 2012

5 Cool Things you can do with HTML5 (p4)

The fourth part of this series of posts will cover Notifications. When you are a user of GMail and using Chrome, you probably are familiar with those little boxes that popup when you've got new mail. Those type of notifications you can create in your APEX application as well - using HTML5!
Alas, at this moment, these type of notifications only work in Chrome. In Firefox you can add this extension so it'll work in that browser as well. The other ones will follow - sooner or later...
You can also use this plugin that mimics this feature - to be safe on every platform, but that one will appear in your browser, so that's not a desktop notification.
But how do you create a real desktop notification?
As usual with HTML5, you just need a few lines of code. The code below is executed in a Dynamic Action when a button is pressed:

function RequestPermission (callback) {
window.webkitNotifications.requestPermission(callback);
}
function showNotification(){
if (window.webkitNotifications){
  if (window.webkitNotifications.checkPermission() > 0) {
  RequestPermission(showNotification);
  }
  else {
  window.webkitNotifications.createNotification(
  "#APP_IMAGES#HTML5.jpg", //Put link to your icon here
  "Notification",
  "Now you are notified!").show();
  }
 }
else
{ alert('Sorry, you have to switch to another browser to use notifications - or use the other button ;-)');
}
}
//Run:
showNotification();


Just like geoLocations, the browser has to ask the user for permission to use Desktop Notifications (for a domain). Once the permission has been granted you can call the "show()" method, containing an image, the title and the actual message. Easy and cool, huh!

And you can even get completely wild by creating notifications that contain HTML or adding event listeners to your notifications - for instance doing something when the notification is closed. See this for some more examples.

See the working APEX example on : http://apex.oracle.com/pls/apex/f?p=22115:NOTIFICATIONS.
Post a Comment