桌面提醒API之Notification

桌面通知功能能够让浏览器即使是最小化的状态,也能将消息通知给用户。
这和WebIM是最天然的组合,不过目前支持Desktop Notification功能的浏览器
只有Chrome5+,且弹出来的内容只能是文本,而不支持HTML。

在实际使用的过程中,应该尽量减少通知功能对用户的干扰,最大程度的减少通知功能的出现,这就需要解决以下几个问题:

  1. 收到多条消息时确保只出现一条通知;
  2. 当用户处于IM出现的页面中时(页面处于Focus状态)将不出现通知;
  3. 当用户使用多Tab开启多个存在IM的页面时,只要有一个页面处于Focus状态将不出现通知;
  4. 如何让用户点击通知浮动层即可定位到具体的聊天窗口
  5. 此外,还需要解决一个便利性问题
<script type="text/javascript">
	const miao = 5;

	function init() {
		if (window.Notification) {
			// 请求通讯许可 
			window.Notification.requestPermission();
		}
	}

	function notify() {
		var icon = "logo.png";
		var title = "窗口将在 " + miao + " s后关闭";
		var body =  "欢迎关注微信公众号【qdgithub】";

		if (window.Notification) {
			//	检查通讯许可
			if (window.Notification.checkPermission() == 0) {
				// 创建通讯
				var popup = window.Notification.createNotification(icon, title, body);
				popup.ondisplay = function(event) {
					setTimeout(function() {
						event.currentTarget.cancel();
					}, miao * 1000);
				}
				popup.show();
			} else {
				window.Notification.requestPermission();
				return;
			}
		}
	}
</script>

参考

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring Boot中调用小程序API实现弹框消息提醒,你可以按照以下步骤进行操作: 1. 首先,确保你已经在Spring Boot项目中引入了相关的依赖,如WeChat SDK等。 2. 在你的Spring Boot项目中创建一个Controller类,用于处理相关的请求和逻辑。 3. 在Controller中,编写一个接口方法,用于调用小程序API发送消息提醒。你可以使用小程序的模板消息功能来实现弹框消息提醒。 4. 在该接口方法中,调用小程序API发送模板消息。你需要提供小程序的AppID、AppSecret等认证信息,并构造模板消息的数据。 5. 将构造好的模板消息数据发送到小程序API,触发弹框消息提醒。 以下是一个简单的示例代码,供参考: ```java @RestController public class WeChatController { @Autowired private WeChatService weChatService; @PostMapping("/sendNotification") public String sendNotification(@RequestBody NotificationRequest request) { // 构造模板消息数据 Map<String, Object> data = new HashMap<>(); data.put("keyword1", request.getTitle()); data.put("keyword2", request.getContent()); // 调用小程序API发送模板消息 boolean success = weChatService.sendTemplateMessage(request.getOpenId(), "templateId", data); if (success) { return "Notification sent successfully."; } else { return "Failed to send notification."; } } } ``` 在上述示例中,你需要自己实现WeChatService类,并在其中调用小程序API发送模板消息的具体逻辑。 注意:在实际使用中,你需要替换示例代码中的AppID、AppSecret、templateId等参数,并根据小程序API的文档进行具体的实现和配置。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值