@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 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); }
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); } }
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(); }
builder.setContentText(contentText); builder.setSmallIcon(R.drawable.ic_service_notification); builder.setContentIntent(pendingIntent);
builder.setTicker("this is ticker text"); builder.setContentTitle("WhatsApp Notification"); builder.setContentText("You have a new message"); builder.setSmallIcon(R.drawable.ic_launcher); builder.setContentIntent(pendingIntent);
.setWhen(System.currentTimeMillis()) .setContentTitle(contentTitle) .setContentText(contentText) .setAutoCancel(true) .setContentIntent(pendingIntent)
@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 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(minSdk = N) public void build_handlesNonStringContentText_atLeastN() { Notification notification = builder.setContentText(new SpannableString("Hello")).build(); assertThat(shadowOf(notification).getContentText().toString()).isEqualTo("Hello"); }
@Test @Config(minSdk = N) public void build_handlesNullContentText_atLeastN() { Notification notification = builder.setContentText(null).build(); assertThat(shadowOf(notification).getContentText()).isNull(); }
@Test @Config(maxSdk = M) public void build_handlesNonStringContentText() { Notification notification = builder.setContentText(new SpannableString("Hello")).build(); assertThat(shadowOf(notification).getContentText().toString()).isEqualTo("Hello"); }
@Test public void build_setsContentTextOnNotification() throws Exception { Notification notification = builder.setContentText("Hello Text").build(); assertThat(shadowOf(notification).getContentText().toString()).isEqualTo("Hello Text"); }
@Before public void setup() { service = Robolectric.setupService(MyService.class); notBuilder = new Notification.Builder(service) .setSmallIcon(1) .setContentTitle("Test") .setContentText("Hi there"); }
@Test @Config(maxSdk = M) public void build_handlesNullContentText() { Notification notification = builder.setContentText(null).build(); assertThat(shadowOf(notification).getContentText().toString()).isEmpty(); }
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); }