Search in sources :

Example 1 with Subscription

use of com.backendless.Subscription in project Android-SDK by Backendless.

the class ChatActivity method onCreate.

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.chat);
    final String name = getIntent().getStringExtra(Defaults.NAME_TAG);
    final EditText historyField = (EditText) findViewById(R.id.historyField);
    final EditText messageField = (EditText) findViewById(R.id.messageField);
    Backendless.initApp(this, Defaults.APPLICATION_ID, Defaults.SECRET_KEY, Defaults.VERSION);
    Backendless.Messaging.subscribe(Defaults.CHANNEL_NAME, new AsyncCallback<List<Message>>() {

        @Override
        public void handleResponse(List<Message> response) {
            for (Message message : response) historyField.setText(message.getPublisherId() + ": " + message.getData() + "\n" + historyField.getText());
        }

        @Override
        public void handleFault(BackendlessFault fault) {
            Toast.makeText(ChatActivity.this, fault.getMessage(), Toast.LENGTH_SHORT).show();
        }
    }, new AsyncCallback<Subscription>() {

        @Override
        public void handleResponse(Subscription response) {
            subscription = response;
        }

        @Override
        public void handleFault(BackendlessFault fault) {
            Toast.makeText(ChatActivity.this, fault.getMessage(), Toast.LENGTH_SHORT).show();
        }
    });
    messageField.setOnKeyListener(new View.OnKeyListener() {

        @Override
        public boolean onKey(View view, int keyCode, KeyEvent keyEvent) {
            if (keyCode == KeyEvent.KEYCODE_ENTER && keyEvent.getAction() == KeyEvent.ACTION_UP) {
                String message = messageField.getText().toString();
                if (message == null || message.equals(""))
                    return true;
                final ProgressDialog progressDialog = ProgressDialog.show(ChatActivity.this, "", "Sending", true);
                Backendless.Messaging.publish(Defaults.CHANNEL_NAME, message, new PublishOptions(name), new AsyncCallback<MessageStatus>() {

                    @Override
                    public void handleResponse(MessageStatus response) {
                        progressDialog.cancel();
                        messageField.setText("");
                    }

                    @Override
                    public void handleFault(BackendlessFault fault) {
                        progressDialog.cancel();
                        Toast.makeText(ChatActivity.this, fault.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                });
                return true;
            }
            return false;
        }
    });
}
Also used : EditText(android.widget.EditText) Message(com.backendless.messaging.Message) AsyncCallback(com.backendless.async.callback.AsyncCallback) PublishOptions(com.backendless.messaging.PublishOptions) ProgressDialog(android.app.ProgressDialog) View(android.view.View) BackendlessFault(com.backendless.exceptions.BackendlessFault) KeyEvent(android.view.KeyEvent) MessageStatus(com.backendless.messaging.MessageStatus) List(java.util.List) Subscription(com.backendless.Subscription)

Aggregations

ProgressDialog (android.app.ProgressDialog)1 KeyEvent (android.view.KeyEvent)1 View (android.view.View)1 EditText (android.widget.EditText)1 Subscription (com.backendless.Subscription)1 AsyncCallback (com.backendless.async.callback.AsyncCallback)1 BackendlessFault (com.backendless.exceptions.BackendlessFault)1 Message (com.backendless.messaging.Message)1 MessageStatus (com.backendless.messaging.MessageStatus)1 PublishOptions (com.backendless.messaging.PublishOptions)1 List (java.util.List)1