54
votes

I can't seem to be able to create an Android Wear notification that updates with out blinking the app icon whereas the same code works fine on an Android phone.

Most referenced solutions talk about updating the same notification, use setAlertOnlyOnce, keeping the ID or when the same. However, whatever I do, every time the notification is updated it blinks (most noted by the App Icon).

As suggested here Android Wear: Timer like notification card on wear device you can use setHintHideIcon(true) to hide the app icon, which hides the blinking part, however in the limited world of Android Wear Notifications the app icon plays a large part in the branding of the application.

If you want a timer, you can use .setUsesChronometer(true) and let the system update the timer which works perfectly. Unfortunately if you want to update something else than time (like steps or messages received count) it seems to me you're out of luck.

Below you can find code that works fine when run as a phone app, but blinks when run as a wearable app.

Commented line below to demonstrate that the notification still blinks (when running on wear, not the phone) that the notification still blinks when posting an unchanged notification on the wearable. Uncomment to update the notification again.

mNotification = buildNotification(WearMainActivity.this);

Therefore my question is if anyone has any further idea's we can explore to keep the notification from blinking or if we can write this down as an Android Wear bug?

public class WearMainActivity extends Activity {

    public final int NOTIFICATION_ID= 1;
    public Notification mNotification;
    public int count;
    public long when;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        count = 0;
        when = System.currentTimeMillis();
        mNotification = buildNotification(WearMainActivity.this);
        postDelayedHandler();
        finish();
    }

    private void postDelayedHandler(){

        new Handler().postDelayed(new Runnable() {
            public void run() {
                count++;
                mNotification = buildNotification(WearMainActivity.this);
                NotificationManager notifyMgr = ((NotificationManager) getSystemService(NOTIFICATION_SERVICE));
                notifyMgr.notify(NOTIFICATION_ID, mNotification);
                postDelayedHandler();
            }
        }, 1000L);
    }

    private Notification buildNotification(Context context){
        return new Notification.Builder(context)
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle(context.getString(R.string.app_name))
                .setContentText("Count: "+count)
                .setWhen(when)
//                .setOngoing(true) //Don't do this, adds "Mute app" action
                .setOnlyAlertOnce(true)
                .setPriority(Notification.PRIORITY_MAX)
                .extend(new Notification.WearableExtender()
//                        .setHintHideIcon(true) //Hides the icon, so kinda hides the blink
                )
                .build();
    }
}

Tested on:
Wearable: Moto 360 (4.4W2) Wear Emulator (5.0.1)
Phones: Galaxy Nexus (4.3) and Nexus 5 (5.0.0)

Occurs: When running as a Wearable app or as phone notification displayed on Wearable. Works perfect on phone.

Referenced questions:
How can I avoid blinking notification update while changing button
Updating an ongoing notification quietly
How to properly update a notification post api 11?

2
I'm sorry but I didn't quite understand -- does this also happen with regular notifications in phone apps (no wear component) that are automatically mirrored to the watch? Because I tested this scenario with your code and saw no flickering (Moto 360 with Android 5.0).matiash
Ah thanks for noticing that. Apparently when you update a notification on your phone the system is smart enough not to send an unchanged notification to the Wearable. To clarify I've made a video of the behaviour: youtube.com/watch?v=8QBuUjb_vTALearnDriver
Update: This doesnt seem to happen on a Gear Live running 5.0. So two options: a) It doesnt flicker there due to its better hardware b) It might fixed in 5.0. Now waiting for my watch to receive the update to confirm either a or b... (Oh i wish I could just sideload the OTA ;) )LearnDriver
I would bet on (b). We shall see :)matiash
So I just got the 5.0 update (took em long enough!) and the answer is: yes and no! When you get an extended notification from an app that's started on the phone it works seamless without any blinking. However, if you start an app from the watch it will still blink. So now we have two different behaviours based on where the notification originated...LearnDriver

2 Answers

1
votes

Replace:

NotificationManager notifyMgr = 
    ((NotificationManager)getSystemService(NOTIFICATION_SERVICE));

to:

NotificationManagerCompat notifyMgr =
    NotificationManagerCompat.from(this);

More informations: https://developer.android.com/training/wearables/notifications/creating.html

You also making a lot of updates. Every update is send via bluetooth to Wear. You should create self-install app to Android Wear. The delay of sending is about 3 seconds.

1
votes

I solved this problem a while back when I was working with Android Wear but the code in unfortunately gone. Anyway, what I did was to not BUILD the notification every time I wanted to update it, I just tagged the notification the first time I created it and then I retrieved it with the TAG and made my changes directly to that object. That completely stopped the flickering...