Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@waynepiekarski
Last active July 16, 2018 08:34
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save waynepiekarski/775dcd9105bd5c376eb4 to your computer and use it in GitHub Desktop.
Save waynepiekarski/775dcd9105bd5c376eb4 to your computer and use it in GitHub Desktop.
Example of using stack and page notifications with the Android Wear SDK
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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment