TokenRegistry implements InvalidClientIdHandler, InternalRegistrationProgressListener { public void receive(String token) throws JSONException { Log.d(TAG, "Received token for " + platformType.toString() + ": " + token); SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); String cachedId = preferences.getString(sharedPreferencesKey(appKey), null); if (cachedId == null) { upload(token); } else { update(token, cachedId); } } TokenRegistry(String appKey, RegistrationListenerStack listenerStack, Context context, PlatformType platformType, PusherAndroidOptions options, PusherAndroidFactory factory); void receive(String token); @Override void onInvalidClientId(StringEntity params); @Override void onSuccessfulRegistration(String clientId, Context context); @Override void onFailedRegistration(int statusCode, String reason);  }
@Test public void receiveUploadsWhenNoCachedId() throws Exception { tokenRegistry.receive("mysuperspecialfcmtoken"); verify(factory).newTokenUploadHandler(context, stack); ArgumentCaptor paramsCaptor = ArgumentCaptor.forClass(StringEntity.class); verify(client).post( eq(context), eq("https: (HttpEntity) paramsCaptor.capture(), eq("application/json"), eq(tokenUploadHandler) ); HttpEntity params = (HttpEntity) paramsCaptor.getValue(); JSONAssert.assertEquals( EntityUtils.toString(params), "{\"platform_type\":\"fcm\",\"token\":\"mysuperspecialfcmtoken\",\"app_key\":\"superkey\"}", true ); }
@Test public void receivesUpdatesWhenCachedId() throws Exception { PreferenceManager. getDefaultSharedPreferences(context). edit(). putString("__pusher__client__key__fcm__superkey", "this-is-the-client-id") .apply(); tokenRegistry.receive("woot-token-woot"); ArgumentCaptor paramsCaptor = ArgumentCaptor.forClass(StringEntity.class); verify(factory).newTokenUpdateHandler( eq("this-is-the-client-id"), (StringEntity) paramsCaptor.capture(), eq(context), eq(stack), eq(tokenRegistry) ); StringEntity sentParams = (StringEntity) paramsCaptor.getValue(); verify(client).put( eq(context), eq("https: eq(sentParams), eq("application/json"), eq(tokenUpdateHandler) ); JSONAssert.assertEquals(EntityUtils.toString((HttpEntity) paramsCaptor.getValue()), "{\"platform_type\":\"fcm\",\"token\":\"woot-token-woot\",\"app_key\":\"superkey\"}", true ); }