@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); App.getApp(this).getAppComponent().inject(this); if (!appPreferences.isLoggedIn()) new AuthDialog(this) .show(getString(R.string.please_auth), "", "") .subscribeOn(AndroidSchedulers.mainThread()) .observeOn(Schedulers.io()) .flatMap( new Func1<Auth, Observable<Token>>() { @Override public Observable<Token> call(Auth auth) { return chatApi.getToken(auth); } }) .observeOn(AndroidSchedulers.mainThread()) .subscribe( new Action1<Token>() { @Override public void call(Token token) { MessageBox.show(token.getToken(), MainActivity.this); appPreferences.setToken(token.getToken()).setLoggedIn(true); if (checkPlayServices()) { if (!appPreferences.isGcmRegistered()) { registerGcm(); Log.d("GCM ID", appPreferences.getGcmId()); } else { Log.d("GCM ID", "not created"); startService(new Intent(MainActivity.this, RegistrationIntentService.class)); } } else Log.d("GCM ID", "no play services"); } }, getOnError()); else if (!appPreferences.isGcmRegistered() && checkPlayServices()) registerGcm(); }
private void registerGcm() { chatApi .gcmRegister(Build.MODEL, null, appPreferences.getGcmId()) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe( new Action1<Void>() { @Override public void call(Void aVoid) { appPreferences.setGcmRegistered(true); } }, getOnError()); }