private Notification getForegroundNotification(int allowed, int blocked) { Intent main = new Intent(this, ActivityMain.class); PendingIntent pi = PendingIntent.getActivity(this, 0, main, PendingIntent.FLAG_UPDATE_CURRENT); TypedValue tv = new TypedValue(); getTheme().resolveAttribute(R.attr.colorPrimary, tv, true); NotificationCompat.Builder builder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ic_security_white_24dp) .setContentTitle(getString(R.string.app_name)) .setContentText(getString(R.string.msg_started)) .setContentIntent(pi) .setCategory(Notification.CATEGORY_STATUS) .setVisibility(Notification.VISIBILITY_SECRET) .setPriority(Notification.PRIORITY_MIN) .setColor(tv.data) .setOngoing(true) .setAutoCancel(false); if (allowed > 0 || blocked > 0) { NotificationCompat.BigTextStyle notification = new NotificationCompat.BigTextStyle(builder); notification.bigText(getString(R.string.msg_started)); notification.setSummaryText(getString(R.string.msg_packages, allowed, blocked)); return notification.build(); } else return builder.build(); }
/** * Big Text Style Notification * * @return Notification * @see CreateNotification */ private Notification setBigTextStyleNotification() { Bitmap remote_picture = null; // Create the style object with BigTextStyle subclass. NotificationCompat.BigTextStyle notiStyle = new NotificationCompat.BigTextStyle(); notiStyle.setBigContentTitle("Big Text Expanded"); notiStyle.setSummaryText("Nice big text."); try { remote_picture = BitmapFactory.decodeStream((InputStream) new URL(sample_url).getContent()); } catch (IOException e) { e.printStackTrace(); } // Add the big text to the style. CharSequence bigText = "This is an example of a large string to demo how much " + "text you can show in a 'Big Text Style' notification."; notiStyle.bigText(bigText); // Creates an explicit intent for an ResultActivity to receive. Intent resultIntent = new Intent(this, ResultActivity.class); // This ensures that the back button follows the recommended convention for the back key. TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); // Adds the back stack for the Intent (but not the Intent itself). stackBuilder.addParentStack(ResultActivity.class); // Adds the Intent that starts the Activity to the top of the stack. stackBuilder.addNextIntent(resultIntent); PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); return new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ic_launcher) .setAutoCancel(true) .setLargeIcon(remote_picture) .setContentIntent(resultPendingIntent) .addAction(R.drawable.ic_launcher, "One", resultPendingIntent) .addAction(R.drawable.ic_launcher, "Two", resultPendingIntent) .addAction(R.drawable.ic_launcher, "Three", resultPendingIntent) .setContentTitle("Big Text Normal") .setContentText("This is an example of a Big Text Style.") .setStyle(notiStyle) .build(); }
private void setNotificationMessage( int notId, Bundle extras, NotificationCompat.Builder mBuilder) { String message = extras.getString(MESSAGE); String style = extras.getString(STYLE, STYLE_TEXT); if (STYLE_INBOX.equals(style)) { setNotification(notId, message); mBuilder.setContentText(fromHtml(message)); ArrayList<String> messageList = messageMap.get(notId); Integer sizeList = messageList.size(); if (sizeList > 1) { String sizeListMessage = sizeList.toString(); String stacking = sizeList + " more"; if (extras.getString(SUMMARY_TEXT) != null) { stacking = extras.getString(SUMMARY_TEXT); stacking = stacking.replace("%n%", sizeListMessage); } NotificationCompat.InboxStyle notificationInbox = new NotificationCompat.InboxStyle() .setBigContentTitle(fromHtml(extras.getString(TITLE))) .setSummaryText(fromHtml(stacking)); for (int i = messageList.size() - 1; i >= 0; i--) { notificationInbox.addLine(fromHtml(messageList.get(i))); } mBuilder.setStyle(notificationInbox); } else { NotificationCompat.BigTextStyle bigText = new NotificationCompat.BigTextStyle(); if (message != null) { bigText.bigText(fromHtml(message)); bigText.setBigContentTitle(fromHtml(extras.getString(TITLE))); mBuilder.setStyle(bigText); } } } else if (STYLE_PICTURE.equals(style)) { setNotification(notId, ""); NotificationCompat.BigPictureStyle bigPicture = new NotificationCompat.BigPictureStyle(); bigPicture.bigPicture(getBitmapFromURL(extras.getString(PICTURE))); bigPicture.setBigContentTitle(fromHtml(extras.getString(TITLE))); bigPicture.setSummaryText(fromHtml(extras.getString(SUMMARY_TEXT))); mBuilder.setContentTitle(fromHtml(extras.getString(TITLE))); mBuilder.setContentText(fromHtml(message)); mBuilder.setStyle(bigPicture); } else { setNotification(notId, ""); NotificationCompat.BigTextStyle bigText = new NotificationCompat.BigTextStyle(); if (message != null) { mBuilder.setContentText(fromHtml(message)); bigText.bigText(fromHtml(message)); bigText.setBigContentTitle(fromHtml(extras.getString(TITLE))); String summaryText = extras.getString(SUMMARY_TEXT); if (summaryText != null) { bigText.setSummaryText(fromHtml(summaryText)); } mBuilder.setStyle(bigText); } /* else { mBuilder.setContentText("<missing message content>"); } */ } }
private void showNotification( Context context, int notificationId, int smallIconId, String title, String contentText, String bigTitle, String bigContentText, String summaryText, String ticker, Intent intent) { NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); NotificationCompat.Builder builder = new NotificationCompat.Builder(context); builder.setSound(soundUri); if (smallIconId == 0) { builder.setSmallIcon(R.mipmap.ic_launcher); } else { builder.setSmallIcon(smallIconId); } builder.setWhen(System.currentTimeMillis()); // builder.setNumber(10); if (!StringUtils.isEmptyString(ticker)) { builder.setTicker(ticker); } if (StringUtils.isEmptyString(title)) { builder.setContentTitle(PackageUtils.getApplicationName(context)); } else { builder.setContentTitle(title); } builder.setContentText(contentText); builder.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE); builder.setAutoCancel(true); // big title and text if (!StringUtils.isEmptyString(bigTitle) && !StringUtils.isEmptyString(bigContentText)) { NotificationCompat.BigTextStyle style = new NotificationCompat.BigTextStyle(builder); if (!StringUtils.isEmptyString(summaryText)) { style.setSummaryText(summaryText); } style.setBigContentTitle(bigTitle); style.bigText(bigContentText); builder.setStyle(style); } if (intent != null) { intent.setFlags( intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK); PendingIntent pendingIntent = PendingIntent.getActivity( context, notificationId, intent, PendingIntent.FLAG_UPDATE_CURRENT); builder.setContentIntent(pendingIntent); } notificationManager.notify(notificationId, builder.build()); }