The desktop notification NOTIFICATION,HTML5 supports instances of Web notifications, but with the user's permission,
Chrome://settings/content/notifications
Settings/Advanced Settings/Content Settings/notifications to see which are prohibited or authorized, can also be viewed on the left side of the page bar
The code is as follows
1 if(window. Notification) {2 if(Notification.permission! = "Granted") {3Notification.requestpermission (function(permission) {4 mynotification ();5 });6}Else{7 mynotification ();8 }9 //no pop-up window.Ten functionmynotification () { One if(Notification.permission = = "Granted") { A varnotification =NewNotification ("title", { -Body: "Content", -Icon: "Https://www.baidu.com/img/bd_logo1.png" the }); - //message box is clicked Event -Notification.onclick =function () { - notification.close (); + }; -Notification.onshow =function () { +SetTimeout (function () { A notification.close (); at}, 5000); - }; - } - } -}Else { -Alert ("Notification not supported"); in}
The effect is as follows:
The license for Chrome's own notifications is in the Manifest.json file, which requires the user to click the authorization button.
The thing is, the extension structure, 3 files, Manifest.json, a JS file, a picture icon file.
The Manifest.json code is as follows
1 { 2 "manifest_version": 2, 3 "name": "Message Prompt", 4 " Version ":" 1.0.0 ", 5 " Permissions ": [ 6 " Notifications " 7 ], 8 "background": {"Scripts": [ 9 " Notifications.js "Ten}" }
The Notifications.js code is as follows
1 /*Desktop Message Notifications2 * _MSG message content3 * _title title4 * _ico icon5 * _time How many milliseconds after destruction6 */7 functionmynotification (_msg,_title,_ico,_time) {8 varico = _ico | | "Ico/icon.png";9 vartitle = _title | | Notification;Ten varmsg = String (_msg) | | Message "; One varTime = _time | | 5000; A //display a desktop notification - if(window.webkitnotifications) { - varnotification =window.webkitNotifications.createNotification (ico,title,msg); the notification.show (); -SetTimeout (function() {notification.cancel ();}, time); -}Else if(chrome.notifications) { - varopt = { +Type: ' Basic ', - Title:title, + Message:msg, A Iconurl:ico, at } -Chrome.notifications.create (", OPT,function(ID) { -SetTimeout (function(){ -Chrome.notifications.clear (ID,function(){}); - }, time); - }); in } -};
This code is running in the background, so it can only be called in the background
such as Mynotification ("title", "Hello World");
This can be in the form of pop-up windows, prompting the user has robbed how many treasure chest, and how the proceeds.
Betta Expansion--notifications Tips (12)