[英]How do I set android notification icon at runtime?
因此,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和Mobile Notification Settings界面获取图标或图标ID的名称。
所以我想的是,也许有一种方法可以在运行时访问Assets / Plugins / Android / res / drawable(或等效的),所以我可以通过代码,在运行时将我的图标png添加到该文件夹,然后我可以正常发送通知,使用png文件名,但不必事先将其添加到Assets / Plugins / Android / res / drawable或Mobile Notification设置。
ORR如果有办法通过代码编辑移动通知设置中的列表。 这是一个Texture2D列表,这是完美的,但我找不到一种方法来编辑该列表,而不是仅编辑方法。
注意,如果在应用程序运行之前不知道通知图标,这将特别有用。 就像您希望用户从他们的手机中选择一个自定义png,然后您的应用将其用作通知。 一个完美的例子是即时消息,其中图标通常是发送者的个人资料图片,可以是字面上的任何内容,并且在应用程序运行之前显然是未知的。 我知道这些是在线通知,而不是本地通知,但我如何使用本地通知做同样的事情?
看看UnityNotificationEditorManager.cs
,这个unity包将你在设置中输入的纹理导出到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;
}
所以基本上任何可绘制文件夹中的图像都可以用作图标。
但代码在构建时运行,所以我认为你无法在运行时更改drawables。 因此,如果您想这样做,我建议在资产商店中找到另一个包(如果存在)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.