Android smack.util.StringUtil does not have parseBareAddress

207 views Asked by At

I was looking for different ways to support offline messaging in my application but each of these examples include parseBareAdress and Android studio gives error stating that it cannot resolve parseBareAdress. It can resolve everything else. Here is my code

import org.jivesoftware.smack.util.StringUtils;

public static void handleOfflineMessages(XMPPConnection connection, Context ctx)throws Exception {
        OfflineMessageManager offlineMessageManager = new OfflineMessageManager(connection);

        if (!offlineMessageManager.supportsFlexibleRetrieval()) {
            Log.d(TAG,"Offline messages not supported");
            return;
        }

        if (offlineMessageManager.getMessageCount() == 0) {
            Log.d(TAG,"No offline messages found on server");
        } else {
            List<Message> msgs = offlineMessageManager.getMessages();
            for (Message msg : msgs) {
                String fullJid = msg.getFrom();
                String bareJid = StringUtils.parseBareAddress(fullJid);
                String messageBody = msg.getBody();
                if (messageBody != null) {
                    Log.d(TAG,"Retrieved offline message from " +messageBody);
                }
            }
            offlineMessageManager.deleteMessages();
        }
    }

Here are my dependencies

  compile 'org.igniterealtime.smack:smack-android-extensions:4.1.0-alpha6'
    compile 'org.igniterealtime.smack:smack-tcp:4.1.0-alpha6'
1

There are 1 answers

0
MrPk On

Replace StringUtils.parseBareAddress(fullJid) with this custom method:

/**
 * Input: user@server/Smack || user@server-035<br>
 * user<br>
 * @param jid
 * @return bareAddress
 */
public static String parseBareAddress(String jid)
{
    Pattern regex = Pattern.compile("(.+)(@.+)");


      Matcher matcher = regex.matcher( jid );
      if (matcher.matches())
      {
          return matcher.group(1);
      }

      return jid; //safe condition
}