简体   繁体   中英

How do I set android notification icon at runtime?

So Unity's method to set Android local notification icons is through their editor interface in Mobile Notification Settings. It's basically, add your icons to the list in the editor, specify an ID for each one, and then when sending the message set the LargeIcon field to the ID. Alternatively, instead of adding your icons to the list in the editor, you can have them in Assets/Plugins/Android/res/drawable.

Instead of doing any of this, I want to simply specify a Texture2D or png file path entirely through code. Something like:

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

or

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

I know this won't work because the method to send the notification is pre-built to only take the name of the icon or icon ID from Assets/Plugins/Android/res/drawable and the Mobile Notification Settings interface respectively.

So what I'm thinking is, maybe there's a way to access Assets/Plugins/Android/res/drawable (or equivalent) in run time, so I can add my icon pngs to that folder through code, in run time, and then I can send the notification as normal, with the png file name but not having to add it to Assets/Plugins/Android/res/drawable or Mobile Notification settings beforehand.

ORR if there's a way to edit the list in Mobile Notification Settings via code. It's a list of Texture2Ds, which is perfect, but I couldn't find a way to edit that list programmatically other than Editor only methods.

Side note, this would be especially useful if the notification icons are not known until the app is running. Like if you want the user to select a custom png from their phone which your app then uses as the notification. A perfect example is instant messaging, where the icons are often the profile pictures of the sender, which can be literally anything and is obviously not known until the app is running. I know those are online notifications and not local, but how do I do that same thing with local notifications?

Having a look at UnityNotificationEditorManager.cs , this unity package exports the textures you input in settings to drawables folder with the name of its 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;
    }

So basically any image in drawable folders can be used as your icon.

But the code runs at build time so what I think is you can not change drawables at runtime. so if you want to do that, I recommend finding another package in asset store if it exists.

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