@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); }
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); }
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); } }
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); }  }
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); }   }
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); }    }