David Corsalini
David Corsalini

Reputation: 8208

Android Wear notification won't show up

I'm trying to use Pages and Stacks from the Android Wear SDK preview. Without the Wear code the notifications show up fine, while if I use Wear specific code I can't get a notification, neither on the phone nor on the Wear Emulator. I've gone throught the code 10 times, I think I need a fresh pair of eyes to catch the error.

This code should create, on the phone, a Notification for each Tracker (an external device sending a Message) with a list of unread Messages (using the InboxStyle). On Wear it should stack multiple Notifications grouped by Tracker, adding a Page for each unread Message.

  public static void showNewMessagesNotif(Context context, Tracker tracker, List<Message> messages) {
    String trackerName = tracker.getName() + " - " + tracker.getPhoneNumber();
    String contentTitle = context.getResources().getQuantityString(R.plurals.notif_new_messages, messages.size(), messages.size());


    Intent callIntent = new Intent(Intent.ACTION_CALL);
    callIntent.setData(Uri.parse("tel:" + tracker.getPhoneNumber()));
    PendingIntent callPendingIntent = PendingIntent.getActivity(context, 0, callIntent, 0);


    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.ic_action_location_searching)
            .setContentTitle(contentTitle)
            .setContentText(trackerName)
            .setAutoCancel(true)
            .addAction(R.drawable.ic_action_call, context.getString(R.string.action_call), callPendingIntent);
    NotificationCompat.InboxStyle inboxStyle =
            new NotificationCompat.InboxStyle();
    // Sets a title for the Inbox style big view
    inboxStyle.setBigContentTitle(contentTitle);

    // Moves events into the big view
    for (Message message : messages) {
        inboxStyle.addLine(message.getText());
    }
    inboxStyle.setSummaryText(trackerName);
    // Moves the big view style object into the notification object.
    mBuilder.setStyle(inboxStyle);

    mBuilder.setContentIntent(getNotificationIntent(context, tracker));
    // Issue the notification here.
    NotificationManagerCompat notificationManager =
            NotificationManagerCompat.from(context);

    int notifId = (int) (NEW_MESSAGE_NOTIF_BASE_ID + tracker.getRowId());

//Android Wear Notifications
    List<Notification> wearPages = new ArrayList<Notification>();

    for (Message message : messages) {
        NotificationCompat.BigTextStyle extraPageStyle = new NotificationCompat.BigTextStyle();
        extraPageStyle.setBigContentTitle(message.getText())
                .bigText(message.getAddress());
        Notification extraPageNotification = new NotificationCompat.Builder(context)
                .setStyle(extraPageStyle)
                .build();


        wearPages.add(extraPageNotification);
    }


    WearableNotifications.Builder wearNotificationBuilder =
            new WearableNotifications.Builder(mBuilder)
                    .setHintHideIcon(true)
                    .setGroup(GROUP_BY_TRACKER).addPages(wearPages);


    // mId allows you to update the notification later on.
    notificationManager.notify(notifId, wearNotificationBuilder.build());
}

Upvotes: 0

Views: 551

Answers (1)

ianhanniballake
ianhanniballake

Reputation: 199815

For stacking notifications, you create and notify multiple notifications

  • For each stacked notification that you want to appear on Wear, notify a notification where you've called setGroup(trackerGroupId)
  • For the summary notification that you want to appear on the phone/tablet, notify a notification where you've called setGroup(trackerGroupid, WearableNotifications.GROUP_ORDER_SUMMARY)

I think your problem is that you must have a summary notification for any notifications to appear on either device.

Upvotes: 1

Related Questions