飞信通知api_网络通知API

飞信通知api

Every UI framework has the same set of widgets which have become almost essential to modern sites: modals, tooltips, button varieties, and notifications.  One problem I find is each site having their own widget colors, styles, and more -- users don't get a consistent experience.  Apparently the W3C felt the same way because they've created a Web Notifications API.  Here's how to use it!

每个UI框架都有相同的小部件集,这些小部件几乎已成为现代网站所必需的:模态,工具提示,按钮变体和通知。 我发现的一个问题是,每个站点都有各自的窗口小部件颜色,样式等,用户无法获得一致的体验。 显然W3C感觉也一样,因为他们已经创建了Web Notifications API 。 这是使用方法!

JavaScript (The JavaScript)

The first step is basic feature detection, as you would expect, and then you jump into the notification creation code:

第一步是您期望的基本功能检测,然后跳至通知创建代码:


if(window.Notification && Notification.permission !== "denied") {
	Notification.requestPermission(function(status) {  // status is "granted", if accepted by user
		var n = new Notification('Title', { 
			body: 'I am the body text!',
			icon: '/path/to/icon.png' // optional
		}); 
	});
}


Once you confirm the Notification API is supported, you must request notification access from the user.  If the status comes back as "granted", you can create a new notification with a title and body text.

确认支持Notification API后,您必须请求用户的通知访问权限。 如果状态返回为“已授予”,则可以创建带有标题和正文的新通知。

Closing the notification is simple with the close method:

使用close方法关闭通知很简单:


n.close();


I really like the idea of the browser providing native APIs for common UI components, but I do find it funny that each browser has a different look for notifications.  I do like the simplicity of the Web Notifications API though -- what do you think?

我真的很喜欢浏览器为常见的UI组件提供本机API的想法,但是我确实感到很有趣,每个浏览器都有不同的通知外观。 我确实喜欢Web Notifications API的简单性-您如何看待?

翻译自: https://davidwalsh.name/notifications-api

飞信通知api

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值