QuickBlox Chat history - javascript

I have saved the chat history by using following code in javascript:
var message = {
body: text,
type: 'chat',
extension: {
nick: chatUser.login,
save_to_history: 1
}
};
Now my problem is how can I get the whole chat history of two users in my chat window?

To get a chat history for particular dialog use this request, You must have use dialog id of private dialog.
var dialogId = "xxxxxxxxxxxxxxxxxxxxxxxxx";
var params = {chat_dialog_id: dialogId, sort_desc: 'date_sent', limit: 100, skip: 0};
QB.chat.message.list(params, function(err, messages) {
if (messages) {
}else{
console.log(err);
}
});

Related

Firebase messaging for web. Multiple tabs notifications

I'm using Firebase Messaging for browser notifications. Problem is when multiple tabs are open. Notification for every tab is shown. Adding tag to options didn't help
firebase.initializeApp(constants.CONFIG);
const messaging = firebase.messaging();
messaging.setBackgroundMessageHandler(({ data }) => {
const options = {
body: data.body,
data: { link: data.link, },
icon: data.icon,
};
const backgroundNotification = self.registration.showNotification(data.title, options);
return backgroundNotification;
});
I need only one notification to be displayed even when many tabs with same app are open
Comment this line of code
const backgroundNotification = self.registration.showNotification(data.title, options);
and use instead
new Notification(notification.title, {
body : notification.body,
tag : notification.title,
icon : notification.icon
});
Note that - Tag is unique, If you have 2 tabs and you are sending a push
with name title: title1 and title1, it will show the push only ones.
Example 3.5:
https://notifications.spec.whatwg.org/

Quickblox WebRTC issue: onCallListener is not working inside my react app

So, basically I'm trying to receive a call from provider to my app. For that purpose Quickblox gives us a listener to receive the upcoming calls onCallListener. So here is my code snippet that should work but doesn't.
const calleesIds = [4104]
const sessionType = QB.webrtc.CallType.VIDEO
const additionalOptions = {}
let callSession = QB.webrtc.createNewSession(calleesIds, sessionType, null, additionalOptions)
console.log(callSession, "SESSION")
const mediaParams = {
audio: true,
video: true,
options: {
muted: true,
mirror: true,
},
elemId: "myVideoStream"
}
QB.webrtc.onCallListener = function(session: any, extension: object) {
callSession = session
console.log('asdasd')
// if you are going to take a call
session.getUserMedia(mediaParams, function (error: object, stream: object) {
if (error) {
console.error(error)
} else {
session.accept(extension)
session.attachMediaStream("videoStream", stream)
}
})
}
P.S. I also integrated chat which works perfect!
Found the solution by myself! Whenever you create a user and dialog id, search that user in the quickblox dashboard by the dialogId and change its settings: you will see that userId and providerId is the same which is wrong. So put your userId in the userId field and save that. After that you video calling listeners will work fine!)
P. S. also in the backend replace provider token with user token.

Getting messages in chat history to display in messenger Pubnub

I have come to post this question after 2 days of torture not being able to understand how I can actually publish the historic messages stored on my pubnub storage account. To try and understand it at its most basic I have made a chat app and used the history function as described in the SDK but still every time I refresh the page the messages are lost. I have tried the backfill and the restore attributes in subscribe with no luck. All I want to do is click refresh on chrome and see the messages still there.
<div><input id=input placeholder=you-chat-here /></div>
Chat Output
<div id=box></div>
<script src="https://cdn.pubnub.com/sdk/javascript/pubnub.4.4.0.min.js"></script>
<script>(function(){
var pubnub = new PubNub({ publishKey : 'demo', subscribeKey : 'demo' });
function $(id) { return document.getElementById(id); }
var box = $('box'), input = $('input'), channel = 'chat';
pubnub.addListener({
message: function(obj) {
box.innerHTML = (''+obj.message).replace( /[<>]/g, '' ) + '<br>' + box.innerHTML
}});
pubnub.history({
channel: 'chat',
reverse: true, // Setting to true will traverse the time line in reverse starting with the oldest message first.
count: 100, // how many items to fetch
callback : function(msgs) {
pubnub.each( msgs[0], chat );
}
},
function (status, response) {
// handle status, response
console.log("messages successfully retreived")
});
pubnub.subscribe({channels:[channel],
restore: true,
backfill: true,
ssl: true});
input.addEventListener('keyup', function(e) {
if ((e.keyCode || e.charCode) === 13) {
pubnub.publish({channel : channel, message : input.value,x : (input.value='')});
}
});
})();
</script>
</body>
EDIT: updated link that was broken. New version of history function is called fetchMessages.
I think your history code is not correct. No need for the callback as your code response will be in the function argument. This example is from the JavaScript SDK docs.
// deprecated function
pubnub.history(
{
channel: 'chat',
},
function (status, response) {
var msgs = response.messages;
if (msgs != undefined && msgs.length > 0) {
// if msgs were retrieved, do something useful
console.log(msgs);
}
}
);
// latest function (response output format has changed)
pubnub.fetchMessages(
{
channels: ['chat']
},
(status, response) => {
console.log(msgs);
}
);

Parse Server Push Notification to Specific user issue

I need to send a Push notification after a class is updated by the user, the push notification is working from the Dashboard.
I added this code to my Cloud Code, i can see the logs with the success message, but the notification is not being sent to the user, on the past notification, it shows that was sent, but the audience is Everyone, but pushs sent is 0, this is my Cloud Code
Parse.Cloud.afterSave("Class", function(request, response) {
var inObject = request.object;
if (inObject.get("isLost")){
console.log("inObject is lost");
var targetUser = new Parse.User();
targetUser.id = inObject.get("UserID");
var query = new Parse.Query(Parse.Installation);
query.equalTo("user", targetUser);
Parse.Push.send({
where: query,
data: {
alert: "InObject",
badge: "Increment",
message: "Testando"
}
}, {
success: function() {
// Push was successful
console.log("success");
},
error: function(error) {
// Handle error
console.log("error: "+error.code+" message "+error.message);
throw "Got an error " + error.code + " : " + error.message;
},
useMasterKey: true
});
}
});
I tried to do the double query as the post link, but no success, i loged the user ID and is the same as show on my Installation in the Dashboard.
If i click the Push notification for more details this is the Target that it shows:
Target details from push
Edit
Save code for that class(it's in java):
ParseObject object = new ParseObject(Class.class.getSimpleName());
object.put(NAME, getName());
object.put(UUID, getUUID());
object.put(ICON_ID, getIconID());
object.put(ADDRESS, getAddress());
object.put(ID1, iD1.toString());
object.put(ID2, iD2.toString());
object.put(ID3, iD3.toString());
object.put(IS_LOST, getIsLost());
object.put(USER, parseUserID);
object.put(DISTANCE, getDistance());
object.put(LATITUDE, getLatitude());
object.put(LONGITUDE, getLongitude());
object.saveEventually();
The parseID is being generated by the ParseUser.getCurrentUser().getObjectID();
So, after trying this in a lot of ways, i ended up creating a relation between my object and the ParseUser object, this solved the issue, still i don't know why can't i just save the UserID, but my problem is solved now.

Quickblox one to one chat history not working

I am using Javascript SDK for 1-1 chat in Quickblox, but somehow I am not able to store the chat history.
I am following this link.
var message = {
body: text,
type: 'chat',
extension: {
nick: chatUser.email,
// token from session is set on window object
token: window.token,
// MyChat is a custom class_name
class_name: 'MyChat'
}
};
I am passing the class_name and token since I saw the android sdk following the same pattern.
private Message createMsgWithAdditionalInfo(int userId, String body, Map<?, ?> addinfoParams){
Message message = new Message(QBChatUtils.getChatLoginFull(userId), Message.Type.chat);
String addInfo = ToStringHelper.toString(addinfoParams, "", Consts.ESCAPED_AMPERSAND);
//
MessageExtension messageExtension = new MessageExtension(Consts.QB_INFO, "");
try {
messageExtension.setValue("token", QBAuth.getBaseService().getToken());
messageExtension.setValue("class_name", "ChatMessage");
messageExtension.setValue("additional", addInfo);
} catch (BaseServiceException e) {
e.printStackTrace();
}
message.addExtension(messageExtension);
message.setBody(body);
return message;
}
Also in instructions I see this.
<message id="123" type="chat" to="291-92#chat.quickblox.com" from="292-92#chat.quickblox.com"><body>Hi there</body><quickblox xmlns=""><token>848d4bf336d99532deff6bf7c8bb4b7e7b1a71f9</token><class_name>ChatMessage</class_name></quickblox></message>
Here also I see token & class passed so I am guessing how to I structure in my message object so that I get it to work.
The way I have created chatService is this.
chatService = new QBChat(params);
// to send message I am using sendMessage function
// message object is same as defined above.
chatService.sendMessage(recipientID, message);
This is an old and deprecated method to store chat history
Look at this guide http://quickblox.com/developers/Chat#Server-side_chat_history
var msg = {
body: "Hey",
extension: {
save_to_history: 1
},
senderId: currentUser.id,
};
You have to use 'save_to_history' to store a message
You can use this branch as a basis
https://github.com/QuickBlox/quickblox-javascript-sdk/tree/develop.chat/samples/chat

Categories