使用字符串 id 进行通知或将字符串 id 转换为唯一的 int id

Using string id for notification or converting string id to generate unique int id

在我的应用程序中,我有多个组,每个组都可以有更新,我会在通知中显示这些更新。但是在数据库中,我的组 ID 为 string。因此,为了为每个组生成通知,我需要一个唯一的 int id。那么如何将 string id 转换为 int id。这样每个组只能有 1 个通知,并且可以附加内容,因为该特定组中有更新。

我在显示通知时看到我也可以提供tag

NotificationManager.notify(String tag, int id, Notification notification)

但是为此我还需要提供 int id。那么这个组合是如何工作的呢?或者我只能依靠通知的 string id?

您可以使用字符串的哈希值。或者您可以将标签用于字符串。

您要以不同方式显示的 (tag, id) 通知组合应该有所不同。否则最新的将取代前一个。所以,在这里你可以只使用你拥有的 string id 作为 tag 如果它是唯一的并且使用任何 int id 因为组合必须是唯一的。

参考:(https://developer.android.com/reference/android/app/NotificationManager.html#notify(java.lang.String,%20int,%20android.app.Notification)