Thanks for reading in advance.
My app has chat and notification functionality which relies on both the sinch javascript sdk and the pushwoosh-cordova-plugin.
However, the eventlisteners meant for receiving the sinch instant messages and pushwoosh notifications seem to not be firing on the receiving device. My implementation doesn’t stray too far from the official docs’ example, or at least I don’t think so.
For Sinch, the messages are sent successfully, and the Sinch dashboard confirms this.
This is the instant messaging service I have:
angular.module("cross").factory("IM",
function($rootScope, $ajax, $ionicHistory, $ionicPopup, Notification, Users, Quote){
var sessionName, sinchClient, msgClient, verifyClient,
sinchSessionObj, sinchUserObj;
function init(){
sinchClient = new SinchClient({
applicationKey: 'a07e38d2-d49c-4df1-bf8b-e9afb673a02a',
capabilities: { messaging: true },
startActiveConnection: true,
supportActiveConnection: true,
onLogMessage: function(message) {
console.log("Sinch: " + message.message + " Progress: " + message.progress);
// shut up
}
});
sinchUserObj = {
username: Users.getCurrent().id,
password: "cross"+Users.getCurrent().id
};
sessionName = 'sinchSession-' + Users.getCurrent().id;
}
function handleError(error) {
console.warn(error);
}
function onIncomingMessage(message){
$rootScope.$broadcast('new_msg', message);
console.info("New sinch msg!");
console.info(message);
if($ionicHistory.currentStateName() !== 'conversation')
Users.getByID(message.senderId, function(user){
var msg_text = message.textBody,
msg_headers = JSON.parse(message.headers);
if (msg_headers.hasOwnProperty("type")){
if (msg_headers.type === 'quote_response')
msg_text = "Quote";
else if (msg_headers.type === 'quote_request')
msg_text = "Quote Request";
else if (msg_headers.type === 'project')
msg_text = "Your quote has been accepted!"
} else {
msg_text="You got a new message from "+sender_id;
}
Notification.send(
user.first_name + " " + user.last_name,
msg_text,
user.profile_picture,
msg_headers
);
});
}
function createUser(){
sinchClient.newUser(sinchUserObj, function(ticket) {
sinchClient.start(ticket).then( function() {
console.log("createUser() successful");
Users.setProp("sinchData", sinchClient.getSession());
msgClient = sinchClient.getMessageClient();
msgClient.addEventListener({
onIncomingMessage: onIncomingMessage
});
Users.post();
})
.fail(handleError);
})
.fail(handleError);
}
function login(){
init();
sinchClient.start(sinchUserObj).then(function() {
console.info("logged in successfully");
console.info("Sinch user session: ");
console.info(sinchClient.getSession());
var missing_sinch_credentials = !Users.getCurrent().sinchData || !Users.getCurrent().sinchData.sessionId || !Users.getCurrent().sinchData.sessionSecret;
Users.setProp("sinchData", sinchClient.getSession());
msgClient = sinchClient.getMessageClient();
msgClient.addEventListener({
onIncomingMessage: onIncomingMessage
});
console.info(msgClient);
if(missing_sinch_credentials)
Users.post();
})
.fail(function(e){
// login failed = user doesnt exist
console.warn(e);
createUser();
});
}
function findConversation(user1, user2) {
return $ajax.get('/chats/find_conv/'+user1+'/'+user2);
}
function getConversationByID(convo_id) {
return $ajax.get('/chats/conv/'+convo_id);
}
function getConversationMessages(convo_id, batch_size, offset) {
return $ajax.get('/chats/conv/'+convo_id + '/msgs/' + batch_size + "-" + offset);
}
function getConversations(){
return $ajax.get('/chats');
}
function send(convo_id, recipient, message, msg_type, callback) {
var messageObject = {
author_id: Users.getCurrent().id,
recipient_id: recipient,
message: message
},
headers = {"convo_id": convo_id};
if (msg_type)
headers.type = messageObject['type'] = msg_type;
msgClient.send(
msgClient.newMessage(
[recipient.toString()],
message,
JSON.stringify(headers)
)
).then(function(message){
console.log("message recieved");
}).fail(function(error){
console.log(error);
});
//Saves the post to backend
$ajax.post('/chats/conv/' + convo_id, messageObject);
if(callback)
callback();
}
function requestQuote(convo_id, seller_id, callback) {
send(convo_id, seller_id, "quote_request", "quote_request", callback);
}
function sendQuote(convo_id, buyer_id, quoteObject, callback) {
console.log(convo_id, buyer_id, quoteObject, callback);
Quote.post(quoteObject).then(function(success){
console.log("posted quote. result:");
console.info(success);
var quote_id = success.data;
send(convo_id, buyer_id, quote_id.toString(), "quote_response");
if(callback)
callback(quote_id);
});
}
function toggleBlockUser(convo_id, user_id) {
return $ajax.post('/chats/conv/' + convo_id, {'toggleBlock': user_id});
}
function toggleFavoriteUser(convo_id, user_id) {
return $ajax.post('/chats/conv/' + convo_id, {'toggleFavorite': user_id});
}
function toggleHideConversation(convo_id, for_user_id) {
return $ajax.post('/chats/conv/' + convo_id, {'toggleHideFor': for_user_id });
}
return {
login: login,
send: send,
findConversation: findConversation,
getConversationByID: getConversationByID,
getConversations: getConversations,
getConversationMessages: getConversationMessages,
toggleHideConversation: toggleHideConversation,
toggleBlockUser: toggleBlockUser,
toggleFavoriteUser: toggleFavoriteUser,
sendQuote: sendQuote,
requestQuote: requestQuote,
session: sinchSessionObj,
}
});
Any ideas on how the eventlistener may not be firing off? I can provide more context if necessary. Any help would be greatly appreciated.