My app is using Pubnub and I'm retrieving chat history with the following code:
$scope.populateMessages = function() {
// Populate message history using jsapi (optional)
PubNub.jsapi.history({
channel: $scope.channel,
count: 15,
callback: function(payload) {
payload[0].forEach(function(message) {
$scope.messages.push(message);
//message timetoken
console.log(message.pn_apns.aps.timetoken);
});
$scope.messagesStatus = false;
$scope.connectionStatus = false;
$scope.$apply();
}
});
};
When my app is coming from the background I want check for new messages again. But when I run the code above I get duplicates. If I clear $scope.messages before I run the code above it is working only there is a load time.
So Wat I'm currently trying to do is check the current list of messages by retrieving the time tokens of the existing messages:
var div = $('.list');
var divs = $('.item .chatMessage');
var divArray = [];
for (var i = 0; i < divs.length; i += 1) {
divArray.push(divs.attr('timetoken'));
}
//list of existing messages timetokens
console.log(divArray);
And I want to compare them with the new messages retrieved with the populateMessages function above. If a message timetoken already exist do nothing, if the timetoken is not existing add the message ($scope.messages.push(message);)