12

If flag "FLAG_NO_CLEAR" is used the notification gets not displayed on the Android Wear.

Does anyone know why or any workaround? I didn't find any information in the documentation.

I need the flag "FLAG_NO_CLEAR" on my notifications and have Action button for "dismiss", "snooze" etc.!

chrisonline
  • 6,949
  • 11
  • 42
  • 62
  • Could you paste your code? I guess you always use the same notification id when you call NotificationManagerCompat.notify(). – Poly Jul 08 '14 at 18:22
  • I have tested it. If I remove FLAG_NO_CLEAR it is working. So no problem with notification id. And it is working on the phone since years. So it seems really that this is not supported on Android Wear... – chrisonline Jul 08 '14 at 18:24

2 Answers2

24

Notification flag FLAG_NO_CLEAR basically makes your notification "ongoing". Ongoing notifications posted from phone will NOT be displayed on the wearable device.

You have two solutions to your problem - both of them have advantages and disadvantages. Please read text below and decide which solution will solve your situation better:)

Solution 1 - use group:

You can make use of group feature of Android Wear framework. It's basically created to post many (grouped) notifications on wearable device and one summary notification on phone. But using this mechanism you can also post one ongoing notification on your phone and second notification only on wear. You will end up with one ongoing notification on your phone and one normal notification on your wearable device.

    final NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);

    // This notification will be shown only on phone
    final NotificationCompat.Builder phoneNotificationBuilder = new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.ic_launcher)
        .setContentTitle("Title phone")
        .setContentText("Text phone")
        .setOngoing(true)
        .setOnlyAlertOnce(true)
        .setGroup("GROUP")
        .setGroupSummary(true);

    // This notification will be shown only on watch
    final NotificationCompat.Builder wearableNotificationBuilder = new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.ic_launcher)
        .setContentTitle("Title wearable")
        .setContentText("Text wearable")
        .setOngoing(false)
        .setOnlyAlertOnce(true)
        .setGroup("GROUP")
        .setGroupSummary(false);

    notificationManager.notify(0, phoneNotificationBuilder.build());
    notificationManager.notify(1, wearableNotificationBuilder.build());

This method will allow you to post an "alternative" notification for watch only, keeping your ongoing notification on phone. But (as mentioned earlier) the notification on watch cannot be ongoing - it has to be a normal notification. If you really want to have a true ongoing notification on watch you'll need to go for second solution.

Please read more about grouping (stacking) notifications here:
https://developer.android.com/training/wearables/notifications/stacks.html

Solution 2 - create wearable app:

Ongoing notifications from phone won't be shown on watch, but you can create wearable part of your Android application and post your notification directly on Android Wear. You can easily post an ongoing notification from there, but it won't be the same notification as is on phone. You will need to sync them between both devices.

Please read more about DataApi here:
https://developer.android.com/training/wearables/data-layer/index.html
https://developer.android.com/training/wearables/data-layer/data-items.html

You can also take a look at my post where I've posted a code demonstrating how to use a DataApi in practise:
https://stackoverflow.com/a/24896043/3827276

Community
  • 1
  • 1
Maciej Ciemięga
  • 10,125
  • 1
  • 41
  • 48
  • Thanks again for your great info. I have now tried it and it is working if I use .setOngoing(true) for my phone notification, but if I use Notification FLAG -> FLAG_NO_CLEAR it does not work. On the watch there is no notification. So I have to add a wearable app I think. – chrisonline Nov 17 '14 at 20:38
  • Not sure if you still care about WearOS, but do you have an update for Android Wear 2.x? – rekire Jan 15 '22 at 13:24
1

Honestly something really weird changed in wear 1.5 so a solution would be to set an alarm

Uri alarmSound = getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);


    Intent displayIntent = new Intent(this, WearNotif.class);
    //displayIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent displayPendingIntent = PendingIntent.getActivity(this,
            0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    Notification notification = new Notification.Builder(this)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle("WeirdShit")
            .setContentText("some random crap")
            .extend(new Notification.WearableExtender()
                    .setDisplayIntent(displayPendingIntent))
            .setSound(alarmSound)
            .build();

    NotificationManagerCompat notificationManager =
            NotificationManagerCompat.from(this);
    notificationManager.notify(25454, notification);