powered by Jive Software

How to Get offline messages asmack (Android) real time

android

#1

Hi I’m newbie on openfire .

I have offline messages option enabled in the openfire server.

User A is online ,User B is online in this case I’m able to get messages.

Now User B Turned off his WiFi(Note : User A waited till the user B Session completely killed in the server ) now User A sent a message to User B.
Now User B Comes online again server is sending the message to user B at time 10 minutes.

My code:

 private class MyLoginTask extends AsyncTask<String, String, String> {
        @Override
        protected String doInBackground(String... params) {
            // Create a connection to the jabber.org server.
            realm = Realm.getDefaultInstance();
            dataUsers = realm.where(EntityUsers.class).findFirst();
            if (dataUsers != null) {
                userName2 = dataUsers.getUsername();
                password = dataUsers.getPass();

            } 
            new Thread() {
                @Override
                public void run() {
                        InetAddress addr = null;
                    try {
                        // inter your ip4address now checking it
                        addr = InetAddress.getByName(domainAddress);
                    } catch (UnknownHostException e) {
                        e.printStackTrace();
                    }
                    HostnameVerifier verifier = new HostnameVerifier() {
                        @Override
                        public boolean verify(String hostname, SSLSession session) {
                            return false;
                        }
                    };
                    DomainBareJid serviceName = null;
                    try {
                        serviceName = JidCreate.domainBareFrom(domainAddress);
                    } catch (XmppStringprepException e) {
                        e.printStackTrace();
                    }
                    XMPPTCPConnectionConfiguration config = XMPPTCPConnectionConfiguration.builder()
                            .setUsernameAndPassword(userName2, password)//userName2,"spinku12345"
                            .setPort(5222)
                            .setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
                            .setXmppDomain(serviceName)
                            .setHostnameVerifier(verifier)
                            .setHostAddress(addr)
                            .setDebuggerEnabled(true)
                            .setSendPresence(true)
                            .build();
                    mConnection = new XMPPTCPConnection(config);
                    try {
                        mConnection.connect();
                        // all these proceedure also thrown error if you does not seperate this thread now we seperate thread create
                        if(mConnection.isConnected()) {
                            Log.w("app", "conn done");
                        }
                        mConnection.login();

                        if (mConnection.isAuthenticated() && mConnection.isConnected()) {
                            //now send message and receive message code here
                            Log.e(TAG, "run: auth done and connected successfully");
                            ChatManager chatManager = ChatManager.getInstanceFor(mConnection);
                            chatManager.addListener(new IncomingChatMessageListener() {
                                @Override
                                public void newIncomingMessage(EntityBareJid from, Message message, Chat chat) {
                                    Log.e(TAG, "New message from " + from + ": " + message.getBody());
//                                MessagesData data = new MessagesData("received",message.getBody().toString());
//                                final MessagesData data = new MessagesData(userName1 + "@" + serverName, message.getBody().toString());//received
                                    final MessagesData data = new MessagesData("(" + getTime() + ")" + userName1, message.getBody().toString(), 0);//received
                                    mMessagesData.add(data);
                                    vibrator.vibrate(500);
                                    mRingtone.play();
                                    //now update recyler view
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            //this ui thread important otherwise error occur
                                            mAdapter = new Adapter(mMessagesData);
                                            mRecyclerView.setAdapter(mAdapter);
//                                        setupChatMessages();
                                        }
                                    });
                                }
                            });
                        }
                    } catch (SmackException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    } catch (XMPPException e) {
                        e.printStackTrace();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

            }.start();

            return "";
        }

How to setup a server to sending offline messages in real time.


#2

Openfire should send offline messages without any delay, once a client connects to the server. There should be something missing in your code dealing with https://xmpp.org/extensions/xep-0203.html
Also, move this topic to Smack Support as this has nothing to do with Openfire and you might get better help there (press the pencil besides the title of this topic and choose a category).


#3

what should I do.
I’m newbie on Smack.