[英]javascript to read new rss entry and create a notification
I want to create a javascript which checks if a new rss feed has been updated and display a notification using phonegaps notification methods.
我想创建一个JavaScript,检查新的rss feed是否已更新,并使用phonegaps通知方法显示通知。 http://docs.phonegap.com/phonegap_notification_notification.md.html#Notification
http://docs.phonegap.com/phonegap_notification_notification.md.html#Notification
var oldEntry="";
document.addEventListener("deviceready", onDeviceReady, false);
// PhoneGap is ready
//
function onDeviceReady() {
// Empty
}
// Show a custom alert
//
function sendRequest(){
getRss("http://rss.cnn.com/rss/cnn_latest.rss.xml");
setTimeout('showAlert(newEntry.title)',4000);
}
function showAlert(data) {
var st = randomString();
if(oldEntry==""){
oldEntry = data;
navigator.notification.alert(
data, // message
'New Rss Entry', // title
'New Rss Entry'
);
}
else {
if(oldEntry!=data){
navigator.notification.alert(
data, // message
'New Rss Entry', // title
'New Rss Entry'
);
oldEntry = data;
}
}
setTimeout('sendRequest()',8000);
}
function randomString() {
var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
var string_length = 8;
var randomstring = '';
for (var i=0; i<string_length; i++) {
var rnum = Math.floor(Math.random() * chars.length);
randomstring += chars.substring(rnum,rnum+1);
}
return randomstring;
}
sendRequest();
Right now both new along with the old Rss title pops up as notification, is it possible to restrict the function to display only the latest Rss title ? 现在,新消息和旧的Rss标题都会作为通知弹出,是否可以限制该功能以仅显示最新的Rss标题?
And even after I dismiss both the notification after 8000 ms they both appear again, but I want to check if there are any new notifications and display only them, not the one s the user has dismissed. 而且即使在8000毫秒后我都关闭了这两个通知之后,它们都再次出现,但是我想检查是否有任何新的通知并仅显示它们,而不是用户已关闭的那些通知。
This is for getting the newentry. 这是为了获得新条目。
var newEntry="";
function getRss(url){
if(url == null) return false;
// Create Google Feed API address
var api = "http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&callback=?&q="
+ encodeURIComponent(url);
api += "&output=json_xml"
// Send request
$.getJSON(api, function(data){
// Check for error
if (data.responseStatus == 200) {
// Process the feeds
_process(data.responseData);
}
else {
};
});
}
var _process = function(data) {
// Get JSON feed data
var feeds = data.feed;
if (!feeds) {
return false;
}
// Get XML data for media (parseXML not used as requires 1.5+)
var xml = getXMLDocument(data.xmlString);
var xmlEntries = xml.getElementsByTagName('item');
var entry = feeds.entries[0];
newEntry = entry;
};
there could be many problems. 可能有很多问题。 I think maybe you are hitting a caching error.
我认为您可能遇到了缓存错误。 so try adding this -
因此,请尝试添加-
api += "&tempVar=" + Math.random() api + =“&tempVar =” + Math.random()
right after your code - 在您的代码之后-
api += "&output=json_xml" api + =“&output = json_xml”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.