- Home /
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);
}
}
did you solve this issue? I'm having the same problem here :S
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.
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
Your answer
Follow this Question
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