Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Notifications for Android Wear

Notifications for Android Wear

How to make normal Android smartphone notifications look nice on Android Wear Smartwatches.

Janusz Leidgens

November 09, 2014
Tweet

Other Decks in Programming

Transcript

  1. 2

  2. Notifications on Android! NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
 
 builder.setContentTitle("Title");


    builder.setSmallIcon(R.drawable.ic_message);
 builder.setContentIntent(
 PendingIntent.getActivity(this, 0, new Intent(this, ActivateActivity.class), PendingIntent.FLAG_UPDATE_CURRENT));
 builder.setContentText(notificationText);
 builder.setLargeIcon(
 BitmapFactory.decodeResource(getResources(), R.drawable.large_icon));
 
 notificationManager.notify(messageIndex, builder.build());
  3. Nice Notifications! NotificationCompat.WearableExtender  extender  =      new  NotificationCompat.WearableExtender();  

      extender.setBackground(BitmapFactory.decodeResource(    getResources(),  R.drawable.notif_background));   extender.setContentIcon(R.drawable.ic_message);   extender.setHintHideIcon(true);     extender.extend(builder);! §  Android Wear cards have 400 x 400 background that should be placed into the drawable-nodpi folder!
  4. Grouping Notifications! NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
 
 for (String

    notification : notifications) {
 inboxStyle.addLine(notification);
 groupIndex++;
 createStackedNotification(notificationManager, 
 notification, messageIndex + groupIndex);
 } 
 builder.setStyle(inboxStyle);
 builder.setGroup(GROUP_KEY);
 builder.setGroupSummary(true);
  5. Grouping Notifications! private void createStackedNotification(
 NotificationManagerCompat notificationManager, String notification, int

    stackedId) {
 NotificationCompat.Builder stackedCardBuilder = 
 new NotificationCompat.Builder(this);
 
 stackedCardBuilder.setContentTitle(getTitle());
 stackedCardBuilder.setSmallIcon(R.drawable.ic_message);
 stackedCardBuilder.setContentIntent(getIntent());
 stackedCardBuilder.setLargeIcon(getLargeIcon());
 stackedCardBuilder.setContentText(notification);
 
 stackedCardBuilder.setGroup(GROUP_KEY);
 
 notificationManager.notify(
 stackedId, stackedCardBuilder.build());
 }
  6. Grouping Notifications! •  Notifications with a group id that are

    not a summary are not shown on the phone.! •  To have notifications only on the phone: setLocalOnly(true)!
  7. Adding pages to notifications! NotificationCompat.Builder  baseNotification  =      generateBaseNotification(notificationText);

      NotificationCompat.WearableExtender  baseExtender  =      new  NotificationCompat.WearableExtender();     NotificationCompat.Builder  shortBio  =      new  NotificationCompat.Builder(this);   shortBio.setContentText(getBioText());   NotificationCompat.WearableExtender  shortBioExtender  =      new  NotificationCompat.WearableExtender();   shortBioExtender.setBackground(getBioBackground());     shortBioExtender.extend(shortBio);   baseExtender.addPage(shortBio.build());     baseExtender.extend(baseNotification);   notificationManager.notify(messageIndex,    baseNotification.build());
  8. Adding Wear only Actions! Intent  tagService  =  new  Intent(this,  TagService.class);

      tagService.putExtra(INDEX,  messageIndex);   String  title  =  "Tag  for  later“;   PendingIntent  actionIntent  =  PendingIntent.getService(this,    messageId,  tagService,    PendingIntent.FLAG_UPDATE_CURRENT);     NotificationCompat.Action  phoneOnlyAction  =      new  NotificationCompat.Action(      R.drawable.tag,      title,        actionIntent);     extender.addAction(phoneOnlyAction);
  9. Voice actions! RemoteInput.Builder remoteInputBuilder = 
 new RemoteInput.Builder(EXTRA_VOICE_REPLY);
 
 remoteInputBuilder.setLabel("Speak

    a note");
 remoteInputBuilder.setChoices(
 new String[]
 {"Interesting", "Share", "Read up on this"});
 
 NotificationCompat.Action.Builder actionBuilder = 
 new NotificationCompat.Action.Builder(
 R.drawable.write_note,
 "Speak a note",
 pendingIntent);
 
 actionBuilder.addRemoteInput(remoteInputBuilder.build());
 extender.addAction(actionBuilder.build());
  10. Reacting to voice Actions! @Override
 protected void onCreate(Bundle savedInstanceState) {


    super.onCreate(savedInstanceState);
 CharSequence note = getMessageText(getIntent());
 if (!TextUtils.isEmpty(note)) {
 saveNote(note);
 finish();
 } else {
 setupRegularUi();
 }
 }
  11. Reacting to voice actions! private CharSequence getMessageText(Intent intent) {
 Bundle

    remoteInput = RemoteInput.getResultsFromIntent(intent);
 
 if (remoteInput != null) {
 return remoteInput.getCharSequence(
 SendNotificationService.EXTRA_VOICE_REPLY);
 }
 return null;
 }
  12. Links! •  Example Projects: https://github.com/jleidgens/ NotificationTestApp •  http://developer.android.com/guide/topics/ui/notifiers/ notifications.html • 

    http://developer.android.com/training/wearables/notifications/ creating.html •  http://developer.android.com/training/wearables/notifications/ voice-input.html