Android Wear通知无法显示

时间:2014-06-20 15:59:34

标签: android android-notifications wear-os

我尝试使用Pages预览中的StacksAndroid Wear SDK。如果没有Wear代码,通知就会显示正常,而如果我使用Wear特定代码,则无法通过电话或Wear Emulator收到通知。我已经完成了10次代码,我想我需要一双新眼睛来捕捉错误。

此代码应在手机上为每个Notification(发送Tracker的外部设备)创建Message,其中包含未读Message的列表(使用InboxStyle)。在Wear上,它应该按Notification分组多个Tracker,为每个未读Page添加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());
}

1 个答案:

答案 0 :(得分:1)

对于stacking notifications,您可以创建并notify多个通知

  • 对于您希望在Wear上显示的每个堆叠通知,notify您已拨打setGroup(trackerGroupId)的通知
  • 对于要在手机/平板电脑上显示的摘要通知,notify您已拨打setGroup(trackerGroupid, WearableNotifications.GROUP_ORDER_SUMMARY)的通知

我认为您的问题是必须有任何通知显示在任一设备上的摘要通知。