wayne piekarski Porting notification code over to Android Wear
Converting code from the public preview to the final Android Wear SDK

 
Share Blog article posted July 2014

Back in April 2014, I created two DevByte videos for Android Wear notifications. These videos were based on the public preview SDK, which contained support for an early API that is not supported in the final SDK released at Google I/O. I produced an Android Developers blog article about this new SDK, and how to convert your code over to the stable API:

Android Developers Blog Post

Here are the two DevByte videos, but note that the code samples are slightly out of date since they were based on the public preview:

If you are looking for a sample of how to use Stacks and Pages with the Android Wear SDK, I have provided a complete example that you can download and try out:

Full source code to MainActivity.java

package testing.blog.waynepie.blogtesting;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import android.app.Notification;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;

// Extra dependencies needed for the pages example
import java.util.ArrayList;
import java.util.List;
import android.support.v4.app.NotificationCompat.BigTextStyle;

public class MainActivity extends ActionBarActivity {

    @Override
	protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_blog_testing);

        // Only enable one of these notification methods at a time, comment the other out
        showStackNotifications();
        // showPageNotifications();
    }


    @Override
	public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.blog_testing, menu);
        return true;
    }

    @Override
	public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }


    //
    // Stacks implementation
    // http://android-developers.blogspot.com/2014/05/stacking-notifications-for-android-wear.html
    //
    public void showStackNotifications() {
        Bitmap bitmapMila = BitmapFactory.decodeResource(getResources(), R.drawable.mila128);

        // Nuke all previous notifications and generate unique ids
        NotificationManagerCompat.from(this).cancelAll();
        int notificationId = 0;

        // String to represent the group all the notifications will be a part of
        final String GROUP_KEY_MESSAGES = "group_key_messages";

        // Group notification that will be visible on the phone
        Notification summaryNotification = new NotificationCompat.Builder(this)
	    .setContentTitle("2 Pet Notifications")
	    .setContentText("Mila and Dylan both sent messages")
	    .setSmallIcon(R.drawable.ic_launcher)
	    .setLargeIcon(bitmapMila)
	    .setGroup(GROUP_KEY_MESSAGES)
	    .setGroupSummary(true)
	    .build();

        // Separate notifications that will be visible on the watch
        Intent viewIntent1 = new Intent(this, MainActivity.class);
        PendingIntent viewPendingIntent1 =
	    PendingIntent.getActivity(this, notificationId+1, viewIntent1, 0);
        Notification notification1 = new NotificationCompat.Builder(this)
	    .addAction(R.drawable.ic_action_done, "Treat Fed", viewPendingIntent1)
	    .setContentTitle("Message from Mila")
	    .setContentText("What's for dinner? "
			    + "Can we have steak?")
	    .setSmallIcon(R.drawable.ic_launcher)
	    .setGroup(GROUP_KEY_MESSAGES)
	    .build();

        Intent viewIntent2 = new Intent(this, MainActivity.class);
        PendingIntent viewPendingIntent2 =
	    PendingIntent.getActivity(this, notificationId+2, viewIntent2, 0);
        Notification notification2 = new NotificationCompat.Builder(this)
	    .addAction(R.drawable.ic_action_done, "Water Filled", viewPendingIntent2)
	    .setContentTitle("Message from Dylan")
	    .setContentText("Can you refill our water bowl?")
	    .setSmallIcon(R.drawable.ic_launcher)
	    .setGroup(GROUP_KEY_MESSAGES)
	    .build();

        // Issue the group notification
        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
        notificationManager.notify(notificationId+0, summaryNotification);

        // Issue the separate wear notifications
        notificationManager.notify(notificationId+2, notification2);
        notificationManager.notify(notificationId+1, notification1);
    }

    //
    // Pages implementation
    // http://android-developers.blogspot.com/2014/05/another-easy-sample-for-notification.html
    //
    public void showPageNotifications() {
        // Nuke all previous notifications and generate unique ids
        NotificationManagerCompat.from(this).cancelAll();
        int notificationId = 0;

        // Titles, authors, and overdue status of some books to display
        String[] titles = { "How to survive with no food",
			    "Sailing around the world",
			    "Navigation on the high seas",
			    "Avoiding sea monsters",
			    "Salt water distillation",
			    "Sail boat maintenance" };
        String[] authors = { "I. M. Hungry",
			     "F. Magellan",
			     "E. Shackleton",
			     "K. Kracken",
			     "U. R. Thirsty",
			     "J. Macgyver" };
        Boolean[] overdue = { true, true, true, true, true, false };
        List extras = new ArrayList();

        // Extra pages of information for the notification that will
        // only appear on the wearable
        int numOverdue = 0;
        for (int i = 0; i < titles.length; i++) {
            if (!overdue[i]) continue;
            BigTextStyle extraPageStyle = new NotificationCompat.BigTextStyle();
            extraPageStyle.setBigContentTitle("Overdue Book " + (i+1))
		.bigText("Title: " + titles[i] + ", Author: " + authors[i]);
            Notification extraPageNotification = new NotificationCompat.Builder(this)
		.setStyle(extraPageStyle)
		.build();
            extras.add(extraPageNotification);
            numOverdue++;
        }

        // Main notification that will appear on the phone handset and the wearable
        Intent viewIntent1 = new Intent(this, MainActivity.class);
        PendingIntent viewPendingIntent1 =
	    PendingIntent.getActivity(this, notificationId+1, viewIntent1, 0);
        Notification notification1 = new NotificationCompat.Builder(this)
	    .addAction(R.drawable.ic_action_done, "Returned", viewPendingIntent1)
	    .setContentTitle("Books Overdue")
	    .setContentText("You have " + numOverdue + " books due at the library")
	    .setSmallIcon(R.drawable.ic_launcher)
	    .extend(new NotificationCompat.WearableExtender().addPages(extras))
	    .build();

        // Issue the notification
        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
        notificationManager.notify(notificationId+1, notification1);
    }

}

Share Blog article posted July 2014


Google Developer Advocate 2014-2023


X-Plane plugins and apps for flight simulation


IoT water meter monitoring


IoT computer vision monitoring


Tiny and cheap offline Wikipedia project 2017


Outdoor augmented reality research
Tinmith 1998-2007


Outdoor augmented reality 3D modelling
Tinmith 1998-2007


Outdoor augmented reality gaming
ARQuake 1999-2007


Scanned physical objects outdoors
Hand of God 3D 2006


Google Developer Advocate 2014-2023


X-Plane plugins and apps for flight simulation


IoT water meter monitoring


IoT computer vision monitoring


Tiny and cheap offline Wikipedia project 2017


Outdoor augmented reality research
Tinmith 1998-2007


Outdoor augmented reality 3D modelling
Tinmith 1998-2007


Outdoor augmented reality gaming
ARQuake 1999-2007


Scanned physical objects outdoors
Hand of God 3D 2006


Contact Wayne Piekarski via email wayne AT tinmith.net for more information

Last Updated 2023