• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by Hotshot10101 · Feb 13, 2013 at 12:19 AM · androidnotification

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!

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image elixir-bash · Feb 21, 2013 at 11:15 AM 0
Share

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

2 Replies

· Add your reply
  • Sort: 
avatar image
13
Best Answer

Answer by Chowdery · Apr 17, 2013 at 12:55 PM

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 :).

Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Apr 17, 2013 at 01:22 PM 1
Share

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 ;)

avatar image Hotshot10101 · Apr 17, 2013 at 01:34 PM 0
Share

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

avatar image Chowdery · Apr 18, 2013 at 09:55 AM 0
Share

Sorry for not elaborating more guys :S. The package name that you need to use is whatever you have called the package that contains the Java plugin (E.g. "com.java.plugin").

Regarding how to include the plugin within your Unity project, I'm sure there's plenty more in-depth tutorials, but the plain version is: - Convert the Eclipse project to a Library - Copy the plugin.jar (in the bin folder) to the following folder in your Unity project "Assets/Plugins/Android". - $$anonymous$$ake your standard Unity calls to the plugin to call the required functions - Before deploying to your Android device, ensure that the "player settings" specify the bundle identifier as your plugin package (same package referenced in the getIdentifier function).

avatar image segment · Dec 05, 2013 at 03:11 PM 0
Share

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.

avatar image thebmxeur · Dec 15, 2014 at 07:22 PM 1
Share

The answer helped me with loading an icon in a library. The one thing that you have to make sure is that the "package_name_here" in

 res.getIdentifier("I$$anonymous$$AGE_NA$$anonymous$$E", "drawable", "package_name_here")


is the unity player package name, not the java package name of your plugin.

If anybody comes here in the future :

  • if you want to use NotificationCompat.builder in a plugin, you have to put "android-support-v4.jar" file in the "Assets/Plugins/android" directory or subdirectory.

  • The android notification system doesn't allow to delay notifications. The "when" parameters is only there to sort notification. If you want to delay notifications you have to use something like the Alarm$$anonymous$$anager and a BroadcastReceiver.

  • If you develop your plugin in Eclipse, the res folder will contain a crunch folder, itself containing the drawable folders. You have to move the drawable folders to the level of the crunch folder and then remove the crunch folder (res/crunch/drawable => res/drawable ).

Here is some basic java plugin code to do local notifications (WIP, it's an eclipse project). I would recommend NOT USING THIS in production code as it's a quick thing I hacked together : AndroidNotification

It's derived from that free package in the asset store : Simple local notifications for Android

Show more comments
avatar image
-1

Answer by pimpin · Aug 02, 2013 at 08:52 PM

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image tredpro · Mar 01, 2014 at 05:53 PM 1
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

Does the iOS($400) work with Unity3d (Free) ? 2 Answers

Mobile Ads Frameworks that support unity 0 Answers

Connect to Android in Unity 1 Answer

Get Android Phone Number 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges