I want to create a javascript which checks if a new rss feed has been updated and display a notification using phonegaps notification methods. 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 ?
And even after I开发者_JAVA技巧 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.
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()
right after your code -
api += "&output=json_xml"
精彩评论