简体   繁体   中英

Browser notifications invoked website

So lately on some websites I have seen these browser level invoked notifications that popup even if you didn't open that certain website. You have to allow those notifications to be displayed and then they are pretty much everywhere, even when you first open a browser without tabs.

I don't know how to use for these browser level notifications invoked by my website. A name and some code examples would be appreciated, I don't even know what programming language it's written in (assuming Javascript).

This is a screenshot of what I'm talking about (top right corner):

通知1

通知2

通知3

As you see, they still appear even though the browser is minimized!

These are implemented using the Notification API - some recent documentation can be found here:

https://developer.mozilla.org/en-US/docs/Web/API/notification

You can easily try it from the console of most current browsers -

Notification.requestPermission();

Approve the notification permission and then try

new Notification('Hello, you have been notified!');

The documentation has more detailed usage examples.

As pvg said, there is a Browser API named Notification , which allows you to send notifications to your users. You must request permission first, and if granted , build a new Notification .

I have built a micro library in javascript that does exactl that as an object, go check it out here: https://github.com/jsmrcaga/miniNotif

Usage is simple, you make a new notification as an object like so:

var yourNotif = new miniNotif.notification('title', {body: 'your body', icon: 'URL'});

, and call yourNotif.show() whenever you want it to be displayed.

I believe that the readme on GitHub is out of date though

If you want to implement normal notifications, the answers that you have received are all correct.

If you want to implement PUSH notifications, the answer you expect is in this post (with explanation and a tutorial explaining how you can implement it).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM