Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 Baptboy01 · Oct 12, 2012 at 12:52 AM · androidpluginnotificationjni

How to call an android notification plugin if it's not the main activity?

Hello,

I tried to call an android plugin without using it as the main activity.

If in my manifest I declare it as the main activity is working using this :

 AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
             AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
             jo.Call("createNotification", new object[] { "Test", "Test" ", "Test", time });

But how can I call it if it's not my main activity?

Thanks

This is my classes in my jar file :

NotificationActivity.java :

 package com.test.me;
 
 import com.unity3d.player.UnityPlayerActivity;
 
 import android.app.AlarmManager;
 import android.app.PendingIntent;
 import android.content.Context;
 import android.content.Intent;
 import android.os.Bundle;
 import android.util.Log;
 
 public class NotificationActivity extends UnityPlayerActivity {
     /** Called when the activity is first created. */
      AlarmManager am;
     
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
     }
     
     public void test() {
         Log.d("test", "test");
         
     }
 
     public void createNotification(String tickerText, String contentTitle, String contentText, int time) {
         
         am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
         
         Intent intent = new Intent(this, TimeAlarm.class);
             //intent.putExtra("tickerText",tickerText);
             //intent.putExtra("contentTitle",contentTitle);
             intent.putExtra("contentText",contentText);
             
             PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
           intent, PendingIntent.FLAG_ONE_SHOT);
             
             am.set(AlarmManager.RTC_WAKEUP,
           System.currentTimeMillis() + (time * 1000), pendingIntent);
             
     }
     
     public void cancelNotification() {
         am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
         Intent intent = new Intent(this, TimeAlarm.class);
         PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
         intent, PendingIntent.FLAG_ONE_SHOT);
                     
         am.cancel(pendingIntent);
         
     }
     
   } 

This is TimeAlarm.java

 package com.test.me;
 
 import java.util.UUID;
 
 import android.app.Notification;
 import android.app.NotificationManager;
 import android.app.PendingIntent;
 import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;
 
 public class TimeAlarm extends BroadcastReceiver {
 
      NotificationManager nm;
 
      @Override
      public void onReceive(Context context, Intent intent) {
          String tickerText = "test";
          String contentTitle = "test";
          String contentText = intent.getStringExtra("contentText");
          
       nm = (NotificationManager) context
         .getSystemService(Context.NOTIFICATION_SERVICE);
       
       PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
         new Intent(), 0);
       
       Notification notif = new Notification(R.drawable.ic_launcher,
               tickerText, System.currentTimeMillis());
       
       notif.flags |= Notification.FLAG_AUTO_CANCEL;
       notif.defaults = Notification.DEFAULT_ALL;
       
       notif.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
       Integer uid = (int) (long)  UUID.randomUUID().getMostSignificantBits();
       nm.notify(uid, notif);
      }
     }
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 Riacosta · Apr 03, 2013 at 04:01 PM 0
Share

did you solve this issue? I'm having the same problem here :S

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Statement · Apr 03, 2013 at 04:16 PM

Well, remember that you're just calling code in the plugin. So use whatever methods you would normally use to get access to a specific object. Some simple solutions would be maintaining a static variable and accessing that or a static function via JNI/the helper classes that you used.

Just like you called com.unity3d.player.UnityPlayer.currentActivity.createNotification() you can call some other static method like com.mycompany.myproduct.MyClass.MyMethod() like so:

 var myClass = new AndroidJavaClass("com.mycompany.myproduct.MyClass");
 myClass.CallStatic("MyMethod");

If you think about it, its not that much different from SendMessage() and GetComponent() (but they do different things, of course). Once you have any object that allows you to navigate to a certain object, then you can just keep on accessing members or calling methods until you reach what you are looking for.

Comment
Add comment · 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
0

Answer by pimpin · Aug 03, 2013 at 10:36 AM

Call it with UnityPlayer.activity; Also you would find the implementation in these tools helpful http://digitalcrackapp.com/images/androidpluginsunity/androidpluginsdemo.zip

Comment
Add comment · 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

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.

Follow this Question

Answers Answers and Comments

13 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

Related Questions

I need a local push function. so I made it using alrammanager. but it doesn't 0 Answers

Using AndroidJavaObject.CallStatic to retrieve a return value 0 Answers

An working example of calling Native C code from a C# script in Android 0 Answers

How to pass an interface to Java from Unity code? 2 Answers

SEGV_ACCERR issue in java plugin 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges