如何在 Sony Xperia 设备上向应用程序图标添加通知标记/计数? [英] How to add a notification badge/count to application icon on Sony Xperia devices?

查看:44
本文介绍了如何在 Sony Xperia 设备上向应用程序图标添加通知标记/计数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

借助 Sony 的 Xperia Home,某些应用能够在应用图标上显示计数气泡或徽章.Facebook 和 Facebook Messenger 以及内置的电子邮件应用程序都可以这样做.

With Sony's Xperia Home, certain apps have the ability to display a count bubble or badge on the app icon. Facebook and Facebook Messenger both do this, as well as the built in Email app.

这已经解决了三星的启动器,但我还没有遇到任何有关如何为索尼的启动器执行此操作的文档.

This has been solved for Samsung's launcher, but I have not come across any documentation on how to do it for Sony's launcher.

怎么做?

推荐答案

在看到 Daniel Ochoa 为三星启动器提供的解决方案后,我开始为索尼的 Xperia Home 做同样的事情,该解决方案使用 BadgeProvider 来处理徽章.这个答案是直接取的 来自我的博客.

After having seen Daniel Ochoa's solution for Samsung's launcher, which uses a BadgeProvider to handle the badges, I set out to do the same for Sony's Xperia Home. This answer is taken directly from my blog.

我偶然发现了 Sony 的 AppXplore 和用它来检查 Facebook 应用程序的权限.Facebook 应用请求以下权限,这是在 Sony 设备上显示徽章的关键:

I stumbled upon Sony's AppXplore and used it to check out the permission's of the Facebook app. The Facebook app requests the following permission, which is the key to displaying badges on Sony devices:

com.sonyericsson.home.permission.BROADCAST_BADGE

com.sonyericsson.home.permission.BROADCAST_BADGE

接下来,我查看了所有可用的内容提供商但我在那里没有发现与应用程序图标徽章相关的任何内容.我在这个答案中运行了命令来获取系统转储文件并使用 Notepad++ 搜索徽章".我发现了这个:

Next, I had a look through all available content providers but I found nothing related to app icon badges there. I ran the command in this answer to get a system dump file and searched for "badge" using Notepad++. I found this:

com.sonyericsson.home.action.UPDATE_BADGE:41be9a90 com.sonyericsson.home/.BadgeService$BadgeReceiver 过滤器 41be9858

com.sonyericsson.home.action.UPDATE_BADGE: 41be9a90 com.sonyericsson.home/.BadgeService$BadgeReceiver filter 41be9858

因此,它是使用 Sony 上的 BroadcastReciever 处理的,而不是使用三星的 Content Provider.因此,我创建了一个我自己的虚拟 BroadcastReciever,监听 com.sonyericsson.home.action.UPDATE_BADGE 的动作,并发现传递给 Sony 的 BadgeService 的额外内容.为此,我还需要一个权限,但在转储文件中很容易找到:

So, it's handled using a BroadcastReciever on Sony as opposed to Samsung's Content Provider. So, I created a dummy BroadcastReciever of my own, listening for the action com.sonyericsson.home.action.UPDATE_BADGE, and found the extras passed to Sony's BadgeService. For this, I also needed a permission, but that was easy to find in the dump file:

com.sonyericsson.home.permission.RECEIVE_BADGE

com.sonyericsson.home.permission.RECEIVE_BADGE

Facebook、电子邮件应用等发送的额外内容是:

The extras sent by Facebook, the Email app, etc, are:

  • com.sonyericsson.home.intent.extra.badge.ACTIVITY_NAME -您的应用程序的主要活动的名称,android.intent.action.MAIN.这是这样启动器就知道要在哪个图标上显示徽章.
  • com.sonyericsson.home.intent.extra.badge.SHOW_MESSAGE - a指示我们是否要显示徽章的布尔值(我们这样做!)
  • com.sonyericsson.home.intent.extra.badge.MESSAGE - 一个字符串(不是整数 - 我花了一段时间才意识到......)与数字显示.
  • com.sonyericsson.home.intent.extra.badge.PACKAGE_NAME - 您的应用程序包的名称.
  • com.sonyericsson.home.intent.extra.badge.ACTIVITY_NAME - The name of your app's main activity, android.intent.action.MAIN. This is so the launcher knows which icon to show the badge on.
  • com.sonyericsson.home.intent.extra.badge.SHOW_MESSAGE - a boolean indicating if we want to show the badge or not (which we do!)
  • com.sonyericsson.home.intent.extra.badge.MESSAGE - a string (not an integer - that took me a while to realize...) with the number to show.
  • com.sonyericsson.home.intent.extra.badge.PACKAGE_NAME - The name of your application package.

因此,事实证明在启动器中的应用程序图标上显示徽章非常简单.IMO 比三星的发射器更直接.这是分步指南(而且不长!)

So, it turns out it's very simple to show a badge on your application icon in the launcher. IMO it's much more straight-forward than for Samsung's launcher. Here's a step-by-step-guide (and it's not long!)

  1. 在清单文件中声明 com.sonyericsson.home.permission.BROADCAST_BADGE 权限:

BadgeReceiver广播一个Intent:

Intent intent = new Intent();

intent.setAction("com.sonyericsson.home.action.UPDATE_BADGE");
intent.putExtra("com.sonyericsson.home.intent.extra.badge.ACTIVITY_NAME", "com.yourdomain.yourapp.MainActivity");
intent.putExtra("com.sonyericsson.home.intent.extra.badge.SHOW_MESSAGE", true);
intent.putExtra("com.sonyericsson.home.intent.extra.badge.MESSAGE", "99");
intent.putExtra("com.sonyericsson.home.intent.extra.badge.PACKAGE_NAME", "com.yourdomain.yourapp");

sendBroadcast(intent);

  • 完成.一旦这个 Intent 被广播,启动器应该在你的应用程序图标上显示一个徽章.

  • Done. Once this Intent is broadcast the launcher should show a badge on your application icon.

    要再次移除徽章,只需发送一个新的广播,这次将 SHOW_MESSAGE 设置为 false:

    To remove the badge again, simply send a new broadcast, this time with SHOW_MESSAGE set to false:

    intent.putExtra("com.sonyericsson.home.intent.extra.badge.SHOW_MESSAGE", false);
    

  • <小时>

    很高兴知道

    消息是一个字符串!

    由于 MESSAGE 是一个 String,您实际上可以在徽章中添加文字:


    Good to know

    The message is a string!

    Since MESSAGE is a String, you can actually add words to the badge:

    intent.putExtra("com.sonyericsson.home.intent.extra.badge.MESSAGE", "Testing");
    

    但我不会那样做,因为它看起来很奇怪.

    But I wouldn't do that 'cause it just looks weird.

    BROADCAST_BADGE 权限不仅可以让您访问自己应用的图标,还可以让您访问所有图标.例如,您可以通过以下方式设置 Facebook 的徽章:

    The BROADCAST_BADGE permission does not only give you access to your own app's icon, but to ALL of them. For example, here's how you can set Facebook's badge:

    Intent intent = new Intent();
    intent.setAction("com.sonyericsson.home.action.UPDATE_BADGE");
    intent.putExtra("com.sonyericsson.home.intent.extra.badge.ACTIVITY_NAME", "com.facebook.katana.LoginActivity");
    intent.putExtra("com.sonyericsson.home.intent.extra.badge.SHOW_MESSAGE", true);
    intent.putExtra("com.sonyericsson.home.intent.extra.badge.MESSAGE", "99");
    intent.putExtra("com.sonyericsson.home.intent.extra.badge.PACKAGE_NAME", "com.facebook.katana");
    
    sendBroadcast(intent);
    

    <小时>

    我希望这对某人有所帮助!:)


    I hope this has been of help to someone! :)

    这篇关于如何在 Sony Xperia 设备上向应用程序图标添加通知标记/计数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆