PushIntentUtils { static Map<String, String> getCustomData(Intent pushIntent) { Map<String, String> customData = new HashMap<>(); Map<String, String> standardData = new HashMap<>(); Bundle intentExtras = pushIntent.getExtras(); if (intentExtras != null) { for (String key : intentExtras.keySet()) { String value = String.valueOf(intentExtras.get(key)); if (key.startsWith(EXTRA_GCM_PREFIX) || key.startsWith(EXTRA_GOOGLE_PREFIX) || EXTRA_STANDARD_KEYS.contains(key)) { standardData.put(key, value); } else { customData.put(key, value); } } } AppCenterLog.debug(LOG_TAG, "Push standard data: " + standardData); AppCenterLog.debug(LOG_TAG, "Push custom data: " + customData); return customData; }    }
@Test public void getCustomData() { Map<String, String> customData = new HashMap<>(); customData.put("some key", "some value"); customData.put("some key2", "some value2"); final Map<String, String> extras = new HashMap<>(customData); extras.put(PushIntentUtils.EXTRA_COLOR, "val"); extras.put(PushIntentUtils.EXTRA_SOUND_ALT, "val"); extras.put(PushIntentUtils.EXTRA_GOOGLE_MESSAGE_ID, "val"); extras.put(PushIntentUtils.EXTRA_ICON, "val"); extras.put(PushIntentUtils.EXTRA_MESSAGE, "val"); extras.put(PushIntentUtils.EXTRA_SOUND, "val"); extras.put(PushIntentUtils.EXTRA_TITLE, "val"); extras.put(PushIntentUtils.EXTRA_GOOGLE_PREFIX + "ttl", "val"); for (String key : PushIntentUtils.EXTRA_STANDARD_KEYS) { extras.put(key, "val"); } Bundle mockBundle = mock(Bundle.class); when(mockBundle.keySet()).thenReturn(extras.keySet()); when(mockBundle.get(anyString())).thenAnswer(new Answer<Object>() { @Override public Object answer(InvocationOnMock invocation) { String key = (String) invocation.getArguments()[0]; return extras.get(key); } }); Intent pushIntent = mock(Intent.class); when(pushIntent.getExtras()).thenReturn(mockBundle); Map<String, String> retrievedCustomData = PushIntentUtils.getCustomData(pushIntent); assertEquals(customData, retrievedCustomData); }
@Test public void getCustomDataFromNullIntentExtras() { assertEquals(Collections.emptyMap(), PushIntentUtils.getCustomData(mock(Intent.class))); }