Notification


Posted by TempuraEngineer on 2022-02-20

Notification是甚麼

Notification是Notification API的interface,用於設定和展示(傳送)電腦桌面的推播

使用

建立

const notificationA = new Notification('this is title', {
    body: 'qqq123',
    icon:'https://images.unsplash.com/photo-1543852786-1cf6624b9987?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=387&q=80'
});

要求推播許可

function notifyMe(){
    // 如果 window底下沒有Notification的話,直接return
    // 例如iPhone不支援Notification就可以用
    if(!('Notification' in window)) return; 

    // requestPermission()是靜態方法,只能透過Notification本身呼叫
    Notification.requestPermission().then((res) => { // 要求通知權限
        if(res === 'granted'){
            new Notification('hello');
        }
    })
}    

notifyMe();


事件

  • click
const notificationA = new Notification('this is title', {body:'this is body'});

notificationA.addEventListener('click', () => {
    console.log('A is clicked');    
})
  • close (關掉/移除收到的推播)
notificationA.close();
notificationA.__proto__.close();
  • error (當出現error導致notification無法出現時)

  • show (當notification出現時)


常見的Notification物件屬性

屬性 說明 補充
title 標題
body 內文
icon icon的網址
image image的網址
data Notification的資料,放甚麼都可以
permission 通知權限 default(詢問)、granted、denied
silent 勿擾(無聲音、震動) 無視電腦通知設定是否開勿擾模式


VoIP notification是甚麼?

VoIP通知是一種不會跳警告彈窗或有聲音的背景推播,被用於叫醒App,並傳來電通知
App會顯示本地推播(應用程式本身發起,通常只會有一個裝置收到),當使用者滑過通知時通話就會被建立

VoIP push notification is a background push that generates no alert or sound, and used to wake up an app and pass it the information about the incoming call. The app should display a local notification to users, and the call is established when the user swipes the notification.


參考資料

MDN - Notification


#Notification #推播







Related Posts

如何用 TensorFlow object detection API 的 Single Shot MultiBox Detector 來做 hand detection

如何用 TensorFlow object detection API 的 Single Shot MultiBox Detector 來做 hand detection

給自己看的 JS 進階-Closure

給自己看的 JS 進階-Closure

瀏覽器渲染流程

瀏覽器渲染流程


Comments