Asked  10 Months ago    Answers:  5   Viewed   11 times

I want to implement stacked notifications on Android Wear To do that I create 1 summary notification and N individual notifications for each "item". I want only the summary to be shown on the phone. Here's my code:

private void showNotifications() {
    NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    showNotification1(notificationManager);
    showNotification2(notificationManager);
    showGroupSummaryNotification(notificationManager);
}

private void showNotification1(NotificationManager notificationManager) {
    showSingleNotification(notificationManager, "title 1", "message 1", 1);
}

private void showNotification2(NotificationManager notificationManager) {
    showSingleNotification(notificationManager, "title 2", "message 2", 2);
}

protected void showSingleNotification(NotificationManager notificationManager,
                                      String title,
                                      String message,
                                      int notificationId) {
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    builder.setContentTitle(title)
            .setContentText(message)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setGroupSummary(false)
            .setGroup("group");
    Notification notification = builder.build();
    notificationManager.notify(notificationId, notification);
}

private void showGroupSummaryNotification(NotificationManager notificationManager) {
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    builder.setContentTitle("Dummy content title")
            .setContentText("Dummy content text")
            .setStyle(new NotificationCompat.InboxStyle()
                    .addLine("Line 1")
                    .addLine("Line 2")
                    .setSummaryText("Inbox summary text")
                    .setBigContentTitle("Big content title"))
            .setNumber(2)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setCategory(Notification.CATEGORY_EVENT)
            .setGroupSummary(true)
            .setGroup("group");
    Notification notification = builder.build();
    notificationManager.notify(123456, notification);
}

This works just fine on Android 5.1, only the summary is shown in the phone's notification bar:

But on Android 4.4 it also shows individual notifications 1 and 2:

In both cases notifications on Android Wear are shown in a stack, as desired. How do I make Android 4.4 only show the summary notification in the notification bar?

 Answers

5

Fixed this by using

NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);

instead of

NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

and replacing NotificationManager with NotificationManagerCompat in corresponding method signatures.

Monday, August 9, 2021
 
Kenny
 
1

I found the problem: I had my vector-drawables in drawable-anydpi/ - this produces this crash - when i move the drawables to drawable/ it works fine

Saturday, June 19, 2021
 
PHPWDev
 
3

You are sending the same request code for your pending intens. Change this:

PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

To:

PendingIntent contentIntent = PendingIntent.getActivity(context, UNIQUE_INT_PER_CALL, notificationIntent, 0);

intents are not created if you send the same params. They are reused.

Monday, June 21, 2021
 
3

You can't expand notification. The only solution is, set Priority Max, then the top of the notification list where it would be expanded. And it depends on the device as well.

mBuilder.setPriority(Notification.PRIORITY_HIGH)
Wednesday, October 13, 2021
5

Here is the layout that the alertdialog is using to create your view:

<!--
    This layout file is used by the AlertDialog when displaying a list of items.
    This layout file is inflated and used as the TextView to display individual
    items.
-->
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="@android:color/primary_text_light_disable_only"
    android:gravity="center_vertical"
    android:paddingLeft="14dip"
    android:paddingRight="15dip"
    android:ellipsize="marquee"
/>

As you can see the ellipsize is set to marquee, so I don't believe it was ever written with the intention to allow multilines.

There is a bug open at the moment that ellipse dots are never shown: http://code.google.com/p/android/issues/detail?id=10554

Therefore it is acting correctly.

If you want it to go onto multiple lines, create your own layout file and pass that to your dialog, that way you have more control anyway.

Thursday, November 4, 2021
 
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share