Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Styling notification InboxStyle

I try to implement an extendable notification and I have used the InboxStyle for that.

Based on the following image from the documentation:

inboxstyle notification

it should be possible to style the text. In this case make "Google Play" bold.

InboxStyle has only addLine() where I can pass a CharSequence. I tried with Html.fromHtml() and used some html formatting but I couldn't succeed.

NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle(); inboxStyle.setBigContentTitle("title"); inboxStyle.setSummaryText("summarytext"); // fetch push messages synchronized (mPushMessages) {     HashMap<String, PushMessage> messages = mPushMessages.get(key);     if (messages != null) {         for (Entry<String, PushMessage> msg : messages.entrySet()) {             inboxStyle.addLine(Html.fromHtml("at least <b>one word</b> should be bold!");         }         builder.setStyle(inboxStyle);         builder.setNumber(messages.size());     } } 

Any idea about this?

like image 957
WarrenFaith Avatar asked Jan 30 '13 10:01

WarrenFaith


People also ask

How do you customize push notifications?

Configure custom push notifications in the Dashboard To do so, navigate to the Settings > App Settings page, select the app you'd like to set up custom push notifications for, then scroll down to the push notifications section. Change the option under 'Select push notification server' to 'Custom push notifications'.

What is displaying notification?

A notification is a message that Android displays outside your app's UI to provide the user with reminders, communication from other people, or other timely information from your app. Users can tap the notification to open your app or take an action directly from the notification.

How do I integrate notifications on Android?

Navigate to Settings > Mobile Apps. Click the mobile app for which you'd like to send a push notification. For Device Token, enter the token you located above. For Message, enter a message to display in the push notification.


2 Answers

You don't need to use fromHtml. I've had issues with fromHtml in the past (when what you display comes from user, code injection can result in ugly things). Also, I don't like putting formatting elements in strings.xml (if you use services for translation, they might screw up your HTML tags).

The addLine method, as most methods to set text in notifications (setTicker, setContentInfo, setContentTitle, etc.) take a CharSequence as parameter.

So you can pass a Spannable. Let's say you want "Bold this and italic that.", you can format it this way (of course don't hardcode positions):

Spannable sb = new SpannableString("Bold this and italic that."); sb.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); sb.setSpan(new StyleSpan(android.graphics.Typeface.ITALIC), 14, 20, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); inboxStyle.addLine(sb); 

Now if you need to build string dynamically with localized strings, like "Today is [DAY], good morning!", put string with a placeholder in strings.xml:

<string name="notification_line_format">Today is %1$s, good morning!</string> 

Then format this way:

String today = "Sunday"; String lineFormat = context.getString(R.string.notification_line_format); int lineParamStartPos = lineFormat.indexOf("%1$s"); if (lineParamStartPos < 0) {   throw new InvalidParameterException("Something's wrong with your string! LINT could have caught that."); } String lineFormatted = context.getString(R.string.notification_line_format, today);  Spannable sb = new SpannableString(lineFormatted); sb.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), lineParamStartPos, lineParamStartPos + today.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); inboxStyle.addLine(sb); 

You'll get "Today is Sunday, good morning!", and as far as I know it works with all versions of Android.

like image 140
tdevaux Avatar answered Sep 19 '22 13:09

tdevaux


Your code worked like a charm on my Samsung S3 with Android 4.1.1 on it. What Android version are you using?

import android.app.Activity; import android.app.NotificationManager; import android.content.Context; import android.os.Bundle; import android.support.v4.app.NotificationCompat; import android.text.Html; import android.view.Menu;  public class MainActivity extends Activity {      private final int ID = 1;      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);         NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(                 this).setSmallIcon(R.drawable.ic_launcher);         NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();         inboxStyle.addLine(Html                 .fromHtml("<i>italic</i> <b>bold</b> text works"));         mBuilder.setStyle(inboxStyle);         mBuilder.setNumber(1);          NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);         mNotificationManager.notify(ID, mBuilder.build());     }  } 
like image 45
riwnodennyk Avatar answered Sep 18 '22 13:09

riwnodennyk