需求描述

简单讲就是轮询列表,当发现最新的清算信息时候,需要弹出chrome通知

image.pngtask

  • 业务权限问题
  • 向用户发起授权通知
  • 查看官网暴露的api是否能满足业务需求

初步实现

image.png

代码

  1. import { useVotes } from "./useVotes";
  2. import { useWallet } from "./useWallet";
  3. export function useNotice() {
  4. /**
  5. * @description: chrome, firfox, edge通知
  6. * @param {*} hash
  7. * @return {*}
  8. */
  9. function noticeLiquidation(hash) {
  10. let { wallet } = useWallet();
  11. let { myVotes } = useVotes();
  12. if (!("Notification" in window)) {
  13. console.log(
  14. "%c%s",
  15. "color: #99adcc",
  16. "This browser does not support desktop notification"
  17. );
  18. return;
  19. }
  20. if (!hash) {
  21. console.log("hash is null");
  22. return;
  23. }
  24. if (!wallet.isConnected) {
  25. console.log("wallet.isConnected is fail ,noticeLiquidation is closed");
  26. return;
  27. }
  28. if (!myVotes?.isRuler) {
  29. console.log("not a ruler,noticeLiquidation is closed", myVotes);
  30. return;
  31. }
  32. let orgin =
  33. process.env === "production"
  34. ? "https://dpl.shorter.finance/app/liquidations/" + hash
  35. : window.location.origin + "/app/liquidations/" + hash;
  36. let newMessage = `PLease click the following url to check liquidation detail. ${orgin}`;
  37. console.log("%c%s", "color: #408059", newMessage, Notification.permission);
  38. if (Notification.permission === "granted") {
  39. new Notification("New Liquidation item coming", {
  40. tag: "message",
  41. body: newMessage,
  42. icon: "https://gitee.com/web-kubor/oss/raw/master/uPic/favicon.png"
  43. });
  44. } else if (Notification.permission !== "denied") {
  45. Notification.requestPermission().then(permission => {
  46. if (permission === "granted") {
  47. new Notification("New Liquidation item coming", {
  48. tag:"message",
  49. body: newMessage,
  50. icon: "https://gitee.com/web-kubor/oss/raw/master/uPic/favicon.png"
  51. });
  52. }
  53. });
  54. }
  55. }
  56. return {
  57. noticeLiquidation
  58. };
  59. }

问题来了

  • 授权没问题,消息弹出没问题, 但是点击弹出消息,预期是直接跳转目标页面,但是这里跳转到了网站的默认主页面
  • 无论是否有消息,每次只提示一次

问题定位

  • 点击事件是由chrome自主的默认行为,并没有触发,我们newMessage变量中传入的网址,也就是说,我们要自己添加事件,同时阻止默认行为
  • 通过查阅文档, tag属性相当于id标记消息,来确保不会重复弹出

解决问题

  • 加入onclick事件
  • tag加入时间戳 ```javascript import { useVotes } from “./useVotes”; import { useWallet } from “./useWallet”;

export function useNotice() { /**

  • @description: chrome, firfox, edge通知
  • @param {*} hash
  • @return {} /

    function noticeLiquidation(hash) { let { wallet } = useWallet(); let { myVotes } = useVotes(); if (!(“Notification” in window)) { console.log( “%c%s”, “color: #99adcc”, “This browser does not support desktop notification” ); return; } if (!hash) { console.log(“hash is null”); return; } if (!wallet.isConnected) { console.log(“wallet.isConnected is fail ,noticeLiquidation is closed”); return; }

    if (!myVotes?.isRuler) { console.log(“not a ruler,noticeLiquidation is closed”, myVotes); return; } let theLink = process.env === “production” ? “https://dpl.shorter.finance/app/liquidations/“ + hash : window.location.origin + “/app/liquidations/“ + hash; let body = PLease click the following url to check liquidation detail. ${hash}; let title = “New Liquidation item coming” let iconURL = “https://gitee.com/web-kubor/oss/raw/master/uPic/favicon.png“ console.log(“%c%s”, “color: #408059”, body); if (Notification.permission === “granted”) { createNotification(title, body, theLink, iconURL) } else if (Notification.permission !== “denied”) { Notification.requestPermission().then(permission => { if (permission === “granted”) {

    1. createNotification(title, body, theLink, iconURL)

    } }); } }

    /**

  • @description: 创建chrome通知
  • @param {*} title
  • @param {*} body
  • @param {*} theLink 跳转链接
  • @param {*} iconURL
  • @return {} / function createNotification(title, body, theLink, iconURL) { let notification = new Notification(title, { body: body, tag: “message-“ + Date.now(), icon: iconURL, }) notification.onclick = function(event) { event.preventDefault(); // prevent the browser from focusing the Notification’s tab window.open(theLink, ‘_blank’); } setTimeout(notification.close.bind(notification), 7000); }

    return { noticeLiquidation }; }

```