【问题标题】:Different text for the wear device and the mobile device穿戴设备和移动设备的不同文本
【发布时间】:2014-12-20 09:11:04
【问题描述】:

是否可以在 Android Wear 设备和移动设备中显示不同文本(内容标题和内容文本)的通知?

【问题讨论】:

    标签: android android-notifications wear-os


    【解决方案1】:

    暂时没有。但是,您可以通过以下方式实现此效果:

    1. 在手机上发布通知setLocalOnly(true)
    2. 使用描述通知和更改文本的DataAPI 发布DataItem
    3. 当可穿戴设备收到DataItem时,用不同的文字发布通知,再次设置setLocalOnly(true)
    4. 在每次通知时,也请致电 setDeleteIntent,以便您知道,当有被解雇时
    5. 当通知被忽略时,从第 2 点删除 DataItem
    6. DataItem被删除时,您会收到回调;删除剩余的通知

    这里可能有一些我没有立即看到的极端情况,但一般方法应该可以让你实现你想要的。

    【讨论】:

    • 非常感谢!我会试一试。您认为使用 DataItem 而不是 Message 更好吗?
    • 还有一件事,这意味着我必须在我的应用程序中添加一个磨损模块,对吧? (尽管我只对通知感兴趣......)
    • 是的,确实是这个意思。
    • 我很害怕...无论如何,我还找到了一个 google 的示例项目,它几乎可以按照您所说的那样做:github.com/gdgbogota/codelab-synchronized-notifications。感谢您的帮助!
    【解决方案2】:

    是的,现在可以通过 Android Wear 上的一些小技巧和错误来帮助我们。以上这个技巧不需要Android Wear API,只要一个普通的带有RemoteViews的Notification就足够了。

    NotificationCompat.Builder mBuilder;
        mBuilder = new NotificationCompat.Builder(context)
        .setSmallIcon(R.drawable.ic_launcher)
        .setAutoCancel(true)
        .setContentText("This msg won't display in your phone, only on wear you can see.")
        .setContentTitle("Hello")
    
        .setContentIntent(
                PendingIntent.getActivity(context, 10,intent,PendingIntent.FLAG_ONE_SHOT));
    
        NotificationManager mNM = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = mBuilder.build();
    
        RemoteViews contentView = new RemoteViews(context.getPackageName(),
        R.layout.notification_layout);
    
        contentView.setTextViewText(R.id.noti_text,"This message won't display in your wear device, only on phone you can see.");
        contentView.setImageViewResource(R.id.noti_image,R.drawable.ic_launcher);
        notification.contentView = contentView;
    
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
    
        mNM.notify(50, notification);
    

    现在在您的设备上运行该应用程序并检查手表和手机上的通知,RemoteViews 中的内容不会显示在您的手表中,但在手机上会显示,如果您删除 .setContentTitle() 和 .setContentText (),那么 RemoteViews 的内容也会同时显示在手表和手机上。

    【讨论】:

      【解决方案3】:

      其实你可以使用

      .setGroup(GROUP_KEY) .setGroupSummary(true)

      在您的手机通知上。然后用你想在手表上显示的数据创建一个通知并设置

      .setGroup(GROUP_KEY)

      到您的手机通知所在的同一组。这用于显示堆栈通知,但如果您只使用它,那么它就可以了。

      完整文档:Stacking Notifications

      【讨论】:

        猜你喜欢
        • 2020-04-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多