Learn about the different hard drive formats like Mac OS, NTFS and FAT32. Find out how you can use NTFS drives with a Mac, and Mac OS drives using Windows. See how FAT32 formatted drives can be used to store files accessible to both systems.
Hi this is Gary with MacMost now. On today’s episode let’s look how to implement notifications using Safari 6 and Java Script. So this is something for web developers. Using Java Script on your web pages you can use the Mountain Lion notifications feature along with Safari 6. It is just a matter of knowing the right Java Script to put in there. So I have made a simple example. So let’s look at the code first. It is a very simple html example. I have a script here in the header. Of course this would be in a dot JS file normally. I have two functions that I define.
The first is going to ask the user permission to send notification from this web page. The second is to send the notification. Then I’ve got here at the bottom some very simple links here to trigger either one of these. So the ask function basically it will send to the console here just the permission level so that I can see what the permission level is.
Now there are three different permission levels that you need to be concerned about. The first one is the default which means the user has never done anything to enable to disable notification from this page. So it what you are going to get when the user clicks this. The second is denied which means they have said no. The third is granted which means they are going to allow notifications so notifications will work.
So, I’m just going to output that there. That’s not part of the regular code of course. This is testing windows dot notification tells you whether or not the browser has the ability. So if somebody is going to a browser other than Safari 6 right now then the code inside here won’t work and the key thing here is that notification.request permission which will basically put up a dialog box that we will look at soon that will ask the user if the web page can send them notifications. It has to have a function in here as the parameter so in this case I am just going to create a simple function that sends again the log to the console so that you can see that they have changed it so the perfect thing would be that it starts off saying default and then it goes and says granted.
The second function is to actually send a notification and this will also test for windows dot notification. It will log the permission level as well which should be granted. Now here I am testing the permission level so it should be granted in order to continue or otherwise this next command won’t have any effect. You use new notification to create notification and the parameters are pretty straight forward. This is the title notification. Then you have this object here, and it has to be an object with different parameters in it. This is all optional. You could just send the one for testing. In this optional set of parameters you have going to have body which is the actual bit of text that will display under the title. So you have to have that of course. It is the whole point. You also want to have something called the tag. The tag is some sort of number or key word that you will use to identify that notification. Now this is good because it prevents multiple notifications being sent. So in this case it will be called test0 if the page for some reason sends test0 more than one time it is not going to list it multiple times. So basically it is an ID for the notification and then you can have various functions that react to events. So you have on click, on show, on close, and on error.
Now “on click” is what happens when the user gets a little alert and it says close or show me the notification. On click is what will then happen when they select that. So it is the most logical one to use for something in Java Script. “On close” is when they dismiss it and “on show” is when it appears and there may be a delay if the system does not show it immediately and it is good to have that call back and of course “on error” is self explanatory. So I’m I am going to have that call back and let’s go and take a look when we go this page in Safari 6. So here I am at this page in Safari and I’ve got my two links. So let’s try the first one which is going to ask permission and you can see it comes up with the dialog box. I’m can hit allow or don’t allow. I’m going to hit allow and one of the things I am going to look at now is under preferences in notifications.
I can see it now lists MacMost and has it set to allow. I can set it to deny and I can remove it. So, that’s the settings in there and shows the ask permission does work. Now I am going to hit send notification. And there it is; it appears on the right as a notification and if I click on it it will run that alert, that function and show what I want. So it kind of reacts. Its like if you put a notification out there that something is going on the user goes over there and clicks on it and your web page reacts to that. Of course, in system preferences under notifications they could change how Safari shows the alert. So they can also go to the alert here rather than the banner and if they do that then send notification will change and you see it will come up here with the close and show. Close will do nothing. Send notification show will then actually act as if they had clicked on the banner.
So a few things. First, I still find this a little bit buggy. When developing this simple sample script I ran into times when Safari simply ignored it. And I just quit Safari and restarted it again and it worked. So, that should be ironed out soon. The other thing is when do you use this? Well it only works if the web page is actually active and open, it is not like you can have somebody accept a notification and then reach out and send them one when they are not at their web site. So for pages that have chat windows or pages that are video conferencing or perhaps a page that’s multi player game it can be useful because you can have Java Script send notification and if the person is not looking at their Safari window or perhaps it is in another tab or it is hidden, they will get the notification whereas if you did an in-page notification using Java Script or something else they might not see it if the page is not open. So it is useful for that.
It is also useful for developing Safari extensions and developing applications that have Java Script imbedded in them if you want to use notifications and it supports the whole web kit thing. So you have got a lot of different options there and I am sure in the future we will see some interesting uses for it. For instance I can see implementing it at MacMost perhaps having a link saying notify me when you vist MacMost and there is some new security information. And if I post a new article about some new security threat next time you visit MacMost even if you are looking at a tutorial on something different you will get a notification saying Hey, there is an article here that you might want to check out. So that is one possible use for it. We will probably see a lot more uses over the next months and years.
I hope you found this useful. Until next time this is Gary with MacMost now.