Reputation: 1850
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?
Upvotes: 1
Views: 1385
Reputation: 73405
Additional note: check your imports and make sure you use the android.support.v7.app.NotificationCompat
class
instead of android.support.v4.app.NotificationCompat
, because even in 25.2.0, using MessagingStyle
with the "v4" builder does not show anything in devices with API < 24.
Upvotes: 1
Reputation: 199900
Update: this is fixed as of the 25.0.0 Support Library release and NotificationCompat.MessagingStyle
now backports much of the styling to previous versions of Android.
Previous 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).
Upvotes: 3
Reputation: 1006964
If I am reading the source code correctly, the fallback for pre-24 devices has not been implemented as of the time of this writing. So, at the moment, it gives you something that compiles, and delegates properly to the native implementation on API Level 24+ devices, but will not show the messages on older devices.
Upvotes: 1