SlideShare a Scribd company logo
Submit Search
Upload
利用 JavaScript 實作瀏覽器推播通知
Report
Share
Hsin-Hao Tang
Front End Developer
Follow
•
1 like
•
32,065 views
1
of
16
利用 JavaScript 實作瀏覽器推播通知
•
1 like
•
32,065 views
Report
Share
Download Now
Download to read offline
Internet
利用 JavaScript 實作瀏覽器推播通知
Read more
Hsin-Hao Tang
Front End Developer
Follow
Recommended
大学・学校VDIのお悩み解決:教育機関特化型 仮想デスクトップだからできる効率化 by
大学・学校VDIのお悩み解決:教育機関特化型 仮想デスクトップだからできる効率化
株式会社クライム
1.3K views
•
37 slides
Smart Home Insurance by
Smart Home Insurance
Matteo Carbone
7.6K views
•
25 slides
Apostila operacoes logisticas_exercicios_fabio_lucio questões com gabarito by
Apostila operacoes logisticas_exercicios_fabio_lucio questões com gabarito
Fabiano Cunha Marinho
2.6K views
•
8 slides
Les Mills GRIT - A Great Small Group Training Solution by
Les Mills GRIT - A Great Small Group Training Solution
Bryan K. O'Rourke
3.5K views
•
36 slides
Electronic commerce and Data Warehouses by
Electronic commerce and Data Warehouses
Sorina Chirilă
620 views
•
26 slides
Administração da Produção - Planejamento e Controle de Estoques by
Administração da Produção - Planejamento e Controle de Estoques
douglas
7.1K views
•
91 slides
More Related Content
Featured
ChatGPT and the Future of Work - Clark Boyd by
ChatGPT and the Future of Work - Clark Boyd
Clark Boyd
25.3K views
•
69 slides
Getting into the tech field. what next by
Getting into the tech field. what next
Tessa Mero
6K views
•
22 slides
Google's Just Not That Into You: Understanding Core Updates & Search Intent by
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Lily Ray
6.5K views
•
99 slides
How to have difficult conversations by
How to have difficult conversations
Rajiv Jayarajah, MAppComm, ACC
5.2K views
•
19 slides
Introduction to Data Science by
Introduction to Data Science
Christy Abraham Joy
82.4K views
•
51 slides
Time Management & Productivity - Best Practices by
Time Management & Productivity - Best Practices
Vit Horky
169.7K views
•
42 slides
Featured
(20)
ChatGPT and the Future of Work - Clark Boyd by Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
Clark Boyd
•
25.3K views
Getting into the tech field. what next by Tessa Mero
Getting into the tech field. what next
Tessa Mero
•
6K views
Google's Just Not That Into You: Understanding Core Updates & Search Intent by Lily Ray
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Lily Ray
•
6.5K views
How to have difficult conversations by Rajiv Jayarajah, MAppComm, ACC
How to have difficult conversations
Rajiv Jayarajah, MAppComm, ACC
•
5.2K views
Introduction to Data Science by Christy Abraham Joy
Introduction to Data Science
Christy Abraham Joy
•
82.4K views
Time Management & Productivity - Best Practices by Vit Horky
Time Management & Productivity - Best Practices
Vit Horky
•
169.7K views
The six step guide to practical project management by MindGenius
The six step guide to practical project management
MindGenius
•
36.6K views
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright... by RachelPearson36
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
RachelPearson36
•
12.7K views
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present... by Applitools
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Applitools
•
55.5K views
12 Ways to Increase Your Influence at Work by GetSmarter
12 Ways to Increase Your Influence at Work
GetSmarter
•
401.7K views
ChatGPT webinar slides by Alireza Esmikhani
ChatGPT webinar slides
Alireza Esmikhani
•
30.4K views
More than Just Lines on a Map: Best Practices for U.S Bike Routes by Project for Public Spaces & National Center for Biking and Walking
More than Just Lines on a Map: Best Practices for U.S Bike Routes
Project for Public Spaces & National Center for Biking and Walking
•
6.9K views
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G... by DevGAMM Conference
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
DevGAMM Conference
•
3.6K views
Barbie - Brand Strategy Presentation by Erica Santiago
Barbie - Brand Strategy Presentation
Erica Santiago
•
25.1K views
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well by Saba Software
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Saba Software
•
25.2K views
Introduction to C Programming Language by Simplilearn
Introduction to C Programming Language
Simplilearn
•
8.4K views
The Pixar Way: 37 Quotes on Developing and Maintaining a Creative Company (fr... by Palo Alto Software
The Pixar Way: 37 Quotes on Developing and Maintaining a Creative Company (fr...
Palo Alto Software
•
88.4K views
9 Tips for a Work-free Vacation by Weekdone.com
9 Tips for a Work-free Vacation
Weekdone.com
•
7.2K views
I Rock Therefore I Am. 20 Legendary Quotes from Prince by Empowered Presentations
I Rock Therefore I Am. 20 Legendary Quotes from Prince
Empowered Presentations
•
142.9K views
How to Map Your Future by SlideShop.com
How to Map Your Future
SlideShop.com
•
275.1K views
利用 JavaScript 實作瀏覽器推播通知
1.
利用 JavaScript 實作瀏覽器推播通知 Summer
Tang 2017 / 08 / 18
2.
Agenda - 瀏覽器支援度 - 使用者授權 -
建立通知 - 標籤 - 事件綁定 - 錯誤處理 - 範例
3.
Push Message - Notification
API:建立和發送通知 - Push API:使用 Service Worker 接收通知
4.
流程 開始 瀏覽器 是否支 援? 使用者 是否授 權? 請求授權 建立通知 發出通知 結束 否 是 同意(granted,使用者已授權過) 尚未要求 授權 (default) 拒絕
(denied) 點擊通知 錯誤處理
5.
瀏覽器支援度檢查 檢查瀏覽器是否支援通知,若支援則可建立通知,否則提示不支援。 if (!('Notification' in
window)) { console.log('This browser does not support notification'); }
6.
Browser Support 目前支援的主流瀏覽器有 - Chrome:42+
才能與 Service Worker 合用 - Firefox:22+ - Safari:6+ - Opera:25+ - Edge Can I use Web Notifications:由於不是每個 feature 都已被瀏覽器實作,所 以使用前要先查詢支援度。
7.
使用者授權 使用者提供的權限分為三種 - granted(同意) - denied(拒絕) -
default(未授權),老舊瀏覽器可能會有 undefined 的狀況。
8.
請求授權 若使用者從未授權過(default)或 undefined(老舊瀏覽器的未知狀態),可使 用 Notification.requestPermission
請求使用者授權,接受通知。 if (Notification.permission === 'default' || Notification.permission === 'undefined') { Notification.requestPermission(function (permission) { // 取得使用者授權後,在這裡處理授權結果 }); }
9.
請求授權
10.
建立通知 若使用者同意授權,則可建立通知。其中通知的 title 是必須的,其他都是 optional。 var
notifyConfig = { body: ' ^o^ /', // 設定內容 icon: '/images/favicon.ico' // 設定 icon }; var notification = new Notification('Hi there!', notifyConfig); // 建立通知
11.
建立通知
12.
標籤 功用 - 分類 - 決定新舊通知的顯示狀況 var
notify = new Notification(Hi there! , { body: ' ^o^ /', icon: '/images/favicon.ico', tag: 'newArrival' // 設定標籤 });
13.
新舊通知的顯示狀況 新舊通知的顯示狀況: - 若標籤已在先前出現過,新通知會關閉 舊通知,只留下新通知 - 若標籤沒有出現過,新舊通知會一起出 現 同一時間,同一種標籤只會出現一個通知。 https://codepen.io/cythilya/pen/RZQPQO?e ditors=1010
14.
事件綁定 - 點擊通知 設定點擊通知之後做些什麼,例如打開特定網頁。 var
notify = new Notification(Hi there! , { body: ' ^o^ /', icon: '/images/favicon.ico' }); notify.onclick = function() { // 綁定點擊事件 window.open('http://sample.com./'); // 打開特定網頁 }
15.
錯誤處理 接收錯誤事件並做處理。 notify.onerror = function(e)
{ // error handling }
16.
範例 https://codepen.io/cythilya/pen/RZQPQO?editors=1010