【问题标题】:How do I set android notification icon at runtime?如何在运行时设置 android 通知图标?
【发布时间】:2019-10-18 11:50:57
【问题描述】:

所以 Unity 设置 Android 本地通知图标的方法是通过其在移动通知设置中的编辑器界面。基本上,将您的图标添加到编辑器的列表中,为每个图标指定一个 ID,然后在发送消息时将 LargeIcon 字段设置为 ID。或者,您可以将它们放在 Assets/Plugins/Android/res/drawable 中,而不是将图标添加到编辑器的列表中。

我不想做任何这些,而是​​想完全通过代码简单地指定一个 Texture2D 或 png 文件路径。比如:

LargeIcon = Application.persistentDataPath + "myNotificationIcon.png";

LargeIcon = Texture2D.getFile(); //or whatever the method would be.

我知道这行不通,因为发送通知的方法是预先构建的,仅分别从 Assets/Plugins/Android/res/drawable 和移动通知设置界面中获取图标或图标 ID 的名称。

所以我在想的是,也许有一种方法可以在运行时访问 Assets/Plugins/Android/res/drawable(或等效项),所以我可以在运行时通过代码将我的图标 png 添加到该文件夹,然后我可以正常发送通知,使用 png 文件名,但不必事先将其添加到 Assets/Plugins/Android/res/drawable 或 Mobile Notification 设置中。

ORR,如果有办法通过代码在移动通知设置中编辑列表。这是一个 Texture2D 列表,非常完美,但除了仅限编辑器的方法之外,我找不到以编程方式编辑该列表的方法。

旁注,如果在应用程序运行之前不知道通知图标,这将特别有用。就像您希望用户从他们的手机中选择一个自定义 png,然后您的应用程序将其用作通知。一个完美的例子是即时消息,其中的图标通常是发件人的个人资料图片,它可以是任何东西,并且显然在应用程序运行之前不知道。我知道这些是在线通知,而不是本地通知,但我如何使用本地通知做同样的事情?

【问题讨论】:

    标签: c# android unity3d notifications icons


    【解决方案1】:

    看看UnityNotificationEditorManager.cs,这个统一包将你在设置中输入的纹理导出到drawables文件夹,并带有它的id名称。

    internal Dictionary<string, byte[]> GenerateDrawableResourcesForExport()
        {
            var icons = new Dictionary<string, byte[]>();
            foreach (var res in TrackedResourceAssets)
            {
                if (!res.Verify())
                {
                    Debug.LogWarning( string.Format("Failed exporting: '{0}' Android notification icon because:\n {1} ", res.Id,
                        DrawableResourceData.GenerateErrorString(res.Errors))
                    );
                    continue;
                }
    
                var texture = TextureAssetUtils.ProcessTextureForType(res.Asset, res.Type);
    
                var scale = res.Type == NotificationIconType.SmallIcon ? 0.375f : 1;
    
                var textXhdpi = TextureAssetUtils.ScaleTexture(texture, (int) (128 * scale), (int) (128 * scale));
                var textHdpi  = TextureAssetUtils.ScaleTexture(texture, (int) (96 * scale), (int) (96 * scale));
                var textMdpi  = TextureAssetUtils.ScaleTexture(texture, (int) (64 * scale), (int) (64 * scale));
                var textLdpi  = TextureAssetUtils.ScaleTexture(texture, (int) (48 * scale), (int) (48 * scale));
    
                icons[string.Format("drawable-xhdpi-v11/{0}.png", res.Id)] = textXhdpi.EncodeToPNG();
                icons[string.Format("drawable-hdpi-v11/{0}.png", res.Id)] = textHdpi.EncodeToPNG();
                icons[string.Format("drawable-mdpi-v11/{0}.png", res.Id)] = textMdpi.EncodeToPNG();
                icons[string.Format("drawable-ldpi-v11/{0}.png", res.Id)] = textLdpi.EncodeToPNG();
    
                if (res.Type == NotificationIconType.LargeIcon)
                {
                    var textXxhdpi = TextureAssetUtils.ScaleTexture(texture, (int) (192 * scale), (int) (192 * scale));
                    icons[string.Format("drawable-xxhdpi-v11/{0}.png", res.Id)] = textXhdpi.EncodeToPNG();
                }
            }
    
            return icons;
        }
    

    所以基本上可绘制文件夹中的任何图像都可以用作您的图标。

    但是代码在构建时运行,所以我认为你不能在运行时更改可绘制对象。所以如果你想这样做,我建议在资产商店中找到另一个包(如果存在)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-12-04
      • 1970-01-01
      • 2015-06-18
      • 2019-01-26
      • 2020-08-04
      • 2014-05-10
      • 1970-01-01
      相关资源
      最近更新 更多