Search Unity

Android Notification Plugin

Discussion in 'Android' started by Mourkain, Jul 26, 2017.

  1. Mourkain

    Mourkain

    Joined:
    Dec 14, 2012
    Posts:
    9
    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):
    1. 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):
    1. package com.example.unityplugin;
    2.  
    3. import android.app.Activity;
    4. import android.os.Bundle;
    5.  
    6. /**
    7. * Created by mourkain on 26.07.17.
    8. */
    9.  
    10. public class MainActivity extends Activity{
    11.     PluginClass nHandler;
    12.  
    13.     @Override
    14.     protected void onCreate(Bundle savedInstanceState) {
    15.         super.onCreate(savedInstanceState);
    16.         nHandler = PluginClass.instance(this);
    17.     }
    18.  
    19.     public String RemoveAllTheNotifications(){
    20.         nHandler.RemoveAllNotifications();
    21.         return "RemovingNotifications";
    22.     }
    23. }
    24.  
    Code (CSharp):
    1. package com.example.unityplugin;
    2.  
    3. import android.app.Activity;
    4. import android.app.NotificationManager;
    5. import android.content.Context;
    6.  
    7. /**
    8. * Created by mourkain on 25.07.17.
    9. */
    10.  
    11. public class PluginClass{
    12.  
    13.     private static PluginClass instance;
    14.     private static NotificationManager notificationManager;
    15.  
    16.     public PluginClass(){
    17.         this.instance = this;
    18.     }
    19.  
    20.     public static PluginClass instance(Context context){
    21.         if(instance == null){
    22.             instance = new PluginClass();
    23.             notificationManager = (NotificationManager) context.getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
    24.         }
    25.         return instance;
    26.     }
    27.  
    28.     public void RemoveAllNotifications(){
    29.         //NotificationManager notificationManager = (NotificationManager) Context.getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
    30.         notificationManager.cancelAll();
    31.     }
    32.  
    33. }
    34.  
    and this is how I call it from Unity.

    Code (CSharp):
    1. var plugin = new AndroidJavaClass("com.example.unityplugin.MainActivity");
    2. Debug.Log("Android Plugin Mesage: " + plugin.Call<string>("RemoveAllTheNotifications"));
    Any help is appreciated ;)
     
  2. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    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):
    1. using (var actClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
    2. {
    3.     var activity = actClass.GetStatic<AndroidJavaObject>("currentActivity");
    4.     Debug.Log("Android Plugin Mesage: " + activity.Call<string>("RemoveAllTheNotifications"));
    5. }
    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.
     
  3. Mourkain

    Mourkain

    Joined:
    Dec 14, 2012
    Posts:
    9
    Thanks for the answer. I'm not sure if I did it correct.
    Code (CSharp):
    1. using (var actClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
    2.         {
    3.             var activity = actClass.GetStatic<AndroidJavaObject>("currentActivity");
    4.             var plugin = new AndroidJavaClass("com.example.unityplugin.MainActivity");
    5.  
    6.             Debug.Log("Android Plugin Mesage: " + plugin.Call<string>("RemoveAllTheNotifications", activity));
    7.         }
    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 ;)
     
  4. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    What is 'com.example.unityplugin.MainActivity' exactly? i think you're mixing up a few things here
     
  5. Mourkain

    Mourkain

    Joined:
    Dec 14, 2012
    Posts:
    9
    It's the my class name from Plugin Class. It's in my first Post.
     
  6. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    You're calling RemoveAllThenotifications and passing an activity into it, but according to the code you showed above it doesn't accept any arguments.
     
  7. Mourkain

    Mourkain

    Joined:
    Dec 14, 2012
    Posts:
    9
    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 :/
     
  8. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    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.