Betta Expansion--notifications Tips (12)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.