Author - Web Developer - Educator
Found 2 results for tag "oz"
RSS Feed

HTML API: Intro to Web Notifications API

After taking some time to get back into my craft, I realized that I had missed a lot of advancements in the HTML world, especially the evolution of the HTML API. Yes, there actually is an HTML API.

While researching some of the "new" API, I came across a bunch of articles by Aurelio De Rosa via SitePoint (a lot of good stuff there), and one of the items that caught my attention was the "Web Notifications API." Now, I will say that this stuff is still experimental (see the W3C's Working Draft for Notifications, so it's not widely supported and may even disappear. But for now, it's pretty awesome!)

Anyway, I'm sure you're not here to hear read about my day, so let's get to the point of the matter: HTML API - specifically, the Web Notifications API.

I know a lot of you just want to get to the demo and play with it, so here's a Working Demo (Chrome, Firefox, and Safari 7+ only)


What are "Web Notifications"?


If you use an email system like Gmail or Outlook, chances are you've already seen something like this:
Web Notification in the wild!

Well, those are technically desktop notifications and are usually controlled by software, not browsers. (Prior to HTML5, in order to get Desktop Notifications for Gmail, you had to download the "Gmail Notifier" application, and Outlook is already a software program to begin with).

The main difference is that these notifications (like the one above) are controlled by the browser - yes, the browser can control (certain) desktop notifications, and it's actually quite simple.

How to Use It


In an HTML file, add this bit of Javascript:
var notification = new Notification("New Email Received", { icon: "mail.png" })
That's pretty much it. No external libraries to load, no frameworks to bend around your whim - it's automatically built in to most modern browsers. Now, I know what you're asking: "What? That seems too simple! There has to be more." Don't worry, that was just the extremely core concept (provided by W3C). Technically, there is one more step to do: your application must first ask for permission before doing anything with the visitor's computer. (Yes, I know this seems a bit backwards, but for privacy, I can understand - especially with all of the ActiveX IE vaulnerabilities out there). This seems that the W3C wants to get rid of, but for now, it stays. (W3C says: Warning: In designing the platform notifications are the one instance thus far where asking the user upfront makes sense. Specifications for other APIs should not use this pattern and instead employ one of the many more suitable alternatives. [ref])
Notification.requestPermission(function(){
 
    //we're declaring a new object here
 
    var notification = new Notification(title, options);
 
});
There are 3 states to the "Permission" request: default (equivilant to "denied", because a choice has not been made yet), denied (the user denied the access, and therefore the request cannot proceed), or granted (the green light). This is similiar to when a browser (and/or application) asks to automatically use your physical location to determine items near you, like so:

Geolocation API in use

If you haven't already allowed a site to display desktop notifications (or allowed for geolocation, as the above image), then it will ask you for permission before proceeding:

Asking Permission

Overall, in accordance with "best practices", here's what I have learned/uncovered/researched:

First, you need to check to see if the API is supported. As this is still in the works, it isn't fully supported everywhere (for example, only Chrome, Firefox, and Safari 7+ support this - see http://caniuse.com/#feat=notifications for the full supported range).

In order to detect if the API is supported, add this bit of JS before you go calling Notifications.
//check to see if the API is supported
 
if (!('Notification' in window)){
 
    //API not supported. Show a message or something
 
    ....
 
} else {
 
    // API is supported. Do things
 
    ....
 
}
Therefore, if you have any buttons, links, or other items that make the notifications appear based on calling them, then you can disable them safely (or, if you are basing it off of automatic notifications, like emails, scores, or calendars, you can safety defer the notifications instead of having an error thrown).

Let's get fancy


The above sample is pretty much all you need to know for calling Notifications. If you want to get a little fancy with things, try this:
title = 'Email Received';
 
options = {
 
    body: 'You have a total of 3 unread emails',
 
    tag: 'preset', //The UID of the notification
 
    icon: 'https://mail.google.com/mail/u/0/images/favicon2.ico'
 
};
Before we go into detail about this, let's break it down:

  • title: This is the title of the Notification (duh)

  • options: This outlines a few of the Instance options available in the API
    (technically, everything is optional)

    • dir: This determines the text direction that the notification will be displayed (options are auto, ltr (left to right), or rtl (right to left) (default: auto)

    • lang: The language that the notification should be displayed in (example: 'en' for 'english') - (default: default language for computer)

    • body: The message that is displayed in the notification, like the body of a letter (default: blank)

    • tag: I like to think of this as the UID (Unique IDentifier) of the notification. Examples in the W3C Draft Documentation show usage of multiple instances of notification windows, so if you want one for your Email, Calendar, Reminders, etc, you can name each appropriately instead of just having to fall back on just 1. (default: blank)

    • icon: If you have a jpg, png, ico, or any other image file to show with the notification, you can list it here (I'd recommend the full absolute path of a web file instead of locally) (default: blank)

Those are all of the options available for the Notification API right now. Maybe more will come in the future - it just depends on what the official word is on the development.

FAQ


Can the notification automatically close after a few seconds?

I'd like to say 'yes', but the answer is unfortunately, no - at least, not completely.

I've tested my demo in Chrome and Firefox and had different results. I set up a setTimeout right after the notification is shown and tested it.
notification.onshow = function() { setTimeout(notification.close, 15000) }
However, Firefox displays the notification and automatically dismisses it after 5 seconds, regardless of the timeout, and Chrome will display the notification until you close it. Apparently, the problem is that the drafted Event Handlers don't register in the browsers, yet. I've tried to implement a calling feature of event.type == 'onshow', then call the setTimeout function, but to no avail. I've also tried to match up the event.type prior to the forEach function, but yet again, nothing.

Demo

In case you missed it above, here's the live demo again. It provides a call button that calls the notification on demand. I'm sure you can play around with it and figure out how it works.

References

Mozilla Dev Documentation: https://developer.mozilla.org/en-US/docs/Web/API/Notification W3C Draft: w3.org/TR/notifications/ Articles by Aurelio De Rosa: sitepoint.com/author/aderosa/ Aurelio's Notifications API Demo: http://aurelio.audero.it/demo/web-notifications-api-demo.html "Can I Use" browser feature comparison: http://caniuse.com/#feat=notifications


Tags:#html #api #javascript #w3c #mozilla #demos #tutorials

RSS Feed

Bad day? That's okay! I have GENIES!!!

More Genies? Why not?
(edited from the original for audiences - view original)

Today seemed to start off good, but then it went down the hole.....fast. And it seemed like I had to build my own ladder to get out of the hole.

Anyway, onto the details: I woke up at about 10:30ish, because Keat had class at 11:30. After writing last night's entry, I wanted to sleep for as long as I could. However, after falling asleep to Futurama and hearing the "Anthology of Interest II" episode (the one where Leela finds her "true home" in a certain film containing lollipop children, a brain-less scarecrow, a squeaky 1930's android, an ironic lion, and an omnipotent wizard), and I realized, "If the Wicked Witch of the West melted with just a small amount of liquid, then does that mean that she never drank anything or ever took a shower? I mean, I can understand the 'no shower' thing because she's a wicked witch and that would make her ugly, but never showering? That's just down right repulsive!"

Anyway, Keat was dropped off at school, and I went to the office. I knew that the server transfer was taking place, and I really want to thank the support guy I'm working with at my hosting company. He's helping me out through the transistion from RHEL 3AS (cPanel) to CentOS 5 (Plesk). I thought that it would be a lot of work to transfer the files over, but I didn't realize it would take this much work (I'll get into one bugging detail at the end).

As I drove to the office, I received a call from Allan, the Executive Director of the building. I couldn't get to the phone before it went to voicemail, but he said that his computer wasn't connecting to the internet and his computer was "fading out." Wasn't quite sure what that meant, but I was only 5 minutes from the office.

I arrived and jumped straight into work. I opened the office door, placed my laptop bag on my chair, and went right into the server room to check the primary connection. It was working fine. So....what's up with Allan's computer?

I went back to my office and set up my laptop. After going through my daily emails and my DDN (or RSS as most know it, but I call it my "Daily Digital Newspaper"), I jumped back into the server transition. It seemed kind of slow since only 1 domain was being transfered at a time. Apparently, the MySQL databases weren't being moved until I said so. 1 task down.

I had a to-do list from a client from yesterday, so I took care of that while I had the chance. 2 tasks down.

After about 2 hours, I had an issue with the MySQL database transfer and credentials. I asked the support guy, and he fixed most of the issue. However, I found out that they want each site to have its own unique login to the database instead of 1 generic login. I went with 1 generic login for the longest time because it was quick. Security wise - that is a large risk. Now I know that Q&D (quick and dirty) isn't the best way to go.

After a while, I realized: "Hey, when they are transferring the shadowdev.com domain, my email system will be down!" So I sent an informative email to the support guy with my backup email address.

After a LONGER while, I was wondering what was taking so long. I hadn't heard anything from my support guy since 1pm (EST) and was wondering how things were going. I went to go pick up Keat at school, got some quick lunch (needed to get back in case there were more server issues), and got back to the office. Guess what: the internet goes out. I started to get furious! THIS IS JUST WHAT I NEEDED! Here I am trying to oversee a sensitive server transition, and the freakin' internet just went out. What a day.

I went to the main server room thinking the problem was local. Nope, the main server wouldn't connect either. Therefore, the problem was on our ISP's end. I tried to load the community college's website on my phone for a contact number (they're our ISP), only to find out that their main site is down. Great. So if their site is down, then the whole county is down. I called someone I knew over there only to get voicemail. I thought "If their phones are on a VoIP system, and the network is down, then their phones are out." Just great. I called again after 10 minutes just because I could, and got the person I was looking for. She let me know that the main internet supplier in the whole region had a fiber link cut and they didn't know how long it would take to fix. That was 3pm. I'm online now, so I hope they fixed it if the office and home use the same artery for the connection.

Keat asked me a few marketing budget questions while we were waiting, and then (after leaving an informational letter) we left for Keat's oil appointment. We pulled into the station to have her car serviced and went for a stroll downtown. Stopped by the library, then had a chocolate malted and a Cheerwine at the Woolworth's Sandwich bar in downtown. Add a Turkey sandwich, and that's good livin' right 'der.

We came home and I took a little nap. Got up, responded to some emails and checked by DDN, then did some laundry. However (here's the detail) I found out that the main reason why the server transistion is taking so long is because the server support guy is going through each of my database configuration files and changing the information to the site-specific login. All 11GB of files.

Honestly, I would have been happy if the files, databases, and domains were transfered as they are, and I would take care of the relative and absolute file location updates and database privileges. That's how I expected to spend Thanksgiving: updating server files.

However, the server support guy said that since so many sites depended on the main shadowdev.com domain, he was going to wait to transfer that until Friday or Saturday. One problem: most of the sites that have a news feature use a centralized function for parsing content links and link-like information - and that's in the main shadowdev.com files. Without the shadowdev.com files, the sites with news features can't parse the link-related content properly. It's going to be a long break.

But I found two things that cheered me up today. 1: the comic above. I can't believe nobody thought (and published) of it before! It all makes sense! and 2: Keat and I were finishing up our chocolate malt, and she stood up to go to the restroom. She put her phone on the table and said "You hold on to it if the car guys call." The moment she places it on the table, "RING RING RING". That was something you couldn't time if you had to do it again. She answered, her car was ready, and she ran to the restroom. She and I had a fun hypothetical conversation after that. Her: "HELLO! YOUHAVETHEWORSTTIMINGINTHEWORLD!" / Car guy: "Your car is ready." / Her: "THANK YOU!" That made me laugh.


Edit: 2010-11-23 22:14:32 Forgot one thing. This photo made me smile as well. Thought I'd share it around.


Enough for today. Time for a whatever-we-can-cook-up dinner. Then, off to Thanksgiving....yay.


Tags:#thanksgiving #cyanideandhappiness #genie #car #server #parse #oz #futurama #centos #internet #mysql