【问题标题】:Android Wear Notification is not displayed if FLAG_NO_CLEAR is used如果使用 FLAG_NO_CLEAR,则不显示 Android Wear 通知
【发布时间】:2014-07-08 12:29:56
【问题描述】:

如果使用标志“FLAG_NO_CLEAR”,则通知将显示在 Android Wear 上。

有人知道原因或解决方法吗?我没有在文档中找到任何信息。

我需要在我的通知上标记“FLAG_NO_CLEAR”,并有“关闭”、“暂停”等操作按钮!

【问题讨论】:

  • 你能粘贴你的代码吗?我猜你在调用 NotificationManagerCompat.notify() 时总是使用相同的通知 ID。
  • 我已经测试过了。如果我删除 FLAG_NO_CLEAR 它正在工作。所以通知ID没有问题。多年来,它一直在手机上工作。因此,Android Wear 似乎确实不支持此功能...

标签: android wear-os


【解决方案1】:

通知标志FLAG_NO_CLEAR 基本上使您的通知“持续进行”。从手机发布的持续通知将不会显示在可穿戴设备上。

你有两个解决你的问题 - 他们都有优点和缺点。请阅读下面的文字并决定哪种解决方案能更好地解决您的情况:)

解决方案 1 - 使用组:

您可以使用 Android Wear 框架的group 功能。它基本上是为了在可穿戴设备上发布许多(分组)通知和在手机上发布一个summary 通知而创建的。但是使用这种机制,您还可以在手机上发布一个ongoing 通知,而仅在佩戴时发布第二个通知。您最终会在手机上收到一条ongoing 通知,在可穿戴设备上收到一条正常通知。

    final NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);

    // This notification will be shown only on phone
    final NotificationCompat.Builder phoneNotificationBuilder = new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.ic_launcher)
        .setContentTitle("Title phone")
        .setContentText("Text phone")
        .setOngoing(true)
        .setOnlyAlertOnce(true)
        .setGroup("GROUP")
        .setGroupSummary(true);

    // This notification will be shown only on watch
    final NotificationCompat.Builder wearableNotificationBuilder = new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.ic_launcher)
        .setContentTitle("Title wearable")
        .setContentText("Text wearable")
        .setOngoing(false)
        .setOnlyAlertOnce(true)
        .setGroup("GROUP")
        .setGroupSummary(false);

    notificationManager.notify(0, phoneNotificationBuilder.build());
    notificationManager.notify(1, wearableNotificationBuilder.build());

此方法将允许您发布仅用于观看的“替代”通知,将您的ongoing 通知保留在手机上。但是(如前所述)手表上的通知不能是ongoing - 它必须是正常的通知。如果你真的想在手表上有一个真正的ongoing 通知,你需要寻求第二种解决方案。

请在此处阅读有关分组(堆叠)通知的更多信息:
https://developer.android.com/training/wearables/notifications/stacks.html

解决方案 2 - 创建可穿戴应用:

来自手机的持续通知不会显示在手表上,但您可以创建 Android 应用程序的可穿戴部分并直接在 Android Wear 上发布通知。您可以轻松地从那里发布正在进行的通知,但它与手机上的通知不同。您需要在两台设备之间同步它们。

请在此处阅读有关DataApi 的更多信息:
https://developer.android.com/training/wearables/data-layer/index.html
https://developer.android.com/training/wearables/data-layer/data-items.html

您还可以查看我的帖子,其中我发布了一段代码,演示了如何在实践中使用 DataApi
https://stackoverflow.com/a/24896043/3827276

【讨论】:

  • 再次感谢您的精彩信息。我现在已经尝试过了,如果我将 .setOngoing(true) 用于我的电话通知,它就可以工作,但是如果我使用 Notification FLAG -> FLAG_NO_CLEAR 它就不起作用。手表上没有通知。所以我认为我必须添加一个可穿戴应用程序。
  • 不确定您是否仍然关心 WearOS,但您有 Android Wear 2.x 的更新吗?
【解决方案2】:

老实说,磨损 1.5 发生了一些非常奇怪的变化 所以解决办法是设置闹钟

Uri alarmSound = getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);


    Intent displayIntent = new Intent(this, WearNotif.class);
    //displayIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent displayPendingIntent = PendingIntent.getActivity(this,
            0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    Notification notification = new Notification.Builder(this)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle("WeirdShit")
            .setContentText("some random crap")
            .extend(new Notification.WearableExtender()
                    .setDisplayIntent(displayPendingIntent))
            .setSound(alarmSound)
            .build();

    NotificationManagerCompat notificationManager =
            NotificationManagerCompat.from(this);
    notificationManager.notify(25454, notification);

【讨论】:

  • 调用需要 api 级别 20 分钟
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多