PushNotifier { static void handleNotification(Context context, Intent pushIntent) throws RuntimeException { context = context.getApplicationContext(); NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE); String messageId = PushIntentUtils.getMessageId(pushIntent); if (messageId == null) { AppCenterLog.warn(LOG_TAG, "Push notification did not contain identifier, generate one."); messageId = UUID.randomUUID().toString(); } int notificationId = messageId.hashCode(); PackageManager packageManager = context.getPackageManager(); Intent actionIntent = packageManager.getLaunchIntentForPackage(context.getPackageName()); if (actionIntent != null) { actionIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); Map<String, String> customData = PushIntentUtils.getCustomData(pushIntent); for (String key : customData.keySet()) { actionIntent.putExtra(key, customData.get(key)); } PushIntentUtils.setMessageId(messageId, actionIntent); } else { actionIntent = new Intent(); } String notificationTitle = PushIntentUtils.getTitle(pushIntent); if (notificationTitle == null || notificationTitle.isEmpty()) { notificationTitle = AppNameHelper.getAppName(context); } String notificationMessage = PushIntentUtils.getMessage(pushIntent); Notification.Builder builder; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && context.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.O) { NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT); notificationManager.createNotificationChannel(channel); builder = new Notification.Builder(context, channel.getId()); } else { builder = getOldNotificationBuilder(context); } setColor(context, pushIntent, builder); setIcon(context, pushIntent, builder); setSound(context, pushIntent, builder); builder.setContentTitle(notificationTitle) .setContentText(notificationMessage) .setWhen(System.currentTimeMillis()) .setStyle(new Notification.BigTextStyle().bigText(notificationMessage)); PendingIntent contentIntent = PendingIntent.getActivity(context, notificationId, actionIntent, 0); builder.setContentIntent(contentIntent); Notification notification = builder.build(); notification.flags |= Notification.FLAG_AUTO_CANCEL; notificationManager.notify(notificationId, notification); }    }
@Test public void handleNotificationWithIconFromMipmap() { String iconString = "picture"; int resourceId = 4; Resources resourcesMock = mock(Resources.class); when(mContextMock.getResources()).thenReturn(resourcesMock); when(resourcesMock.getIdentifier(eq(iconString), eq("drawable"), anyString())).thenReturn(0); when(resourcesMock.getIdentifier(eq(iconString), eq("mipmap"), anyString())).thenReturn(resourceId); when(PushIntentUtils.getIcon(any(Intent.class))).thenReturn(iconString); PushNotifier.handleNotification(mContextMock, new Intent()); verify(mNotificationBuilderMock).setSmallIcon(resourceId); verify(mNotificationManagerMock).notify(mDummyGoogleMessageId.hashCode(), mNotificationMock); }
@Test public void handleNotificationWithIconFromMipmapAdaptiveAndroid26() throws Exception { setVersionSdkInt(Build.VERSION_CODES.O); String iconString = "picture"; int resourceId = 4; Resources resourcesMock = mock(Resources.class); when(mContextMock.getResources()).thenReturn(resourcesMock); when(resourcesMock.getIdentifier(eq(iconString), eq("drawable"), anyString())).thenReturn(0); when(resourcesMock.getIdentifier(eq(iconString), eq("mipmap"), anyString())).thenReturn(resourceId); when(PushIntentUtils.getIcon(any(Intent.class))).thenReturn(iconString); when(mContextMock.getDrawable(resourceId)).thenReturn(mock(AdaptiveIconDrawable.class)); PushNotifier.handleNotification(mContextMock, new Intent()); verify(mNotificationBuilderMock).setSmallIcon(mIconId); verify(mNotificationManagerMock).notify(mDummyGoogleMessageId.hashCode(), mNotificationMock); }
@Test public void handleNotificationWithAppIcon() { String iconString = "picture"; Resources resourcesMock = mock(Resources.class); when(mContextMock.getResources()).thenReturn(resourcesMock); when(resourcesMock.getIdentifier(eq(iconString), eq("drawable"), anyString())).thenReturn(0); when(resourcesMock.getIdentifier(eq(iconString), eq("mipmap"), anyString())).thenReturn(0); when(PushIntentUtils.getIcon(any(Intent.class))).thenReturn(iconString); PushNotifier.handleNotification(mContextMock, new Intent()); verify(mNotificationBuilderMock).setSmallIcon(mIconId); verify(mNotificationManagerMock).notify(mDummyGoogleMessageId.hashCode(), mNotificationMock); }
@Test public void handleNotificationWithInvalidAppIcon() { mApplicationInfoMock.icon = 0; PushNotifier.handleNotification(mContextMock, new Intent()); verify(mNotificationBuilderMock).setSmallIcon(R.drawable.ic_stat_notify_dot); verify(mNotificationManagerMock).notify(mDummyGoogleMessageId.hashCode(), mNotificationMock); }
@Test public void handleNotificationWithAppIconAdaptiveAndroid26() throws Exception { setVersionSdkInt(Build.VERSION_CODES.O); String iconString = "picture"; Resources resourcesMock = mock(Resources.class); when(mContextMock.getResources()).thenReturn(resourcesMock); when(resourcesMock.getIdentifier(eq(iconString), eq("drawable"), anyString())).thenReturn(0); when(resourcesMock.getIdentifier(eq(iconString), eq("mipmap"), anyString())).thenReturn(0); when(mContextMock.getDrawable(mIconId)).thenReturn(mock(AdaptiveIconDrawable.class)); when(PushIntentUtils.getIcon(any(Intent.class))).thenReturn(iconString); PushNotifier.handleNotification(mContextMock, new Intent()); verify(mNotificationBuilderMock).setSmallIcon(R.drawable.ic_stat_notify_dot); verify(mNotificationManagerMock).notify(mDummyGoogleMessageId.hashCode(), mNotificationMock); }
@Test public void handleNotificationWithAppIconAdaptiveAndroid27() throws Exception { setVersionSdkInt(Build.VERSION_CODES.O_MR1); String iconString = "picture"; Resources resourcesMock = mock(Resources.class); when(mContextMock.getResources()).thenReturn(resourcesMock); when(resourcesMock.getIdentifier(eq(iconString), eq("drawable"), anyString())).thenReturn(0); when(resourcesMock.getIdentifier(eq(iconString), eq("mipmap"), anyString())).thenReturn(0); when(mContextMock.getDrawable(mIconId)).thenReturn(mock(AdaptiveIconDrawable.class)); when(PushIntentUtils.getIcon(any(Intent.class))).thenReturn(iconString); PushNotifier.handleNotification(mContextMock, new Intent()); verify(mNotificationBuilderMock).setSmallIcon(mIconId); verify(mNotificationManagerMock).notify(mDummyGoogleMessageId.hashCode(), mNotificationMock); }
@Test public void handleNotificationWithIconFromMipmapAndAppIconAdaptiveAndroid26() throws Exception { setVersionSdkInt(Build.VERSION_CODES.O); String iconString = "picture"; int resourceId = 4; Resources resourcesMock = mock(Resources.class); when(mContextMock.getResources()).thenReturn(resourcesMock); when(resourcesMock.getIdentifier(eq(iconString), eq("drawable"), anyString())).thenReturn(0); when(resourcesMock.getIdentifier(eq(iconString), eq("mipmap"), anyString())).thenReturn(resourceId); when(PushIntentUtils.getIcon(any(Intent.class))).thenReturn(iconString); when(mContextMock.getDrawable(anyInt())).thenReturn(mock(AdaptiveIconDrawable.class)); PushNotifier.handleNotification(mContextMock, new Intent()); verify(mNotificationBuilderMock).setSmallIcon(R.drawable.ic_stat_notify_dot); verify(mNotificationManagerMock).notify(mDummyGoogleMessageId.hashCode(), mNotificationMock); }
@Test public void handleNotificationWithCustomData() { Map<String, String> customData = new HashMap<>(); customData.put("key", "val"); customData.put("key2", "val2"); when(PushIntentUtils.getCustomData(any(Intent.class))).thenReturn(customData); PushNotifier.handleNotification(mContextMock, new Intent()); for (String key : customData.keySet()) { verify(mActionIntentMock).putExtra(key, customData.get(key)); } verify(mNotificationManagerMock).notify(mDummyGoogleMessageId.hashCode(), mNotificationMock); }
@Test public void handleNotificationWithInvalidCustomSound() { String customSoundString = "default"; int resourceId = 2; when(PushIntentUtils.getSound(any(Intent.class))).thenReturn(customSoundString); Resources resourcesMock = mock(Resources.class); when(mContextMock.getResources()).thenReturn(resourcesMock); when(resourcesMock.getIdentifier(eq(customSoundString), eq("raw"), anyString())).thenThrow(new Resources.NotFoundException()); PushNotifier.handleNotification(mContextMock, new Intent()); verify(mNotificationBuilderMock).setDefaults(Notification.DEFAULT_SOUND); verify(mNotificationManagerMock).notify(mDummyGoogleMessageId.hashCode(), mNotificationMock); }
@Test public void handleNotificationWithLauncherActivityHasRightFlags() { PushNotifier.handleNotification(mContextMock, new Intent()); verify(mActionIntentMock).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); verify(mNotificationManagerMock).notify(mDummyGoogleMessageId.hashCode(), mNotificationMock); }
@Test public void handleNotificationWithNoLauncherActivity() throws Exception { Intent intent = mock(Intent.class); whenNew(Intent.class).withNoArguments().thenReturn(intent); when(mPackageManagerMock.getLaunchIntentForPackage(anyString())).thenReturn(null); String title = "title"; when(PushIntentUtils.getTitle(any(Intent.class))).thenReturn(title); Map<String, String> customData = new HashMap<>(); customData.put("key", "val"); customData.put("key2", "val2"); when(PushIntentUtils.getCustomData(any(Intent.class))).thenReturn(customData); PushNotifier.handleNotification(mContextMock, new Intent("mock")); verify(mNotificationBuilderMock).setContentTitle(title); verify(mNotificationManagerMock).notify(mDummyGoogleMessageId.hashCode(), mNotificationMock); verify(intent, never()).putExtra(anyString(), anyString()); }
@Test public void handleNotificationWithDefaultIcon() { int resourceId = 42; Bundle bundle = mock(Bundle.class); when(bundle.getInt(DEFAULT_ICON_METADATA_NAME)).thenReturn(resourceId); mApplicationInfoMock.metaData = bundle; PushNotifier.handleNotification(mContextMock, new Intent()); verify(mNotificationBuilderMock).setSmallIcon(resourceId); verify(mNotificationManagerMock).notify(mDummyGoogleMessageId.hashCode(), mNotificationMock); }
@Test public void handleNotificationMetadataPackageNotFound() throws PackageManager.NameNotFoundException { when(mPackageManagerMock.getApplicationInfo(anyString(), eq(PackageManager.GET_META_DATA))) .thenThrow(new PackageManager.NameNotFoundException()); PushNotifier.handleNotification(mContextMock, new Intent()); verify(mNotificationManagerMock).notify(mDummyGoogleMessageId.hashCode(), mNotificationMock); }
@Test public void handlePushWithNoMessageId() { when(PushIntentUtils.getMessageId(any(Intent.class))).thenReturn(null); PushNotifier.handleNotification(mContextMock, new Intent()); verify(mNotificationManagerMock).notify(anyInt(), any(Notification.class)); verifyStatic(); PushIntentUtils.setMessageId(anyString(), same(mActionIntentMock)); }
@Test public void handlePushBigText() { PushNotifier.handleNotification(mContextMock, new Intent()); verify(mNotificationManagerMock).notify(anyInt(), any(Notification.class)); verifyStatic(); PushIntentUtils.setMessageId(eq(mDummyGoogleMessageId), same(mActionIntentMock)); ArgumentCaptor<Notification.BigTextStyle> styleArgumentCaptor = ArgumentCaptor.forClass(Notification.BigTextStyle.class); verify(mNotificationBuilderMock).setStyle(styleArgumentCaptor.capture()); verify(styleArgumentCaptor.getValue()).bigText("message"); }
@Test public void handlePushTargetN() { mApplicationInfoMock.targetSdkVersion = Build.VERSION_CODES.N; PushNotifier.handleNotification(mContextMock, new Intent()); verify(mNotificationManagerMock).notify(anyInt(), any(Notification.class)); verifyStatic(); PushIntentUtils.setMessageId(eq(mDummyGoogleMessageId), same(mActionIntentMock)); }
@Test public void handleNotificationWithTitle() { String title = "title"; when(PushIntentUtils.getTitle(any(Intent.class))).thenReturn(title); PushNotifier.handleNotification(mContextMock, new Intent()); verify(mNotificationBuilderMock).setContentTitle(title); verify(mNotificationManagerMock).notify(mDummyGoogleMessageId.hashCode(), mNotificationMock); }
@Test public void handleNotificationWithNullTitle() { String appName = "app-name"; mockStatic(AppNameHelper.class); when(AppNameHelper.getAppName(mContextMock)).thenReturn(appName); when(PushIntentUtils.getTitle(any(Intent.class))).thenReturn(null); PushNotifier.handleNotification(mContextMock, new Intent()); verify(mNotificationBuilderMock).setContentTitle(appName); verify(mNotificationManagerMock).notify(mDummyGoogleMessageId.hashCode(), mNotificationMock); }
@Test public void handleNotificationWithEmptyTitle() { String appName = "app-name"; mockStatic(AppNameHelper.class); when(AppNameHelper.getAppName(mContextMock)).thenReturn(appName); when(PushIntentUtils.getTitle(any(Intent.class))).thenReturn(""); PushNotifier.handleNotification(mContextMock, new Intent()); verify(mNotificationBuilderMock).setContentTitle(appName); }
@Test public void handleNotificationWithIconFromDrawable() { String iconString = "picture"; int resourceId = 4; Resources resourcesMock = mock(Resources.class); when(mContextMock.getResources()).thenReturn(resourcesMock); when(resourcesMock.getIdentifier(eq(iconString), eq("drawable"), anyString())).thenReturn(resourceId); when(resourcesMock.getIdentifier(eq(iconString), eq("mipmap"), anyString())).thenReturn(resourceId + 1); when(PushIntentUtils.getIcon(any(Intent.class))).thenReturn(iconString); PushNotifier.handleNotification(mContextMock, new Intent()); verify(mNotificationBuilderMock).setSmallIcon(resourceId); verify(mNotificationManagerMock).notify(mDummyGoogleMessageId.hashCode(), mNotificationMock); }