Hey, so I'm using Android Native Plugin for LocalNotifications. https://www.assetstore.unity3d.com/en/#!/content/10825 But this Plugin doesn't have a Method to remove those from the NotificationsList. I already talked to the developer. So I decided that I can do it by myself. I just need to call Code (JavaScript): notificationManager.cancelAll(); But the thing is I have zero knowledge about Android and it doesn't work. Here is my Plugin that doesn't work: Code (CSharp): package com.example.unityplugin; import android.app.Activity; import android.os.Bundle; /** * Created by mourkain on 26.07.17. */ public class MainActivity extends Activity{ PluginClass nHandler; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); nHandler = PluginClass.instance(this); } public String RemoveAllTheNotifications(){ nHandler.RemoveAllNotifications(); return "RemovingNotifications"; } } Code (CSharp): package com.example.unityplugin; import android.app.Activity; import android.app.NotificationManager; import android.content.Context; /** * Created by mourkain on 25.07.17. */ public class PluginClass{ private static PluginClass instance; private static NotificationManager notificationManager; public PluginClass(){ this.instance = this; } public static PluginClass instance(Context context){ if(instance == null){ instance = new PluginClass(); notificationManager = (NotificationManager) context.getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE); } return instance; } public void RemoveAllNotifications(){ //NotificationManager notificationManager = (NotificationManager) Context.getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.cancelAll(); } } and this is how I call it from Unity. Code (CSharp): var plugin = new AndroidJavaClass("com.example.unityplugin.MainActivity"); Debug.Log("Android Plugin Mesage: " + plugin.Call<string>("RemoveAllTheNotifications")); Any help is appreciated
All looks OK, except for the last part. You do not instantiate an Activity class yourself, the Android runtime does that for you. In Unity, you can grab the game's main activity by calling UnityPlayer.currentActivity, so your calling code should be modified to something like this: Code (CSharp): using (var actClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) { var activity = actClass.GetStatic<AndroidJavaObject>("currentActivity"); Debug.Log("Android Plugin Mesage: " + activity.Call<string>("RemoveAllTheNotifications")); } You would still have to set MainActivity to be your game's main (launcher) activity. For the purpose of calling this method though, this is probably not necessary - you can simply grab the main activity like i showed you above, and feed that into your plugin's class, like you're already doing.
Thanks for the answer. I'm not sure if I did it correct. Code (CSharp): using (var actClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) { var activity = actClass.GetStatic<AndroidJavaObject>("currentActivity"); var plugin = new AndroidJavaClass("com.example.unityplugin.MainActivity"); Debug.Log("Android Plugin Mesage: " + plugin.Call<string>("RemoveAllTheNotifications", activity)); } In the console is just: I/Unity (18649): Android Plugin Mesage: and the Notifications is still there... I can't be so hard to clear the NotificationsList
You're calling RemoveAllThenotifications and passing an activity into it, but according to the code you showed above it doesn't accept any arguments.
I thought that i need to pass the MainActivity to my Plugin, like you wrote above. So I have my Class: "com.example.unityplugin.MainActivity" with the function I wanna call. And the MainActivity from Unity: "com.unity3d.player.UnityPlayer" where I don't know why I need it and what to do with it :/
is your class the main activity of the game? if it is, then it will be automatically exposed by Unity as UnityPlayer.currentActivity. Also, since this is a class that is derived from Activity, Android will instantiate it for you, you don't have to create an instance of it yourself.