RubenGees RubenGees - 2 months ago 8
Android Question

New MessagingStyle on API < 24

I just stumbled upon the new

MessagingStyle
described here: https://developer.android.com/guide/topics/ui/notifiers/notifications.html (last paragraph)

I investigated further and found such a class also as a
NotificationCompat.Style
variant.

This is the code I tried:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE))
.notify(123, new NotificationCompat.Builder(this)
.setContentTitle("Test")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentText("4 new messages")
.setStyle(new NotificationCompat.MessagingStyle("Me")
.setConversationTitle("Team lunch")
.addMessage("Hi", 123, null) // Pass in null for user.
.addMessage("What's up?", 234, "Coworker")
.addMessage("Not much", 345, null)
.addMessage("How about lunch?", 456, "Coworker")).build());
}
}


This is basically just the default Activity generated when creating a new project and the sample code taken from the linked website.

Now my problem: The extended style is not shown on APIs below 24. I tested on a device with API 23. When running on an emulator with API 24, it works.

The documentation states:


Helper class for generating large-format notifications that include multiple back-and-forth messages of varying types between any number of people.
If the platform does not provide large-format notifications, this method has no effect. The user will always see the normal notification view. [...]


But versions since KitKat do provide large-format notifications.

Is the documentation not clear enough or am I doing something wrong?

Answer

As per this bug report, MessagingStyle currently does not do any special formatting prior to Android N. The bug report is marked as FutureRelease which means that the work is done and it will support pre-N devices with a more rich formatting in a future version of the Support Library.

If you wish to use it now, you can certainly build your own pre-N version of the notification (using a BigTextStyle if the version of Android is less than N for example).

Comments