SlideShare a Scribd company logo
利用 JavaScript 實作瀏覽器推播通知
Summer Tang
2017 / 08 / 18
Agenda
- 瀏覽器支援度
- 使用者授權
- 建立通知
- 標籤
- 事件綁定
- 錯誤處理
- 範例
Push Message
- Notification API:建立和發送通知
- Push API:使用 Service Worker 接收通知
流程
開始
瀏覽器
是否支
援?
使用者
是否授
權?
請求授權 建立通知 發出通知
結束
否
是
同意(granted,使用者已授權過)
尚未要求
授權
(default)
拒絕 (denied)
點擊通知 錯誤處理
瀏覽器支援度檢查
檢查瀏覽器是否支援通知,若支援則可建立通知,否則提示不支援。
if (!('Notification' in window)) {
console.log('This browser does not support notification');
}
Browser Support
目前支援的主流瀏覽器有
- Chrome:42+ 才能與 Service Worker 合用
- Firefox:22+
- Safari:6+
- Opera:25+
- Edge
Can I use Web Notifications:由於不是每個 feature 都已被瀏覽器實作,所
以使用前要先查詢支援度。
使用者授權
使用者提供的權限分為三種
- granted(同意)
- denied(拒絕)
- default(未授權),老舊瀏覽器可能會有 undefined 的狀況。
請求授權
若使用者從未授權過(default)或 undefined(老舊瀏覽器的未知狀態),可使
用 Notification.requestPermission 請求使用者授權,接受通知。
if (Notification.permission === 'default' || Notification.permission === 'undefined') {
Notification.requestPermission(function (permission) {
// 取得使用者授權後,在這裡處理授權結果
});
}
請求授權
建立通知
若使用者同意授權,則可建立通知。其中通知的 title 是必須的,其他都是
optional。
var notifyConfig = {
body: ' ^o^ /', // 設定內容
icon: '/images/favicon.ico' // 設定 icon
};
var notification = new Notification('Hi there!', notifyConfig); // 建立通知
建立通知
標籤
功用
- 分類
- 決定新舊通知的顯示狀況
var notify = new Notification(Hi
there! , {
body: ' ^o^ /',
icon: '/images/favicon.ico',
tag: 'newArrival' // 設定標籤
});
新舊通知的顯示狀況
新舊通知的顯示狀況:
- 若標籤已在先前出現過,新通知會關閉
舊通知,只留下新通知
- 若標籤沒有出現過,新舊通知會一起出
現
同一時間,同一種標籤只會出現一個通知。
https://codepen.io/cythilya/pen/RZQPQO?e
ditors=1010
事件綁定 - 點擊通知
設定點擊通知之後做些什麼,例如打開特定網頁。
var notify = new Notification(Hi there! , {
body: ' ^o^ /',
icon: '/images/favicon.ico'
});
notify.onclick = function() { // 綁定點擊事件
window.open('http://sample.com./'); // 打開特定網頁
}
錯誤處理
接收錯誤事件並做處理。
notify.onerror = function(e) {
// error handling
}
範例
https://codepen.io/cythilya/pen/RZQPQO?editors=1010

More Related Content

What's hot

The innerHTML Apocalypse
The innerHTML ApocalypseThe innerHTML Apocalypse
The innerHTML Apocalypse
Mario Heiderich
 
Behavior Driven Development with SpecFlow
Behavior Driven Development with SpecFlowBehavior Driven Development with SpecFlow
Behavior Driven Development with SpecFlow
Rachid Kherrazi
 
게임 프레임워크의 아키텍쳐와 디자인 패턴
게임 프레임워크의 아키텍쳐와 디자인 패턴게임 프레임워크의 아키텍쳐와 디자인 패턴
게임 프레임워크의 아키텍쳐와 디자인 패턴MinGeun Park
 
Workshop 101: Concebendo produtos de forma ágil (e divertida) - Scrum Gathe...
  Workshop 101: Concebendo produtos de forma ágil (e divertida) - Scrum Gathe...  Workshop 101: Concebendo produtos de forma ágil (e divertida) - Scrum Gathe...
Workshop 101: Concebendo produtos de forma ágil (e divertida) - Scrum Gathe...
Diana Corrêa, CSPO / CSM
 
온라인 게임에서 사례로 살펴보는 디버깅 in NDC2010
온라인 게임에서 사례로 살펴보는 디버깅 in NDC2010온라인 게임에서 사례로 살펴보는 디버깅 in NDC2010
온라인 게임에서 사례로 살펴보는 디버깅 in NDC2010
Ryan Park
 
How to Hunt for Lateral Movement on Your Network
How to Hunt for Lateral Movement on Your NetworkHow to Hunt for Lateral Movement on Your Network
How to Hunt for Lateral Movement on Your Network
Sqrrl
 
"CERT Secure Coding Standards" by Dr. Mark Sherman
"CERT Secure Coding Standards" by Dr. Mark Sherman"CERT Secure Coding Standards" by Dr. Mark Sherman
"CERT Secure Coding Standards" by Dr. Mark Sherman
Rinaldi Rampen
 
게임 기획과 Oop
게임 기획과 Oop게임 기획과 Oop
게임 기획과 Oop
snugdc
 
Designing with Capabilities
Designing with CapabilitiesDesigning with Capabilities
Designing with Capabilities
Scott Wlaschin
 
Introducing ArTHIR - ATT&CK Remote Threat Hunting Incident Response Windows tool
Introducing ArTHIR - ATT&CK Remote Threat Hunting Incident Response Windows toolIntroducing ArTHIR - ATT&CK Remote Threat Hunting Incident Response Windows tool
Introducing ArTHIR - ATT&CK Remote Threat Hunting Incident Response Windows tool
Michael Gough
 
The Secret Life of a Bug Bounty Hunter – Frans Rosén @ Security Fest 2016
The Secret Life of a Bug Bounty Hunter – Frans Rosén @ Security Fest 2016The Secret Life of a Bug Bounty Hunter – Frans Rosén @ Security Fest 2016
The Secret Life of a Bug Bounty Hunter – Frans Rosén @ Security Fest 2016
Frans Rosén
 
Eventlog
EventlogEventlog
Eventlog
Shashi Kanth
 
Red Team Apocalypse (RVAsec Edition)
Red Team Apocalypse (RVAsec Edition)Red Team Apocalypse (RVAsec Edition)
Red Team Apocalypse (RVAsec Edition)
Beau Bullock
 
Введение в Scrum
Введение в ScrumВведение в Scrum
Введение в ScrumSergey Semyonov
 
The Rise of the Purple Team
The Rise of the Purple TeamThe Rise of the Purple Team
The Rise of the Purple Team
Priyanka Aash
 
게임 운영에 필요한 로그성 데이터들에 대하여
게임 운영에 필요한 로그성 데이터들에 대하여게임 운영에 필요한 로그성 데이터들에 대하여
게임 운영에 필요한 로그성 데이터들에 대하여
iFunFactory Inc.
 
Threat Hunting with Splunk
Threat Hunting with SplunkThreat Hunting with Splunk
Threat Hunting with Splunk
Splunk
 
[2013 CodeEngn Conference 09] Park.Sam - 게임 해킹툴의 변칙적 공격 기법 분석
[2013 CodeEngn Conference 09] Park.Sam - 게임 해킹툴의 변칙적 공격 기법 분석[2013 CodeEngn Conference 09] Park.Sam - 게임 해킹툴의 변칙적 공격 기법 분석
[2013 CodeEngn Conference 09] Park.Sam - 게임 해킹툴의 변칙적 공격 기법 분석
GangSeok Lee
 
IDA Vulnerabilities and Bug Bounty  by Masaaki Chida
IDA Vulnerabilities and Bug Bounty  by Masaaki ChidaIDA Vulnerabilities and Bug Bounty  by Masaaki Chida
IDA Vulnerabilities and Bug Bounty  by Masaaki Chida
CODE BLUE
 
객체지향 개념 (쫌 아는체 하기)
객체지향 개념 (쫌 아는체 하기)객체지향 개념 (쫌 아는체 하기)
객체지향 개념 (쫌 아는체 하기)
Seung-June Lee
 

What's hot (20)

The innerHTML Apocalypse
The innerHTML ApocalypseThe innerHTML Apocalypse
The innerHTML Apocalypse
 
Behavior Driven Development with SpecFlow
Behavior Driven Development with SpecFlowBehavior Driven Development with SpecFlow
Behavior Driven Development with SpecFlow
 
게임 프레임워크의 아키텍쳐와 디자인 패턴
게임 프레임워크의 아키텍쳐와 디자인 패턴게임 프레임워크의 아키텍쳐와 디자인 패턴
게임 프레임워크의 아키텍쳐와 디자인 패턴
 
Workshop 101: Concebendo produtos de forma ágil (e divertida) - Scrum Gathe...
  Workshop 101: Concebendo produtos de forma ágil (e divertida) - Scrum Gathe...  Workshop 101: Concebendo produtos de forma ágil (e divertida) - Scrum Gathe...
Workshop 101: Concebendo produtos de forma ágil (e divertida) - Scrum Gathe...
 
온라인 게임에서 사례로 살펴보는 디버깅 in NDC2010
온라인 게임에서 사례로 살펴보는 디버깅 in NDC2010온라인 게임에서 사례로 살펴보는 디버깅 in NDC2010
온라인 게임에서 사례로 살펴보는 디버깅 in NDC2010
 
How to Hunt for Lateral Movement on Your Network
How to Hunt for Lateral Movement on Your NetworkHow to Hunt for Lateral Movement on Your Network
How to Hunt for Lateral Movement on Your Network
 
"CERT Secure Coding Standards" by Dr. Mark Sherman
"CERT Secure Coding Standards" by Dr. Mark Sherman"CERT Secure Coding Standards" by Dr. Mark Sherman
"CERT Secure Coding Standards" by Dr. Mark Sherman
 
게임 기획과 Oop
게임 기획과 Oop게임 기획과 Oop
게임 기획과 Oop
 
Designing with Capabilities
Designing with CapabilitiesDesigning with Capabilities
Designing with Capabilities
 
Introducing ArTHIR - ATT&CK Remote Threat Hunting Incident Response Windows tool
Introducing ArTHIR - ATT&CK Remote Threat Hunting Incident Response Windows toolIntroducing ArTHIR - ATT&CK Remote Threat Hunting Incident Response Windows tool
Introducing ArTHIR - ATT&CK Remote Threat Hunting Incident Response Windows tool
 
The Secret Life of a Bug Bounty Hunter – Frans Rosén @ Security Fest 2016
The Secret Life of a Bug Bounty Hunter – Frans Rosén @ Security Fest 2016The Secret Life of a Bug Bounty Hunter – Frans Rosén @ Security Fest 2016
The Secret Life of a Bug Bounty Hunter – Frans Rosén @ Security Fest 2016
 
Eventlog
EventlogEventlog
Eventlog
 
Red Team Apocalypse (RVAsec Edition)
Red Team Apocalypse (RVAsec Edition)Red Team Apocalypse (RVAsec Edition)
Red Team Apocalypse (RVAsec Edition)
 
Введение в Scrum
Введение в ScrumВведение в Scrum
Введение в Scrum
 
The Rise of the Purple Team
The Rise of the Purple TeamThe Rise of the Purple Team
The Rise of the Purple Team
 
게임 운영에 필요한 로그성 데이터들에 대하여
게임 운영에 필요한 로그성 데이터들에 대하여게임 운영에 필요한 로그성 데이터들에 대하여
게임 운영에 필요한 로그성 데이터들에 대하여
 
Threat Hunting with Splunk
Threat Hunting with SplunkThreat Hunting with Splunk
Threat Hunting with Splunk
 
[2013 CodeEngn Conference 09] Park.Sam - 게임 해킹툴의 변칙적 공격 기법 분석
[2013 CodeEngn Conference 09] Park.Sam - 게임 해킹툴의 변칙적 공격 기법 분석[2013 CodeEngn Conference 09] Park.Sam - 게임 해킹툴의 변칙적 공격 기법 분석
[2013 CodeEngn Conference 09] Park.Sam - 게임 해킹툴의 변칙적 공격 기법 분석
 
IDA Vulnerabilities and Bug Bounty  by Masaaki Chida
IDA Vulnerabilities and Bug Bounty  by Masaaki ChidaIDA Vulnerabilities and Bug Bounty  by Masaaki Chida
IDA Vulnerabilities and Bug Bounty  by Masaaki Chida
 
객체지향 개념 (쫌 아는체 하기)
객체지향 개념 (쫌 아는체 하기)객체지향 개념 (쫌 아는체 하기)
객체지향 개념 (쫌 아는체 하기)
 

More from Hsin-Hao Tang

Lighthouse
LighthouseLighthouse
Lighthouse
Hsin-Hao Tang
 
Start your app the better way with Styled System
Start your app the better way with Styled SystemStart your app the better way with Styled System
Start your app the better way with Styled System
Hsin-Hao Tang
 
Build a better UI component library with Styled System
Build a better UI component library with Styled SystemBuild a better UI component library with Styled System
Build a better UI component library with Styled System
Hsin-Hao Tang
 
Nightwatch101
Nightwatch101Nightwatch101
Nightwatch101
Hsin-Hao Tang
 
單元測試:Mocha、Chai 和 Sinon
單元測試:Mocha、Chai 和 Sinon單元測試:Mocha、Chai 和 Sinon
單元測試:Mocha、Chai 和 Sinon
Hsin-Hao Tang
 
SEO Basics
SEO BasicsSEO Basics
SEO Basics
Hsin-Hao Tang
 

More from Hsin-Hao Tang (6)

Lighthouse
LighthouseLighthouse
Lighthouse
 
Start your app the better way with Styled System
Start your app the better way with Styled SystemStart your app the better way with Styled System
Start your app the better way with Styled System
 
Build a better UI component library with Styled System
Build a better UI component library with Styled SystemBuild a better UI component library with Styled System
Build a better UI component library with Styled System
 
Nightwatch101
Nightwatch101Nightwatch101
Nightwatch101
 
單元測試:Mocha、Chai 和 Sinon
單元測試:Mocha、Chai 和 Sinon單元測試:Mocha、Chai 和 Sinon
單元測試:Mocha、Chai 和 Sinon
 
SEO Basics
SEO BasicsSEO Basics
SEO Basics
 

利用 JavaScript 實作瀏覽器推播通知