The problem has been solved by using Push Notifications instead of Local Notifications. Parse.com itself offers a feature called Cloud Code, where you can write a task to read the feed from time to time and fire push Notifications. Follow the code commented below:
Remembering that the code is made in Javascript, referencing libraries Xmlreader and SAX
var xmlreader = require('cloud/xmlreader.js');
var url = "http://www.zelda.com.br/rss.xml";
function SavePost(title, link){
var PostClass = Parse.Object.extend("Post");
var post = new PostClass();
post.set("title", title);
post.set("link", link);
post.save();
}
function SendPush(title, link){
var query = new Parse.Query(Parse.Installation);
Parse.Push.send({
where: query,
data: {
url: link,
alert: title,
sound: "default"
}
}, {
success: function() {
SavePost(title, link);
},
error: function(error) {
console.log("Error sending push: " + error);
}
});
}
Parse.Cloud.job("fetchPosts", function(request, response) {
Parse.Cloud.httpRequest({
url: url,
success: function(httpResponse) {
var responseText = httpResponse.text;
xmlreader.read(responseText, function (err, res){
var newPost = res.rss.channel.item;
var title = newPost.array[0].title.text();
var link = newPost.array[0].link.text();
var PostClass = Parse.Object.extend("Post");
var query = new Parse.Query(PostClass);
query.equalTo("link", link);
query.find({
success: function(results) {
if (results.length == 0){
SendPush(title, link);
} else {
response.error("Post already pushed");
}
}
});
});
},
error: function(httpResponse) {
console.error('Request failed with response code ' + httpResponse.status);
response.error("Error fetching posts from feed");
}
});
});
The requests from time to time would be around how long? Is the app in a finished state? If possible put the code in which you perform these questions
– Gian
@Gian, time would be as little as possible... I really appreciate the help, but since I needed something faster, I found a way to solve this problem using Push Notifications with Parse.com even... I will post the code explaining how I did, to leave future information :)
– Alliston Carlos