首页
/ 推送通知的Angular 2组件 - ng2-notifications

推送通知的Angular 2组件 - ng2-notifications

2024-05-31 09:16:00作者:咎岭娴Homer

Demo Gif

ng2-notifications 是一个强大的Angular 2+ 开源组件,它利用了Web的Notifications API,允许你的应用在用户切换标签或专注于其他应用程序时,也能显示系统级别的推送通知。这款组件设计简洁,兼容现有的各种操作系统和浏览器的通知系统。

了解更多关于Notifications API的信息

阅读博客文章

在线演示与快速启动

想立即体验?点击这里查看在线演示

要在自己的项目中快速启用ng2-notifications,只需执行以下步骤:

$ npm install ng2-notifications --save

接着,在你的App模块中引入并使用该组件:

import { PushNotificationComponent } from 'ng2-notifications/ng2-notifications';

@Component({
  selector: 'my-app',
  template: `
  <push-notification 
    title="ng2-notifications"
    body="Component for Native Push Notifications"
    icon="https://goo.gl/3eqeiE">
  </push-notification>
  `,
  directives: [PushNotificationComponent]
})

使用方法

基础的推送通知可以通过设置标题、内容和图标属性来创建,如下所示:

<push-notification 
  title="ng2-notifications"
  body="Component for Native Push Notifications"
  icon="https://goo.gl/3eqeiE">
</push-notification>

若要实现数据绑定,可以这样做:

<push-notification 
  [title]="notification.title"
  [body]="notification.description"
  [icon]="notification.icon">
</push-notification>

自动关闭

通过设置closeDelay属性,你可以让通知在指定时间后自动关闭。例如,下面的例子将在5秒后自动关闭:

<push-notification 
  ...
  closeDelay="5000">
</push-notification>

控制何时触发通知

通过添加模板变量并在事件上调用.show() 方法,你可以控制何时展示通知:

<push-notification #notification
  ...
  (load)="notification.show()">
</push-notification>

点击事件处理

给通知添加点击事件处理器,只需要使用(action)事件:

<push-notification 
  ...
  (action)="myFunction($event)">
</push-notification>

显示和关闭事件

监听通知的显示和关闭事件,可以为它们添加回调函数:

<push-notification 
  ...
  (show)="myShowFunction($event)"
  (close)="myCloseFunction($event)">
</push-notification>

错误事件

当遇到错误时,可通过(error)事件进行处理:

<push-notification 
  ...
  (error)="handleError($event)">
</push-notification>

其他选项

此外,还有更多自定义选项可供选择,比如datatagdir(默认值:'auto')以及lang(默认值:'en-US')。

浏览器支持

查看完整的浏览器兼容性列表。请注意,有些特性仍处于建议阶段,或者目前尚未被所有浏览器支持,如renotifysilentsoundvibratestickynoscreen。另外,如果你在Chrome的隐私浏览模式下,将无法收到通知。

许可证

本项目采用MIT许可证,详情见LICENSE 文件。

版权 © 2017 Alex Castillo

许可在此明确授予,无需付费,任何个人或实体均可获得一份本软件及其相关文档的副本,不受限制地使用、复制、修改、合并、发布、分发、转授权和/或销售本软件的副本,以及允许为其接收软件的人员这样做,但须遵守以下条件:

上述版权声明和许可通知应包含在本软件的所有副本或实质部分中。

本软件提供“原样”状态,不附带任何形式的明示或暗示保证,包括但不限于对适销性、特定用途适用性和非侵权性的保证。在任何情况下,作者或版权持有者都不应对因使用、不能使用本软件所产生的直接、间接、偶然、特殊、惩罚性或后果性损害承担责任,即使已被告知可能出现此类损害。

登录后查看全文

项目优选

收起
RuoYi-Vue3RuoYi-Vue3
🎉 (RuoYi)官方仓库 基于SpringBoot,Spring Security,JWT,Vue3 & Vite、Element Plus 的前后端分离权限管理系统
Vue
438
335
leetcodeleetcode
🔥LeetCode solutions in any programming language | 多种编程语言实现 LeetCode、《剑指 Offer(第 2 版)》、《程序员面试金典(第 6 版)》题解
Java
51
14
ohos_react_nativeohos_react_native
React Native鸿蒙化仓库
C++
96
170
Cangjie-ExamplesCangjie-Examples
本仓将收集和展示高质量的仓颉示例代码,欢迎大家投稿,让全世界看到您的妙趣设计,也让更多人通过您的编码理解和喜爱仓颉语言。
Cangjie
273
443
openGauss-serveropenGauss-server
openGauss kernel ~ openGauss is an open source relational database management system
C++
51
116
HarmonyOS-ExamplesHarmonyOS-Examples
本仓将收集和展示仓颉鸿蒙应用示例代码,欢迎大家投稿,在仓颉鸿蒙社区展现你的妙趣设计!
Cangjie
342
222
cherry-studiocherry-studio
🍒 Cherry Studio 是一款支持多个 LLM 提供商的桌面客户端
TypeScript
343
34
openHiTLSopenHiTLS
旨在打造算法先进、性能卓越、高效敏捷、安全可靠的密码套件,通过轻量级、可剪裁的软件技术架构满足各行业不同场景的多样化要求,让密码技术应用更简单,同时探索后量子等先进算法创新实践,构建密码前沿技术底座!
C
88
243
CangjieMagicCangjieMagic
基于仓颉编程语言构建的 LLM Agent 开发框架,其主要特点包括:Agent DSL、支持 MCP 协议,支持模块化调用,支持任务智能规划。
Cangjie
559
39
carboncarbon
轻量级、语义化、对开发者友好的 golang 时间处理库
Go
7
2