androidinstant-messagingsinch

Don't receive BroadcastReceiver using sinch example


I tried using this example in my app in order to have instant messaging https://www.sinch.com/tutorials/android-messaging-tutorial-using-sinch-and-parse/ I call broadcast receiver to listen for the broadcast from my service, but it never succeed or fails. My progress dialog keep spinning forever, and no toast is shown.

My service:

public class MessageService extends Service implements SinchClientListener {
...
private LocalBroadcastManager broadcaster;
private Intent broadcastIntent = new Intent("il.ac.app.my.Activity.ListUsersActivity");
...
@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    APP_KEY = getResources().getString(R.string.sinch_key);
    APP_SECRET = getResources().getString(R.string.sinch_secret);
    ENVIRONMENT = getResources().getString(R.string.sinch_hostname);

    //get the current user id from Parse
    String currentUserId = ParseUser.getCurrentUser().getObjectId();
    if (currentUserId != null && !isSinchClientStarted()) {
        startSinchClient(currentUserId);
    }
    broadcaster = LocalBroadcastManager.getInstance(this);
    return super.onStartCommand(intent, flags, startId);
}
...
@Override
public void onClientStarted(SinchClient client) {
    broadcastIntent.putExtra("success", true);
    broadcaster.sendBroadcast(broadcastIntent);

    client.startListeningOnActiveConnection();
    messageClient = client.getMessageClient();
}

My activity:

private void showSpinner() {
    progressDialog = new ProgressDialog(this);
    progressDialog.setTitle("Loading");
    progressDialog.setMessage("Please wait...");
    progressDialog.show();

    //broadcast receiver to listen for the broadcast from MessageService
    receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Boolean success = intent.getBooleanExtra("success", false);
            progressDialog.dismiss();
            if (!success) {
                Toast.makeText(getApplicationContext(), "Messaging service failed to start", Toast.LENGTH_LONG).show();
            }
            else {
                Toast.makeText(getApplicationContext(), "Messaging service started", Toast.LENGTH_LONG).show();
            }
        }
    };

    LocalBroadcastManager.getInstance(this).registerReceiver(receiver, new IntentFilter("il.ac.app.my.Activity.ListUsersActivity"));
}

Solution

  • In LoginActivity, try to start ListUsersActivity before Sinch Service as follows:

     startActivity(intent);
     startService(serviceIntent);