{"id":3499,"date":"2017-02-20T17:01:33","date_gmt":"2017-02-20T09:01:33","guid":{"rendered":"http:\/\/blog.hoyo.idv.tw\/?p=3499"},"modified":"2021-07-08T08:53:47","modified_gmt":"2021-07-08T00:53:47","slug":"%e7%b6%b2%e9%a0%81%e9%80%9a%e7%9f%a5-html5-notification","status":"publish","type":"post","link":"https:\/\/blog.hoyo.idv.tw\/?p=3499","title":{"rendered":"\u7db2\u9801\u901a\u77e5 HTML5 Notification"},"content":{"rendered":"<p>--<\/p>\n<h2>\u7d50\u8ad6 (2021\/7\/8)<\/h2>\n<p>\u5c31\u7b97\u662f\u56e0\u70ba\u6e2c\u8a66\u539f\u56e0\uff0c\u5168\u90e8\u5141\u8a31\u6b0a\u9650 Chrome 91 &amp; Firefox 89 \u90fd\u4e0d\u6703\u52d5\u4f5c\u3002\u610f\u601d\u5c31\u662f\u96a8\u8457\u6642\u4ee3\u6539\u8b8a\uff0c\u500b\u4eba\u5316\u8a2d\u5b9a\u9010\u6f38\u6210\u70ba\u5e38\u614b\u5f8c\uff0c\u5c31\u4e0d\u9700\u8981\u641e\u9019\u7a2e\u5927\u90e8\u5206\u7684\u4eba\u90fd\u6703\u95dc\u9589\uff0c\u6216\u662f\u5c31\u7b97\u60f3\u4f7f\u7528\u4e5f\u4e0d\u898b\u5f97\u80fd\u7528\u7684\u529f\u80fd\u3002<\/p>\n<p>--<\/p>\n<h2>\u53c3\u8003\u8cc7\u6e90<\/h2>\n<ul>\n<li><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/notification\" target=\"_blank\" rel=\"noopener\">Notification<\/a><\/li>\n<li><a href=\"http:\/\/xuhong.github.io\/2014\/05\/22\/notification\/\" target=\"_blank\" rel=\"noopener\">\u4f7f\u7528HTML5 Notification API\u5f00\u542f\u6d4f\u89c8\u5668\u684c\u9762\u63d0\u9192<\/a><\/li>\n<\/ul>\n<p>HTML5 Notification \u00a0\u53ef\u4ee5\u5728\u5141\u8a31\u901a\u77e5\u7684\u60c5\u6cc1\u4e0b\uff0c\u51fa\u73fe\u63d0\u9192\u7684\u8a0a\u606f\uff0c\u800c\u4e14\u4e0d\u9700\u8981\u76ef\u8457\u7db2\u9801\uff0c\u53ea\u8981\u700f\u89bd\u5668\u53ca\u901a\u77e5\u7684\u9801\u9762\u958b\u555f\u7684\u72c0\u614b\u4e0b\u5373\u53ef\u4f7f\u7528\u3002<\/p>\n<p>\u4f7f\u7528\u524d\u5fc5\u9808\u4e86\u89e3\u700f\u89bd\u5668\u652f\u63f4\u7a0b\u5ea6\u3002<\/p>\n<p>--<\/p>\n<p>\u53d6\u5f97\u7db2\u9801\u901a\u77e5\u6b0a\u9650<\/p>\n<pre class=\"lang:js decode:true\">$(function(){\r\n\r\n    if(Notification &amp;&amp; Notification.permission !== \"granted\"){\r\n        Notification.requestPermission(function(status){\r\n            if(Notification.permission !== status){\r\n                Notification.permission = status;\r\n            }\r\n        });\r\n    }\r\n\r\n});\r\n<\/pre>\n<p>\u9801\u9762\u8d77\u59cb\u6642\u8a62\u554f\u3001\u53d6\u5f97\u901a\u77e5\u6b0a\u9650<\/p>\n<p>--<\/p>\n<p>\u986f\u793a\u901a\u77e5<\/p>\n<pre class=\"lang:js decode:true\">function notify(){\r\n    if (Notification.permission === \"granted\") {\r\n        var notification = new Notification(\"Hi there!\");\r\n    }\r\n    else{\r\n        alert('Hi there!');\r\n    }\r\n}<\/pre>\n<p>\u5beb\u4e00\u500b\u7576\u9700\u8981\u901a\u77e5\u6642\u547c\u53eb\u7684\u529f\u80fd\uff0c\u4e26\u4e14\u6839\u64da\u662f\u5426\u5141\u8a31\u901a\u77e5\u4f86\u57f7\u884c\u8a0a\u606f\u986f\u793a\u65b9\u5f0f<\/p>\n<p>--<\/p>\n<p>\u4ec0\u9ebc\u6642\u5019\u901a\u77e5\uff1f<\/p>\n<p>\u63a5\u4e0b\u4f86\u53ea\u8981\u6c7a\u5b9a\u4ec0\u9ebc\u6642\u5019\u57f7\u884c\u986f\u793a\u901a\u77e5\u7684\u65b9\u6cd5\u5373\u53ef\u3002\u53ef\u4ee5\u7528 WebSocket \u4f86\u9054\u6210\u5373\u6642\uff0c\u4e5f\u53ef\u4ee5\u7528 setInterval() \u5b9a\u6642\u57f7\u884c\u6aa2\u67e5<\/p>\n<p>--<\/p>\n<p>\u5b8c\u6574\u7bc4\u4f8b<\/p>\n<pre class=\"lang:default decode:true\">&lt;!-- \u624b\u52d5\u57f7\u884c --&gt;\r\n&lt;button onclick=\"notifyMe()\"&gt;Notify me!&lt;\/button&gt;\r\n\r\n&lt;script&gt;\r\n    Notification.requestPermission(function(status){\r\n        console.log('User Choice', status);\r\n        if (status !== 'granted') {\r\n            console.log('\u63a8\u64ad\u5141\u8a31\u88ab\u62d2\u7d55\u4e86!');\r\n        }\r\n    });\r\n\r\n    function notifyMe() {\r\n        \/\/ Let's check if the browser supports notifications\r\n        if (!(\"Notification\" in window)) {\r\n            alert(\"This browser does not support desktop notification\");\r\n        }\r\n\r\n        \/\/ Let's check whether notification permissions have already been granted\r\n        else if (Notification.permission === \"granted\") {\r\n            \/\/ If it's okay let's create a notification\r\n            var notification = new Notification(\"Hi there!\");\r\n        }\r\n\r\n        \/\/ Otherwise, we need to ask the user for permission\r\n        else if (Notification.permission !== \"denied\") {\r\n            Notification.requestPermission().then(function (permission) {\r\n                \/\/ If the user accepts, let's create a notification\r\n                if (permission === \"granted\") {\r\n                    var notification = new Notification(\"Hi there!\");\r\n                }\r\n            });\r\n        }\r\n    }\r\n\r\n    \/\/ \u6bcf 2 \u79d2\u57f7\u884c\u4e00\u6b21\r\n    setInterval(notifyMe, 2000);\r\n&lt;\/script&gt;<\/pre>\n<p>\u901a\u77e5\u7684\u8a0a\u606f\u662f\u51fa\u73fe\u5728\u7cfb\u7d71\u4e0a\uff0c\u4e0d\u662f\u986f\u793a\u5728\u700f\u89bd\u5668\u4e2d<\/p>\n<p><a href=\"https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2017\/02\/20210708_084256_.jpg\" data-rel=\"lightbox-image-0\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" class=\"alignnone size-medium wp-image-8533\" src=\"https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2017\/02\/20210708_084256_-300x222.jpg\" alt=\"\" width=\"300\" height=\"222\" srcset=\"https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2017\/02\/20210708_084256_-300x222.jpg 300w, https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2017\/02\/20210708_084256_-768x569.jpg 768w, https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2017\/02\/20210708_084256_-1024x759.jpg 1024w, https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2017\/02\/20210708_084256_-405x300.jpg 405w, https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2017\/02\/20210708_084256_.jpg 1094w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>--<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p class=\"pvc_stats all \" data-element-id=\"3499\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> &nbsp;1,524&nbsp;total views, &nbsp;1&nbsp;views today<\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>-- \u7d50\u8ad6 (2021\/7\/8...<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p class=\"pvc_stats all \" data-element-id=\"3499\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> &nbsp;1,524&nbsp;total views, &nbsp;1&nbsp;views today<\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[262],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/3499"}],"collection":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3499"}],"version-history":[{"count":6,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/3499\/revisions"}],"predecessor-version":[{"id":8536,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/3499\/revisions\/8536"}],"wp:attachment":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3499"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3499"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3499"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}