[英]How can I push an RSS feed to HTML5 notifications?
我想將RSS feed更新推送到HTML5桌面通知,如果用戶在Chrome中打開我的網站,該通知將被接收。
我認為我需要執行以下操作(比我在概述中解釋的要聰明得多的人)-創建一個服務器端組件,該組件將輪詢提要以獲取更新; 然后將它們存儲在數據庫中(?)。 然后,客戶端組件將檢查更新並使用HTML5通知API顯示更新。
有沒有人確認這一點,並且如果可能的話,請幫助我提供更多細節,以便我追蹤完成這項工作所需的各個方面? 非常感激。
我將假設您正在使用jQuery,並且您不介意使用插件。 在這種情況下,我們將使用jFeed插件來解析RSS代碼。
// Desktop notifications are only available on WebKit browsers, for now, so only carry out
// notifications if the API is available.
if (window.webkitNotifications) {
// Just a piece of data to determine whether 1) the page just loaded, and 2) there are any
// new elements in the feed.
var lastTime = null;
// This is to check if you have permissions to display notifications. Usually, the
// checkPermissions method only returns a number. 0 being that you don't have permission
// to display notifications, yet.
if (window.webkitNotifications.checkPermissions() <= 0) {
// If you don't have permission, then get the permission from the user.
window.webkitNotifications.requestPermission(callback);
}
// The code below will run every thirty seconds.
setInterval(function () {
// What we want to do here is carry out an AJAX request to check for changes in the RSS
// feed only if we have permission to display notifications. Otherwise, what's the point
// of checking for changes if the user doesn't want to know?
if (window.webkitNotifications.checkPermissions() > 0) {
$.jFeed({
url: 'urltofeeds',
success: function (feed) {
// Looks at the latest item's time, and checks to see if it's any different
// than what we have in memory.
if (lastTime !== feed.items[0].updated) {
// If so, determine whether we recorded the time, or not.
if (lastTime !== null) {
// If we did record the time, that means there are new content from
// the last time we checked.
window.webkitNotifications()
}
// Update the last time.
lastTime = feed.items[0].updated;
}
}
});
}
}, 30000);
}
我認為我需要執行以下操作(比我在概述中解釋的要聰明得多的人)-創建一個服務器端組件,該組件將輪詢提要以獲取更新; 然后將它們存儲在數據庫中(?)。
聽起來您的朋友描述了長期投票 。 對於像博客這樣簡單的事情,這是完美主義者的方法。
簡單的輪詢將是一回事。 區別在於,通知只會在每個輪詢間隔顯示,而不是即時顯示。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.