androidwear-osandroid-wear-notificationandroid-wear-2.0

Android wear 2.0 notifications


So, after the Google I/O 2016, and the new wear 2.0, i am trying to archive the same notification on my huawei watch running beta build.

Here google notification changes doc

I tried, to update from my previous way i was using notifications, but without luck. I just can't add the action button on bottom of notification.

Here what i got so far:

Bitmap background = BitmapFactory.decodeResource(getResources(), R.drawable.nav);
    Intent snoozeIntent = new Intent(getApplicationContext(), SnoozeNotification.class);
    PendingIntent piSnooze = PendingIntent.getBroadcast (getApplicationContext(), 1, snoozeIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    Bitmap resizedBitmap = Bitmap.createScaledBitmap(
            background, 150, 150, false);

    NotificationCompat.Action action_1 = new NotificationCompat.Action.Builder(R.drawable.ic_timer_white_24dp, getString(R.string.snooze), piSnooze).build();

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    builder.setContentTitle(getString(R.string.wear_notification_1));
    builder.setContentText(getString(R.string.wear_notification_2));
    builder.setVibrate(new long[]{2000});
    builder.setPriority(Notification.PRIORITY_MAX);
    builder.setSmallIcon(R.mipmap.ic_launcher);
    builder.setAutoCancel(true);
    builder.addAction(R.drawable.ic_timer, getString(R.string.snooze), piSnooze);
    builder.setLargeIcon(resizedBitmap);
    builder.extend(new android.support.v7.app.NotificationCompat.WearableExtender().addAction(action_1)).build();
    builder.build();

But i just see normal notification without any buttons etc. Someone got an idea what i am doing wrong?


Solution

  • When you call .build(), you need to save the result into a variable, right now the result is just being lost. Here is your current code:

    builder.extend(new android.support.v7.app.NotificationCompat.WearableExtender().addAction(action_1)).build();
    builder.build();
    

    You need to do something like this:

    builder.extend(new android.support.v7.app.NotificationCompat.WearableExtender().addAction(action_1));
    Notification n = builder.build();
    

    Then you need to still actually issue the notification. The sample you linked to does this correctly, so you might want to cut and paste that as a good starting point, get that working first, then make changes.