简体   繁体   English

如果我已经发布了 5 个快捷方式,如何在 Android 11 上发布对话通知?

[英]How do I issue conversation notifications on Android 11, if I already have published 5 shortcuts?

According to “People and conversations” , one of the prerequisites for a conversation notification on Android 11 is the following:根据“人员和对话” ,Android 11 上的对话通知的先决条件之一如下:

The notification is associated with a valid long-lived dynamic or cached sharing shortcut.该通知与有效的长期动态或缓存的共享快捷方式相关联。 The notification can set this association by calling setShortcutId() or setShortcutInfo() .通知可以通过调用setShortcutId()setShortcutInfo()来设置此关联。

But I can only have 5 shortcuts .我只能有 5 个快捷方式 Does this mean that I can't make conversations notifications for more than 5 people?这是否意味着我不能为超过 5 人发送对话通知?

It appears that you can actually publish more than 5 shortcuts.看来您实际上可以发布超过 5 个快捷方式。 You can make notifications which would rank lower than all others by setting rank to a high number, and publish them using shortcutManager.pushDynamicShortcut() .您可以通过将rank设置为高数字来制作排名低于所有其他通知的通知,并使用shortcutManager.pushDynamicShortcut()发布它们。

In case you don't have any launcher shortcuts, the above action will create one.如果您没有任何启动器快捷方式,上述操作将创建一个。 If you don't want that, it's recommended to... simply remove the shortcut you just created :如果您不希望这样,建议...只需删除您刚刚创建的快捷方式

Q: Will my shortcuts appear in the long press app launcher context menu?问:我的快捷方式会出现在长按应用启动器上下文菜单中吗? A: They can depending on their rank, but if you prefer not having shortcuts appear on launcher you can remove the shortcut with ShortcutManager#removeDynamicShortcuts() or #removeAllDynamicShortcuts() after sending the notification.答:他们可以根据他们的等级,但如果您不希望在启动器上显示快捷方式,您可以在发送通知后使用ShortcutManager#removeDynamicShortcuts()#removeAllDynamicShortcuts()删除快捷方式。 You can also rank other app shortcuts with higher ranking, so only those dynamic shortcuts appear on launcher.您还可以对排名较高的其他应用快捷方式进行排名,因此只有那些动态快捷方式才会出现在启动器上。


Some thoughts follow below.下面是一些想法。 It appears that shortcuts on a regular phone will appear in three places:普通手机上的快捷方式似乎会出现在三个地方:

  • launcher— either by long pressing app icon, or pinned;启动器——通过长按应用程序图标或固定;
  • direct share;直接分享;
  • conversation notifications.对话通知。

These shortcuts all come from the same pool.这些快捷方式都来自同一个池。 It makes sense, but as there is only a single rank for every shortcut this means that the launcher list and the direct share list are the same.这是有道理的,但由于每个快捷方式只有一个等级,这意味着启动器列表和直接共享列表是相同的。 This is something you might not want;这是您可能不想要的; you might be sharing more to one conversation but opening another one more often.您可能会在一个对话中分享更多内容,但更频繁地打开另一个对话。 In my app, I solved this in the following way:在我的应用程序中,我通过以下方式解决了这个问题:

  • I maintain statistics of when the user shares to a contact / opens a chat with a contact我维护用户何时与联系人分享/打开与联系人聊天的统计信息
  • Whenever the list of top used/top shared contacts change, I update the shortcut using shortcutManager.pushDynamicShortcut() ;每当最常用/最常用的共享联系人列表发生变化时,我都会使用shortcutManager.pushDynamicShortcut()更新快捷方式; if a shortcut with the same id already exists, it is updated.如果已存在具有相同 id 的快捷方式,则会对其进行更新。 I use the old details from shortcutManager.getShortcuts() .我使用来自shortcutManager.getShortcuts()的旧细节。
    • depending on whether a shortcut becomes or ceases to become a direct share target, I set or unset a category根据快捷方式是否成为或不再成为直接共享目标,我设置或取消设置类别
    • specifying rank allows reordering shortcuts in the launcher指定等级允许在启动器中重新排序快捷方式
  • Whenever I need to push a notification, I check if I already have a shortcut for the conversation.每当我需要推送通知时,我都会检查我是否已经有对话的快捷方式。 If not, I simply create a new one with a rank of 10000.如果没有,我只是创建一个排名为 10000 的新的。

This allows having completely different launcher & direct share shortcuts, and to publish many a conversation notification.这允许拥有完全不同的启动器和直接共享快捷方式,并发布许多对话通知。

Some random observations:一些随机观察:

  • Adding a shortcut id to a conversation notification also adds its icon to the notification, even if you don't set one explicitly.向对话通知添加快捷方式 ID 也会将其图标添加到通知中,即使您没有明确设置。

  • shortcutManager.maxShortcutCountPerActivity actually returns 15 (,) on my device (LineageOS). shortcutManager.maxShortcutCountPerActivity实际上在我的设备 (LineageOS) 上返回 15 (,)。 even though the launcher & direct share only show the regular 4 icons.即使启动器和直接共享仅显示常规的 4 个图标。

  • If you update a shortcut name and/or icon, most of the time it will instantly update in launcher.如果您更新快捷方式名称和/或图标,大多数情况下它会立即在启动器中更新。 This includes pinned shortcuts.这包括固定的快捷方式。

  • Use IconCompat.createWithAdaptiveBitmap() to create icons that work well for various icon shapes.使用IconCompat.createWithAdaptiveBitmap()创建适用于各种图标形状的图标。 The system will keep a hold of the image, you don't need to keep it.系统会保留图像,您不需要保留它。 See the documentation for that method.请参阅该方法的文档。

    There are two other methods to be aware of:还有两种方法需要注意:

    • IconCompat.createWithAdaptiveBitmapContentUri() is the same as the above but will work with content URIs. IconCompat.createWithAdaptiveBitmapContentUri()与上述相同,但适用于内容 URI。 This is good for creating Person for notifications, as this allows not keeping the icons in memory.这对于为通知创建Person很有用,因为这允许不将图标保留在 memory 中。 However, there is seemingly no way to pass URI permissions to shortcut manager, so this method can't be used.不过貌似没有办法将URI权限传递给快捷方式管理器,所以这个方法不能用。 (Please correct me if I'm wrong here!) (如果我在这里错了,请纠正我!)

      Edit: if you cancel the notification after inline (direct) reply, the system might decide to add your reply to it instead of actually cancelling it.编辑:如果您在内联(直接)回复后取消通知,系统可能会决定将您的回复添加到它而不是实际取消它。 Apparently it might lose URI permissions at this point.显然,此时它可能会失去 URI 权限。 Calling this workaround on URIs seems to be helping:在 URI 上调用此解决方法似乎有所帮助:

       fun Uri.grantReadPermissionToSystem() { applicationContext.grantUriPermission("com.android.systemui", this, Intent.FLAG_GRANT_READ_URI_PERMISSION) }
    • IconCompat.createWithData() creates memory efficient icons from compressed image data (PNG/JPEG). IconCompat.createWithData()从压缩图像数据 (PNG/JPEG) 创建 memory 高效图标。 It's not adaptive, however.然而,它不是自适应的。 And, icons made with it don't work with ShortcutManager at all.而且,用它制作的图标根本不适用于ShortcutManager

Also don't forget to check out the readme of the People sample app .另外不要忘记查看People 示例应用程序的自述文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何更改已发布到Google Play商店的Android应用的ApplicationId,我该怎么办? - What do I have to do to change the ApplicationId of an Android App that's already published to the Google Play Store 我有在Android中发布的服务。 如何在iOS中浏览? - I have a service that is published in Android. How to browser it in iOS? 如何在 Espresso 中测试 Android 通知 - How do I test Android notifications in Espresso Android:如何连续显示2条通知 - Android: How do I show 2 notifications in a row Android推送通知教程:如何将通知发送给“关注者”? - Android push notifications tutorial: how do I send notifications to “followers”? 我可以通过React Native访问Android / iPhone通知吗? - Do I have access to Android/iPhone notifications via React Native? 如何在已发布的 android 应用上添加广告? - How do I add ads on my published android app? 如何将项目导入到android studio中,该项目实际上是我已经克隆到计算机的github存储库? - How do I import a project into android studio that is actually a github repository that I have already cloned to my computer? 为什么Android坚持要求我已经拥有操作栏? - why do android insists that I already have action bar? 如何在Android版Corona SDK中推送通知? - How do I do push notifications in Corona SDK for Android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM