@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(); }
Notification foregroundNote; RemoteViews bigView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.notification_layout_big); // bigView.setOnClickPendingIntent() etc.. Notification.Builder mNotifyBuilder = new Notification.Builder(this); foregroundNote = mNotifyBuilder.setContentTitle("some string") .setContentText("Slide down on note to expand") .setSmallIcon(R.drawable.ic_stat_notify_white) .setLargeIcon(bigIcon) .build(); foregroundNote.bigContentView = bigView; // now show notification.. NotificationManager mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); mNotifyManager.notify(1, foregroundNote);
deleteIntent.putExtra(TODOUUID, mTodoUUID); Notification notification = new Notification.Builder(this) .setContentTitle(mTodoText) .setSmallIcon(R.drawable.ic_done_white_24dp) .setAutoCancel(true)
builder.setContentTitle(getText(R.string.application_name)); builder.setContentText(contentText); builder.setSmallIcon(R.drawable.ic_service_notification);
builder.setContentTitle("WhatsApp Notification"); builder.setContentText("You have a new message"); builder.setSmallIcon(R.drawable.ic_launcher);
.setSmallIcon(R.drawable.block_canary_notification) .setWhen(System.currentTimeMillis()) .setContentTitle(contentTitle) .setContentText(contentText) .setAutoCancel(true)
@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); } }
.setContentTitle(context.getString(R.string.leak_canary_notification_dumping)); Notification notification = LeakCanaryInternals.buildNotification(context, builder); NotificationManager notificationManager =
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 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); }
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); }
@Test @Config(maxSdk = M) public void build_handlesNullContentTitle() { Notification notification = builder.setContentTitle(null).build(); assertThat(shadowOf(notification).getContentTitle().toString()).isEmpty(); }
@Test @Config(minSdk = N) public void build_handlesNullContentTitle_atLeastN() { Notification notification = builder.setContentTitle(null).build(); assertThat(shadowOf(notification).getContentTitle()).isNull(); }
@Test public void build_setsContentTitleOnNotification() throws Exception { Notification notification = builder.setContentTitle("Hello").build(); assertThat(shadowOf(notification).getContentTitle().toString()).isEqualTo("Hello"); }
@Before public void setup() { service = Robolectric.setupService(MyService.class); notBuilder = new Notification.Builder(service) .setSmallIcon(1) .setContentTitle("Test") .setContentText("Hi there"); }
@Test @Config(minSdk = N) public void build_handlesNonStringContentTitle_atLeastN() { Notification notification = builder.setContentTitle(new SpannableString("My title")).build(); assertThat(shadowOf(notification).getContentTitle().toString()).isEqualTo("My title"); }
@Test @Config(maxSdk = M) public void build_handlesNonStringContentTitle() { Notification notification = builder.setContentTitle(new SpannableString("My title")).build(); assertThat(shadowOf(notification).getContentTitle().toString()).isEqualTo("My title"); }
public static void showNotification(Context context, CharSequence contentTitle, CharSequence contentText, PendingIntent pendingIntent, int notificationId) { Notification.Builder builder = new Notification.Builder(context) .setContentText(contentText) .setContentTitle(contentTitle) .setAutoCancel(true) .setContentIntent(pendingIntent); Notification notification = buildNotification(context, builder); NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(notificationId, notification); }
protected void showForegroundNotification(int max, int progress, boolean indeterminate, String contentText) { Notification.Builder builder = new Notification.Builder(this) .setContentTitle(getString(notificationContentTitleResId)) .setContentText(contentText) .setProgress(max, progress, indeterminate); Notification notification = LeakCanaryInternals.buildNotification(this, builder); startForeground(notificationId, notification); }