public static Notification buildNotification(Context context, Notification.Builder builder) { builder.setSmallIcon(R.drawable.leak_canary_notification) .setWhen(System.currentTimeMillis()) .setOnlyAlertOnce(true); if (SDK_INT >= O) { NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); NotificationChannel notificationChannel = notificationManager.getNotificationChannel(NOTIFICATION_CHANNEL_ID); if (notificationChannel == null) { String channelName = context.getString(R.string.leak_canary_notification_channel); notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_DEFAULT); notificationManager.createNotificationChannel(notificationChannel); } builder.setChannelId(NOTIFICATION_CHANNEL_ID); } if (SDK_INT < JELLY_BEAN) { return builder.getNotification(); } else { return builder.build(); } }
@TargetApi(Build.VERSION_CODES.JELLY_BEAN) private void tryNewMiuiBadge(Context context, int badgeCount) throws ShortcutBadgeException { if (resolveInfo == null) { Intent intent = new Intent(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_HOME); resolveInfo = context.getPackageManager().resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY); } if (resolveInfo != null) { NotificationManager mNotificationManager = (NotificationManager) context .getSystemService(Context.NOTIFICATION_SERVICE); Notification.Builder builder = new Notification.Builder(context) .setContentTitle("") .setContentText("") .setSmallIcon(resolveInfo.getIconResource()); Notification notification = builder.build(); try { Field field = notification.getClass().getDeclaredField("extraNotification"); Object extraNotification = field.get(notification); Method method = extraNotification.getClass().getDeclaredMethod("setMessageCount", int.class); method.invoke(extraNotification, badgeCount); mNotificationManager.notify(0, notification); } catch (Exception e) { throw new ShortcutBadgeException("not able to set badge", e); } } }
private Notification buildDefaultNotification(Context context) { String title = context.getString(R.string.default_filedownloader_notification_title); String content = context.getString(R.string.default_filedownloader_notification_content); Notification.Builder builder = new Notification.Builder(context, notificationChannelId); builder.setContentTitle(title).setContentText(content) .setSmallIcon(DEFAULT_NOTIFICATION_ID); return builder.build(); }
public QuestDownloadProgressRelay(Service service, String notificationChannelId, int notificationId) { this.service = service; this.id = notificationId; showNotification = false; PendingIntent pendingIntent = PendingIntent.getActivity( service, 0, new Intent(service, MainActivity.class), 0); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { notificationBuilder = new Notification.Builder(service, notificationChannelId); } else { notificationBuilder = new Notification.Builder(service); } notificationBuilder .setSmallIcon(R.mipmap.ic_dl_notification) .setContentTitle(ApplicationConstants.NAME) .setContentText(service.getResources().getString(R.string.notification_downloading)) .setContentIntent(pendingIntent); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { notificationBuilder.setCategory(Notification.CATEGORY_PROGRESS); } }
public class MyNotificationBuilderJellyBean { Notification.Builder mb = null; MyNotificationBuilderJellyBean(Context myContext, int icon, String ticker, String title, String info, Long timeStamp, PendingIntent pendingIntent, boolean onGoing) { mb = new Notification.Builder(myContext); mb.setSmallIcon(icon); mb.setContentIntent(pendingIntent); mb.setContentTitle(title); mb.setContentText(info); mb.setWhen(timeStamp); if (ticker != null) mb.setTicker(ticker); mb.setOngoing(onGoing); } Notification get() { return mb.build(); } }
public class MyNotificationBuilderHoneyCombToIceCreamSandwich { Notification.Builder mb = null; MyNotificationBuilderHoneyCombToIceCreamSandwich(Context myContext, int icon, String ticker, String title, String info, Long timeStamp, PendingIntent pendingIntent, boolean onGoing) { mb = new Notification.Builder(myContext); mb.setSmallIcon(icon); mb.setContentIntent(pendingIntent); mb.setContentTitle(title); mb.setContentText(info); mb.setWhen(timeStamp); if (ticker != null) mb.setTicker(ticker); mb.setOngoing(onGoing); } Notification get() { return mb.getNotification(); } }
@OnClick(R.id.notification_button) public void openNotification() { Context context = getView().getContext(); Intent serviceIntent = new Intent(context, SampleIntentService.class); PendingIntent contentIntent = PendingIntent.getService(context, 0, serviceIntent, 0); Notification notification = new Notification.Builder(context) .setContentTitle("This is a push notification.") .setContentText("Scoop!") .setSmallIcon(R.drawable.ic_launcher) .setContentIntent(contentIntent) .build(); notificationManager.notify(100, notification); } }
Notification.Builder notificationBuilder= new Notification.Builder(context); notificationBuilder.setSmallIcon(R.drawable.yourIcon); // then set other staff...
private Notification createNotification(boolean failure) { Notification.Builder notificationBuilder = createNotificationBuilder(); notificationBuilder.setContentTitle(context.getString(R.string.app_name)); if (failure) { notificationBuilder.setContentText(context.getString(R.string.relay_disconnected)); notificationBuilder.setSmallIcon(R.drawable.ic_report_problem_24dp); } else { notificationBuilder.setContentText(context.getString(R.string.relay_connected)); notificationBuilder.setSmallIcon(R.drawable.ic_usb_24dp); } notificationBuilder.addAction(createStopAction()); return notificationBuilder.build(); }
Notification notification = new Notification.Builder(this) .setContentTitle(mTodoText) .setSmallIcon(R.drawable.ic_done_white_24dp) .setAutoCancel(true) .setDefaults(Notification.DEFAULT_SOUND)
builder.setContentTitle("WhatsApp Notification"); builder.setContentText("You have a new message"); builder.setSmallIcon(R.drawable.ic_launcher); builder.setContentIntent(pendingIntent); builder.setOngoing(true);
builder.setContentTitle(getText(R.string.application_name)); builder.setContentText(contentText); builder.setSmallIcon(R.drawable.ic_service_notification); builder.setContentIntent(pendingIntent); builder.setOngoing(true);
.setSmallIcon(R.drawable.block_canary_notification) .setWhen(System.currentTimeMillis()) .setContentTitle(contentTitle)
private static void fixNotificationIcon(Context context, Notification notification, Notification.Builder builder) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { //noinspection deprecation builder.setSmallIcon(notification.icon); //noinspection deprecation builder.setLargeIcon(notification.largeIcon); } else { Icon icon = notification.getSmallIcon(); if (icon != null) { Bitmap bitmap = drawableToBitMap(icon.loadDrawable(context)); if (bitmap != null) { Icon newIcon = Icon.createWithBitmap(bitmap); builder.setSmallIcon(newIcon); } } Icon largeIcon = notification.getLargeIcon(); if (largeIcon != null) { Bitmap bitmap = drawableToBitMap(largeIcon.loadDrawable(context)); if (bitmap != null) { Icon newIcon = Icon.createWithBitmap(bitmap); builder.setLargeIcon(newIcon); } } } }
@Override protected void onHandleIntent(Intent intent) { if (intent != null) { int badgeCount = intent.getIntExtra("badgeCount", 0); mNotificationManager.cancel(notificationId); notificationId++; Notification.Builder builder = new Notification.Builder(getApplicationContext()) .setContentTitle("") .setContentText("") .setSmallIcon(R.drawable.ic_launcher); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { setupNotificationChannel(); builder.setChannelId(NOTIFICATION_CHANNEL); } Notification notification = builder.build(); ShortcutBadger.applyNotification(getApplicationContext(), notification, badgeCount); mNotificationManager.notify(notificationId, notification); } }
public void raiseBattle(View view) { Notification n=new Notification.Builder(MainActivity.this, CHANNEL_BATTLE) .setContentTitle(getString(R.string.notif_battle_title)) .setContentText(getString(R.string.notif_battle_text)) .setSmallIcon(android.R.drawable.stat_sys_warning) .setBadgeIconType(Notification.BADGE_ICON_SMALL) .setColor(Color.RED) .setColorized(true) .build(); mgr.notify(NOTIF_ID_BATTLE, n); }
public void raiseCoins(View view) { Notification n=new Notification.Builder(MainActivity.this, CHANNEL_COINS) .setContentTitle(getString(R.string.notif_coins_title)) .setContentText(getString(R.string.notif_coins_text)) .setSmallIcon(android.R.drawable.stat_sys_warning) .setTimeoutAfter(5000) .build(); mgr.notify(NOTIF_ID_COINS, n); }
public void raiseContent(View view) { Notification n=new Notification.Builder(MainActivity.this, CHANNEL_CONTENT) .setContentTitle(getString(R.string.notif_content_title)) .setContentText(getString(R.string.notify_content_text)) .setSmallIcon(android.R.drawable.stat_sys_warning) .build(); mgr.notify(NOTIF_ID_CONTENT, n); }
@Before public void setup() { service = Robolectric.setupService(MyService.class); notBuilder = new Notification.Builder(service) .setSmallIcon(1) .setContentTitle("Test") .setContentText("Hi there"); }
@Test @Config(minSdk = M) public void build_setsIconOnNotification() throws Exception { Notification notification = builder.setSmallIcon(R.drawable.an_image).build(); assertThat(notification.getSmallIcon().getResId()).isEqualTo(R.drawable.an_image); }