2015-05-08 56 views
8

我希望能夠向用戶發送桌面通知(不在瀏覽器窗口內),我知道我必須讓用戶選擇以chrome打開對話框問題。HTML5桌面通知(最好用Angular)

我正在使用MEAN堆棧,並在前端使用browserify/bowserify,並且我可以在客戶端使用bower或npm包。我使用CommonJS模式,所以如果解決方案或示例採用這種格式,或者更容易轉換爲該格式,它會幫助我理解它。

什麼是最好的方法來解決這個問題,是否有一個Angular/React插件(我必須承認我找不到自己)。

我只需要一次顯示一個,我很樂意展示最新的並自動刪除那裏的任何東西,最好是一個跨瀏覽器解決方案(它可以是最新版本,如果需要的話)。

任何想法歡迎。

回答

10

澄清我的意思是前端解決方案。但是我遇到了這個,我已經編輯和改進了。

這是純粹的JavaScript爲了清晰起見,我將使用我自己的一些方法將它鏈接到Angular。

document.addEventListener('DOMContentLoaded',function(){ 
document.getElementById('notifyButton').addEventListener('click',function(){ 

    if(! ('Notification' in window)){ 
     console.log('Web Notification not supported'); 
     return; 
    } 

    Notification.requestPermission(function(permission){ 
      var notification = new Notification("Title",{body:'HTML5 Web Notification API',icon:'http://i.stack.imgur.com/Jzjhz.png?s=48&g=1', dir:'auto'}); 
      setTimeout(function(){ 
       notification.close(); 
      },3000); 
     }); 
    }); 
}); 
0

使用node notifier模塊。當然,它只是服務器端。

var notifier = require('node-notifier'); 
notifier.notify({ 
    title: 'Title', 
    message: 'Message' 
}, function(err, resp) { 
    if(err) { 
     console.log(err); 
    } 
});