【问题标题】:How can I push an RSS feed to HTML5 notifications?如何将 RSS 提要推送到 HTML5 通知?
【发布时间】:2012-07-21 16:00:08
【问题描述】:

我想将 RSS 提要更新推送到 HTML5 桌面通知,如果用户在 Chrome 中打开我的网站,他们会收到这些通知。

我认为我需要执行以下操作(比我聪明得多的人在概述中解释了这一点) - 创建一个服务器端组件,它将轮询提要以获取更新;然后也许将它们存储在数据库中(?)。然后客户端组件将检查更新并使用 HTML5 通知 API 显示它们。

有没有人证实这一点,如果可能的话,请帮助我提供更多细节,以便我可以找到完成这项工作所需的各个方面?非常感激。

【问题讨论】:

  • RSS 提要来自哪里?
  • 嗨,我自己的 Wordpress 博客...干杯

标签: javascript html api rss websocket


【解决方案1】:

我假设您使用的是 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);
}

我认为我需要执行以下操作(比我聪明得多的人在概述中解释了这一点) - 创建一个服务器端组件,它将轮询提要以获取更新;然后也许将它们存储在数据库中(?)。

听起来你的朋友描述了long-polling。对于像博客这样简单的事情,这是完美主义者的做法。

简单的轮询也是一样的。不同之处在于,通知只会在每个轮询间隔显示,而不是立即显示。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-09
    • 2015-05-10
    • 1970-01-01
    • 1970-01-01
    • 2010-09-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多