简体   繁体   中英

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:

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() .

But I can only have 5 shortcuts . Does this mean that I can't make conversations notifications for more than 5 people?

It appears that you can actually publish more than 5 shortcuts. You can make notifications which would rank lower than all others by setting rank to a high number, and publish them using 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. 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() ; if a shortcut with the same id already exists, it is updated. I use the old details from 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.

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.

  • shortcutManager.maxShortcutCountPerActivity actually returns 15 (,) on my device (LineageOS). even though the launcher & direct share only show the regular 4 icons.

  • 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. 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. This is good for creating Person for notifications, as this allows not keeping the icons in memory. However, there is seemingly no way to pass URI permissions to shortcut manager, so this method can't be used. (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. Calling this workaround on URIs seems to be helping:

       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). It's not adaptive, however. And, icons made with it don't work with ShortcutManager at all.

Also don't forget to check out the readme of the People sample app .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM