简体   繁体   中英

Phonegap jabber plugin for android

For jabber support i use library Smack. Android port asmack.

I have class SmackAPI which implements MessageListener interface and contains methods to connect, login, send message. In the same time this class contains method:

@Override
public void processMessage(Chat chat, Message message) {
    String from = message.getFrom();
    String body = message.getBody();
    System.out.println(String.format("Received message '%1$s' from %2$s", body, from));
    this.recievedMessage = message;
}

It provides by MessageListener interface. All new messages processed by this method.

I write jabber plugin to connect, login, send message from phonegap.

My question: how i can in javascript listen for new messages?

I did it. I dont know however it is right way, but it works!

Cordova plugin class:

public class SmackJabber extends CordovaPlugin {
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) {
    this.cbContext = callbackContext;
    switch (action) {
            case LISTEN_MESSAGE:
                res = new PluginResult(PluginResult.Status.NO_RESULT);
                res.setKeepCallback(true);
                cordova.getThreadPool().execute(new Runnable() {
                    @Override
                    public void run() {
                        String callbackId = cbContext.getCallbackId();
                        while (true) {
                            String msg = getMsg();
                            if (msg != null) {
                                res = new PluginResult(PluginResult.Status.OK, msg);
                                res.setKeepCallback(true);
                                CallbackContext cb = new CallbackContext(callbackId, webView);
                                cb.sendPluginResult(res);
                            }
                        }
                    }
                });
                cbContext.sendPluginResult(res);
                break; 

And easy javascript. Just call plugin method:

        window.plugins.smackJabber.listenMessage(function(result) {
                    alert(result)
                }, function(error) {
                    alert(error)
                }
        );

Explanation: I call plugin method "listenMessage" (calling "execute" method with action "LISTEN_MESSAGE"). There i start thread from cordova threadpool with runnable, in runnable i got recursive function which check message. But before start runnable i have to take callbackId of method who call method execute. Also, for exit from method, i create new PluginResult with status "NO_RESULT" and set it option "keepCallback" to true - it means, that method calls in javascript awaiting one more callback result from me. When i got message, i create new callbackcontext based on callbackid and my webview, do setKeepCallback to true for futher possible responses for pluginresult, putting in pluginresult my message with status "OK" and sending it to callbackcontext. That's all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM