Nearby Connections: unable to send payload to more than one device - android

I am unable to create a group chat where more than 2 (up to 15) nearby android users are able to join a chat room.
startMeshNetwork() starts advertising the connection and discovering.
This is called in onCreate(), as well as in the callback for a successful connection. This is so that the device will continue to join all nearby devices.
public void startMeshNetwork(){
Nearby.getConnectionsClient(this)
.startAdvertising(
/* endpointName= */ "Name here",
/* serviceId= */ "ID here",
mConnectionLifecycleCallback,
new AdvertisingOptions(com.google.android.gms.nearby.connection.Strategy.P2P_CLUSTER));
Nearby.getConnectionsClient(this)
.startDiscovery(
/* serviceId= */ "ID here",
new EndpointDiscoveryCallback() {
#Override
public void onEndpointFound(String endpointId, DiscoveredEndpointInfo info) { Nearby.getConnectionsClient(getApplicationContext())
.requestConnection(
/* endpointName= */ "Name here",
endpointId,
mConnectionLifecycleCallback);
endpoints.add(endpointId);
}
#Override
public void onEndpointLost(String endpointId) {
startMeshNetwork();
}
},
new DiscoveryOptions(com.google.android.gms.nearby.connection.Strategy.P2P_CLUSTER));
}
Code segment that sends payloads to all connected devices
Payload payload = Payload.fromBytes(pendingmessage.getBytes());
for(String endpointId:endpoints)
Nearby.getConnectionsClient(getApplicationContext()).sendPayload(endpointId, payload);
Connections are being formed with this method, however, the payload sending is inconsistent and is only sent to one device.

Related

How can a chat application user once online again get the messages that were sent to them when they were offline

I'm developing an android chat app, using Node Js and redis to stock messages and user information. I'm using socket io for communication, and Room to store message in local database. When the user is offline, I want them to receive their messages once online again. My problem is, that when user A is offline, and user B send him many messages (let's say for instance 5 messages ), when user A is online again, he only receives the first message, and the last message 4 times. Here is what I'm doing, once the user receives a message, I update the message status in Redis from "Sent" to "Delivered". In the case when the user is offline, I stock their messages in Redis with the status of message "Sent", and once online again, I check their messages received for example from user B, if their status is "Sent", I deliver it to the user, and then it will be updted to "Delivered", as shown in the code below:
//On this event, we update the socket ID of the sender in Redis so they can
receive private messages from their contacts
socket.on('sender', (sender, destinat) =>{
tempId = socket.id;
senderId = sender;
users[sender] = sender;
users [destinat] = destinat;
//We also update the user status: online
client.hset(senderId, 'lastSeen', 'Now', function(reply){
console.log( senderId + reply);
});
//Stocking to the user socket id
client.hset(users[sender], 'tempId', tempId, function(){
console.log("Welcome " + sender);
console.log("Welcome " + tempId);
});
//Getting all the messages of the sender from users
//If the sender has any messages that hasn't received yet, they'll be sent
here
//the id of each message is compsed of two parts: the phone number of the
receiver, and the id of the message itself
(receiverPhoneNumber:idMessage)
client.keys(users [sender] + ':*', function(err, results) {
results.forEach(function(key) {
client.hgetall(key, function(err, reply){
if(err)
console.log(err);
else if(reply){
//Compare the message status: if not sent, deliver it to receiver once online
if('Sent'.localeCompare(reply.status) == 0 && users
[destinat].localeCompare(reply.fromUser) == 0) {
io.to(tempId).emit('message', reply);
}
}
});
});
});
});
After receiving messages from the server, I use Async to store them in Room Database and then display them to the user, as shown in the following code
And here is the AsyncTask Class:
class AddMessage extends AsyncTask<Void, Void, Void> {
#Override
protected Void doInBackground(Void... voids) {
//Creating a user account
m = new Message();
m.setContent( message );
m.setTime( time );
m.setUrl( url );
m.setStatus( status );
m.setFromUser( fromUser );
m.setToUser( toUser );
m.setUsername( receiver.getUsername() );
//adding to database
DatabaseClient.getInstance(getContext()).getAppDatabase()
.messageDao()
.insert(m);
return null;
}
#Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
Toast.makeText( getContext(), "Added!", Toast.LENGTH_SHORT ).show();
}
}
I've checked that messages are received from the server to the android app correctly (by re-sending the messages again to the server once delivered to the app). I believe the problem has something to do with AsyncTask, but I just can't figure it out, any help is greatly appreciated, thank you so much.
//When receving a message
socket.on("message", new Emitter.Listener() {
#Override
public void call(final Object... args) {
if(getActivity() != null){
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
JSONObject data = (JSONObject) args[0];
try {
//extract data from fired event
idMessage = data.getString( "idMessage" );
message = data.getString("message");
fromUser = data.getString( "fromUser" );
toUser = data.getString( "toUser" );
time = data.getString( "time" );
status = data.getString( "status" );
url = data.getString( "url" );
//Here we call asyncTask to Add it to Database
addMessage = new AddMessage();
addMessage.execute( );
//We emit this event to update the status of
the message to delivered
socket.emit( "sent", idMessage, userID );
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
}
});
I solved the problem by switching to RxJava instead of AsyncTask. The problem had something to do with AsyncTask as it sometimes influnces the chain of data, which is not the case with RxJava, as mentioned in this link : "Another issue with AsyncTasks is if you have more than one running at once. You have no guarantee what order they’ll complete in, resulting in complex logic to check when all the tasks have completed. Even worse is the assumption that one will finish before the other, until you hit an edge case that makes the first call slower, which makes them complete in the wrong order and undesired results."

Implement receiver status(read or typing) in messaging app using smack - Android

I am creating an instant messaging app in android using smack library and openfire as a server but i cannot implement the feature of the person with whom the current user is talking to. i.e. like when user read the message or when he starts typing.
Is there any way of achieving this using smack or other ?
For knowing which user is current you must implement your own in your logic. You must use Roster (contact list in xmpp servers) to get contacts of current user and save them in database or somewhere. Then create an activity to show contacts in a list. Each contact has a unique jid that can be distinguished from others with it. So with click on each contact, send it's object(include jid) to chat-activity. In chat-activity you must get previous messages from database or MAM(archived messages in server) and you can send a message to current contact(set contact jid as To).
To achieving delivery of message you must use this link. you can set request of it with this code:
Message message = … //make your stanza
DeliveryReceiptRequest.addTo(message); //add delivery request to message
connection.sendStanza(message); //send message
then you can be notified of delivery with this code:
private void setDelRecListener() {
DeliveryReceiptManager d = DeliveryReceiptManager.getInstanceFor(connection);
d.addReceiptReceivedListener(new ReceiptReceivedListener() {
#Override
public void onReceiptReceived(Jid fromJid, Jid toJid, String receiptId, Stanza receipt) {
Msg msg = F.getMsgBySid(receiptId);
if (msg == null)
return;
Boolean isUpdated = F.setMsgDelivered(msg);
Log.i("m/serv/UpdateDelivery", "for: " + receiptId + (isUpdated ? " Founded&Updated" : " NotFounded"));
if (isUpdated) {
BCTool.notifyPMDelivered(msg.id, msg.conv.frnd.getBareJid());
}
}
});
}
Keep in mind that every stanza has a sid(stanza id) and you must save each corresponding sid to message model in database when send is successful. This way you can detect which message delivery you got.
- For sending chat states like composing you can use this method:
public void sendChatState(String _jid, ChatState chatState) {
try {
Message msg = new Message();
msg.addExtension(new ChatStateExtension(chatState));
msg.setTo(JidCreate.bareFrom(_jid));
msg.setType(Message.Type.chat);
connection.sendStanza(msg);
Log.e("m/service", "ChatStateSent");
} catch (SmackException.NotConnectedException | InterruptedException | XmppStringprepException e) {
Log.e("m/service", "ChatState Not Sent: " + e.getMessage());
e.printStackTrace();
}
}
You must set a timer to prevent send composing in next 5Sec and reset timer when a character typed.
Consider reading this: ChatStateNotifications

OpenTok based application using both multiparty and broadcasting feature

The application that i m developing is mixture of both multiparty and broadcasting. There will be a single particular admin/host who's stream is subscribed by all the subscribers(Broadcasting). Both the host/publisher as well as all the subscribers will be able to see all the participants in the session(sort of multiparty). On event from the host/publisher such as button click etc, the publisher will now stop publishing his/her stream and all the subscriber will view the stream from a particular subscriber in the session that the host selects. Since my application uses android SDK and does not have moderator token. How can i succeed creating the application i desire.(
(note: I have already completed Basic audio,video chat )
Can you provide me with necessary methods and sample code to pull it off.
I used the following codes:
private HashMap<String, Stream> mStringStreams = new HashMap<String, Stream>();
#Override
public void onStreamReceived(Session session, final Stream stream) {
Log.d(LOG_TAG, "onStreamReceived: New Stream Received " + stream.getStreamId() + " in session: " + session.getSessionId()+" Stream name: "+stream.getName());
if(stream.getName().equalsIgnoreCase("Main_stream"))
{
mSubscriber = new Subscriber.Builder(SuscriberActivity.this, stream).build();
mSession.subscribe(mSubscriber);
mSubscriberViewContainer.addView(mSubscriber.getView());
}
if (mSubscribers.size() + 1 > MAX_NUM_SUBSCRIBERS ) {
Toast.makeText(this, "New subscriber ignored. MAX_NUM_SUBSCRIBERS limit reached.", Toast.LENGTH_LONG).show();
return;
}
if(stream.getName().compareTo("Main_stream")!=0)
{
final Subscriber subscriber = new Subscriber.Builder(SuscriberActivity.this, stream).build();
mStringStreams.put(stream.getStreamId().toString(), stream);
mSession.subscribe(subscriber);
mSubscribers.add(subscriber);
mSubscriberStreams.put(stream,subscriber);
int position = mSubscribers.size() - 1;
final int id = getResources().getIdentifier("subscriberview" + (new Integer(position)).toString(), "id", SuscriberActivity.this.getPackageName());
FrameLayout subscriberViewContainer = (FrameLayout) findViewById(id);
subscriber.setStyle(BaseVideoRenderer.STYLE_VIDEO_SCALE, BaseVideoRenderer.STYLE_VIDEO_FILL);
subscriberViewContainer.addView(subscriber.getView());
}
#Override
public void onSignalReceived(Session session, String s, String s1, Connection connection) {
stream = mSubscriberStreams.get(s1).getStream();
Stream stream= mStringStreams.get(s1);
Subscriber mSubscriber1 = new Subscriber.Builder(SuscriberActivity.this, stream).build();
mSubscriber1.setStyle(BaseVideoRenderer.STYLE_VIDEO_SCALE, BaseVideoRenderer.STYLE_VIDEO_FILL);
mSubscriberViewContainer.removeView(mPublisher.getView());
mSubscriberViewContainer.addView(mSubscriber1.getView().getRootView());
}
}
If I understand correctly, you're trying to enable participants to subscribe to a different stream when the publisher(main host) clicks on a button. To accomplish this, you can use the OpenTok Signaling API to send a signal with the streamId as the data to all participants so they can subscribe to that specific user.

MessageListener.onDistanceChanged not being called

I am publishing and subscribing to messages with Google Nearby and am receiving onFound and onLost properly, but am not receiving any callbacks on onDistanceChanged or onBleSignalChanged. I know that BLE signal changed is based on using BLE for discovery, but is there a similar limitation for onDistanceChanged? The docs don't seem to indicate there is. Here is my message listener below, thanks for any suggestions!
mMessageListener = new MessageListener() {
#Override
public void onFound(Message message) {
String messageAsString = new String(message.getContent());
Log.d(TAG, "Found message: " + messageAsString);
}
#Override
public void onLost(Message message) {
String messageAsString = new String(message.getContent());
Log.d(TAG, "Lost sight of message: " + messageAsString);
}
#Override
public void onDistanceChanged(Message message, Distance distance) {
super.onDistanceChanged(message, distance);
Log.d(TAG, "New distance "+distance.getMeters()+" to message: " + new String(message.getContent()));
}
#Override
public void onBleSignalChanged(Message message, BleSignal bleSignal) {
super.onBleSignalChanged(message, bleSignal);
Log.d(TAG, "New ble signal " + bleSignal.getRssi() + " to message: " + new String(message.getContent()));
}
};
There are two reasons you may not get that callback:
Unfortunately, the distance callback currently only works for BLE beacon messages. We've fixed the docs to say that, but they won't update on the web until the next Google Play Services SDK release (in a couple weeks). We hope to make it work for more messages in the future.
The BLE signal and distance callbacks aren't called for the PendingIntent version of subscribe().

GoogleCloudMessaging Android

I am confused while implementing the upstream message using the new GoogleCloudMessaging APIs :
public void onClick(final View view) {
if (view == findViewById(R.id.send)) {
new AsyncTask() {
#Override
protected String doInBackground(Void... params) {
String msg = "";
try {
Bundle data = new Bundle();
data.putString("hello", "World");
String id = Integer.toString(msgId.incrementAndGet());
gcm.send(SENDER_ID + "#gcm.googleapis.com", id, data);
msg = "Sent message";
} catch (IOException ex) {
msg = "Error :" + ex.getMessage();
}
return msg;
}
#Override
protected void onPostExecute(String msg) {
mDisplay.append(msg + "\n");
}
}.execute(null, null, null);
} else if (view == findViewById(R.id.clear)) {
mDisplay.setText("");
}
}
We send the messages(XMPP) to GCM server using the SENDER_ID id, so how can my third party server identify my device only with the SENDER_ID?
gcm.send(SENDER_ID + "#gcm.googleapis.com", id, data);
The code you posted sends a message from your device to your 3rd server, not to another device. Your server should establish a connection with the Cloud Connection Server, and since establishing that connection requires using SENDER_ID + "#gcm.googleapis.com" for user name, the GCM server can identify your 3rd party server.
When you 3rd party server sends a message to your device, it uses the Registration ID, which identifies your app on a specific device.
EDIT :
I may have misunderstood your question. If you are asking how does the 3rd party server know which device sent the upstream message to it, the message that your 3rd party server receives contains the Registration ID of the sender, as you can see here :
<message id="">
<gcm xmlns="google:mobile:data">
{
"category":"com.example.yourapp", // to know which app sent it
"data":
{
"hello":"world",
},
"message_id":"m-123",
"from":"REGID"
}
</gcm>
</message>
Even though you don't specify the Registration ID when you call gcm.send(...), GCM knows which device sent the message, so (assuming your app registered to GCM and therefore has a Registration ID) they can add the Registration ID to the message (I'm not sure if they add it on the client side before connecting the GCM server, or if the add it at the GCM server, but it doesn't really matter).

Categories

Resources