Home > Uncategorized > Create Push-like Notifications with Javascript!

Create Push-like Notifications with Javascript!

Do you know the Iphone Push notifications?
Well you can use something like that IN your web page thanks to the Javascript API that came along with HTML5!
Its as simple as this!

window.webkitNotifications.createNotification(image_to_display, title, text).show()

Of course you will need to include some failture handling…since not all browsers have this implemented.(here goes my little script):

if (window.webkitNotifications){hasnotif="y";}else{hasnotif="n";}

function c_perm(id){
if (hasnotif=="y")//has notifications,ask for permision
{
	document.getElementById(id).addEventListener('click', function() {
	  window.webkitNotifications.requestPermission();
	}, false);

}  
else
{
	throw("Cant use Notifications.")
}	
}
//
//
function showNotification(title, text, img) {
  if (window.webkitNotifications.checkPermission() == 0) {
    // note the show()
    window.webkitNotifications.createNotification(img, title, text).show(); 
  } else {
    		throw("Failed to use Notifications!");
  }  }

And thats some simple error handling(for complex use you should make a better error handling, eg: the “Failed to use Notifications!” error is raised when client did not authorized OR when the client cant use them).
Heres a picture of what you can get!
Example
Notes:To test this Javascript i believe you have to upload you files into a server since the authorization for NOtifications is Domain Based.

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: