Android Notification in a Plugin

I have an android plugin. I need to add the ability to show a notification from within the plugin. My app is paused at this point.

I have tried several examples and I always get a class not found exception on the notification instanciation whether it is by doing “new Notification” or Notification.Builder or NotificationCompat.Builder.

I read a lot on the internet and everything seems to point to the notification library not being there OR my resource icon not being accessible.

Can anyone point me to some code of how to do an android notification in a Unity app?

Keep in mind that I need to be able to show the notification when my app is paused.

Thanks!

im looking for the same for the last few months .im surprised that there is no known option available for push notifications.. :(

2 Answers

2

Hi Hotshot,
I was struggling with this issue for a while and managed to get the below code to work successfully (and display a push notification message within the status bar). You’ll just need to call the below code (within your plugin) when you need to generate a notification:

        Resources res = context.getResources();
        int icon = res.getIdentifier("IMAGE_NAME", "drawable", "package_name_here");
    	long when = System.currentTimeMillis();
        NotificationManager notificationManager = (NotificationManager)        context.getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = new Notification(icon, message, when);
        String title = "Some Game";
        Intent notificationIntent = new Intent(context, MainActivity.class);

        // set intent so it does not start a new activity
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
                Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent intent =
                PendingIntent.getActivity(context, 0, notificationIntent, 0);
        notification.setLatestEventInfo(context, title, message, intent);
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notificationManager.notify(0, notification);

The key section of this is the line ***
int icon = res.getIdentifier(“ic_launcher”, “drawable”, package_name_here); ***
Reason being that *** int icon = R.drawable.ic_launcher; *** will not work as unity will establish it’s own R.txt index (index of all Android resources). So using the “getIdentifier” command forces the plugin to look through the Unity compiled R.txt index.

You just need to make sure that you have all your drawable (image) resources inside the unity folder “Assets/Plugins/Android/res/drawable” (so Unity knows where to find the resources).

The code is rough and not perfect but hopefully it gives you a good starting point :).

Thanks for posting this, however i guess it would help to know what packages you have to import in the Java code, etc... Also you should mention that this is actually Java code and is not a UnityScript or C# script. Anyway +1. There should be more about native plugins here, but for most it's maybe too advanced and others keep it secret ;)

One question. How do I figure out what the package name needs to be?

Guys, I use the above code in my game but notification comes up once I've paused a game. Whatever I tried to do, notification don't wait particular time and comes up as soon as I pause a game :( What do I need to do? I stuck.

Thank you,your answer is very helpful. Now i finaaaaally know why my icon can't be found in my plugin! thanks again.

its working thanks man

This will do everything you need!
http://digitalcrackapp.com/images/androidpluginsunity/androidpluginsdemo.zip

No they are wanting it to run in the background. I purchased this and it doesn't not allow it to run in the background. That is $100 extra. I found that out after I purchased it.