Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Check Push Notification status

Discussion in 'Android' started by novaVision, Apr 17, 2020.

  1. novaVision

    novaVision

    Joined:
    Nov 9, 2014
    Posts:
    494
    Hi,

    As we know, push notification request is already integrated to agreement before app install, but also there could be a case when user manually disable notifications using native Android settings.
    For my app I need to know this status.
    I didn't find any ready to use solution, so searched for android forums and found that it can be implemented using
    Code (CSharp):
    1. NotificationManagerCompat.from(context).areNotificationsEnabled()
    How can I run this code within the Unity?
     
    phobos2077 likes this.
  2. Draikz

    Draikz

    Joined:
    Dec 20, 2016
    Posts:
    12
    I'm at the same point as u are. I need to check both in Android and iOS wether the user can receive notifications or not.

    For iOS, we seem to have this:

    Code (CSharp):
    1. UnityEngine.iOS.NotificationServices.enabledNotificationTypes
    But we don't have anything similar in Android for what I've seen googling apart from that native android method you show, so I'm giving this a bump to see if someone can help further on this matter.

    Thanks!
     
    phobos2077 likes this.
  3. novaVision

    novaVision

    Joined:
    Nov 9, 2014
    Posts:
    494
    As I found, the only option is to use custom .jar plugin with a code

    Code (CSharp):
    1. import androidx.core.app.NotificationManagerCompat;
    2.  
    3. public class NotificationStatusChecker {
    4.  
    5.     private static final String LOGTAG = "UnityU";
    6.  
    7.     private Context context;
    8.  
    9.     public NotificationStatusChecker(Context context) {
    10.  
    11.         this.context = context;
    12.     }
    13.  
    14.     public boolean areNotificationsEnabled() {
    15.         boolean result = NotificationManagerCompat.from(this.context).areNotificationsEnabled();
    16.         Log.i(LOGTAG, "Notifications enabled :: " + result);
    17.         return result;
    18.     }
    19.  
    20.     public static boolean areNotificationsEnabledFromContext(Context customContext) {
    21.         boolean result = NotificationManagerCompat.from(customContext).areNotificationsEnabled();
    22.         Log.i(LOGTAG, "From custom context notifications enabled :: " + result);
    23.         return result;
    24.     }
    25. }
     
  4. Draikz

    Draikz

    Joined:
    Dec 20, 2016
    Posts:
    12
    I'm totally new to "custom .jar plugins", could you please give me some hints or basic guidance about how to create/use/integrate them in the project, if it's not asking too much? Because looking at the documentation on jars for Unity, it presumes you already have the necessary .jar and it just talks about import, so I'm kind of lost.

    Thanks!
     
  5. novaVision

    novaVision

    Joined:
    Nov 9, 2014
    Posts:
    494
    I would recommend you to check these 2 videos, they explain the basics + automatization process using Android Studio. Very usefull to understand how the things works. Before this I just tried to use ready to use solutions without any understanding of logic and details.
    Video1
    Video2

    They explain very simple case. For notifications check NotificationManagerCompat require an app context. There are 2 ways to implement it in Unity - creating a NotificationStatusChecker class and keep the reference in Unity, or just create new one using static method each time you need to ask for notification status. During the test I implemented both of them:
    Code (JavaScript):
    1. using UnityEngine;
    2.  
    3. public static class AndroidNotificationStatus
    4. {
    5. #if UNITY_ANDROID
    6.     private const string NOTIFICATION_STATUS_CLASS = "com.XXX.utils.NotificationStatusChecker";
    7.     private const string UNITY_PLAYER_CLASS = "com.unity3d.player.UnityPlayer";
    8.  
    9.     private static AndroidJavaObject _utilsClass;
    10.  
    11.     private static AndroidJavaObject UtilsClass
    12.     {
    13.         get
    14.         {
    15.             if (_utilsClass == null)
    16.             {
    17.                 AndroidJavaClass unityPlayer = new AndroidJavaClass(UNITY_PLAYER_CLASS);
    18.                 AndroidJavaObject activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
    19.                 _utilsClass = new AndroidJavaObject(NOTIFICATION_STATUS_CLASS, activity);
    20.             }
    21.  
    22.             return _utilsClass;
    23.         }
    24.     }
    25.  
    26.     private static bool AreNotificationsEnabledInternal()
    27.     {
    28.         var result = UtilsClass.Call<bool>("areNotificationsEnabled");
    29.         return result;
    30.     }
    31.  
    32.     private static bool AreNotificationsEnabledSelfDisposable()
    33.     {
    34.         // self disposable method
    35.         using (var javaUnityPlayer = new AndroidJavaClass(UNITY_PLAYER_CLASS))
    36.         {
    37.             using (var currentActivity = javaUnityPlayer.GetStatic<AndroidJavaObject>("currentActivity"))
    38.             {
    39.                 using (var utils = new AndroidJavaObject(NOTIFICATION_STATUS_CLASS, currentActivity))
    40.                 {
    41.                     return utils.Call<bool>("areNotificationsEnabled");
    42.                 }
    43.             }
    44.         }
    45.     }
    46. #endif
    47. }
     
  6. Draikz

    Draikz

    Joined:
    Dec 20, 2016
    Posts:
    12
    Thanks a ton man, will have a look on those videos!
     
  7. maxammadjon95

    maxammadjon95

    Joined:
    Jun 21, 2019
    Posts:
    7
    Hi, Thank you for answer, I almost did it with your answer and the videos I watched. So I created AAR file and added in Plugins in Unity. Could you tell me what should I write into XXX there? private const string NOTIFICATION_STATUS_CLASS = "com.XXX.utils.NotificationStatusChecker";
    What class' name is this?
     
  8. maxammadjon95

    maxammadjon95

    Joined:
    Jun 21, 2019
    Posts:
    7
    Oh, I found it. It's the name of the project when you create in Android Studio, for example: com.Somethin.Something, I should add NotificationStatusChecker at the end of this name like that com.something.something.NotificationStatusChecker and this is worked. Thank you guys, it really helped me, and now I can check Notification status: enabled or disabled
     
    bon22 likes this.
  9. OleksandrW

    OleksandrW

    Joined:
    Jul 13, 2021
    Posts:
    3
    Does someone have a ready-made solution with a library and unity code? Thanks
     
    chris_unity559 likes this.
  10. S0paTa

    S0paTa

    Joined:
    Feb 13, 2020
    Posts:
    4
    Maybe you can try this plugin: https://github.com/hiyorin/PermissionPlugin-for-Unity
    I have not tested it myself, but it seems it might do the job.

    If you are using OneSignal for push notifications, I can confirm that the following code works:

    Code (CSharp):
    1. if (OneSignal.GetPermissionSubscriptionState().permissionStatus.status == OSNotificationPermission.Authorized) {
    2.     // Do stuff
    3.  
    4. }
     
  11. kiss-dev

    kiss-dev

    Joined:
    Apr 4, 2020
    Posts:
    4
    You do not need to create jar/aar to get one native bool parameter, you can do everything directly from C# code:

    Code (CSharp):
    1. public static class PushNotificationAvailability
    2. {
    3.     public static bool ArePushNotificationsAvailable()
    4.     {
    5.         using var contextClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    6.         using var context = contextClass.GetStatic<AndroidJavaObject>("currentActivity");
    7.         using var notificationManagerClass = new AndroidJavaClass("androidx.core.app.NotificationManagerCompat");
    8.         using var managerInstance = notificationManagerClass.CallStatic<AndroidJavaObject>("from", context.Call<AndroidJavaObject>("getApplicationContext"));
    9.      
    10.         return managerInstance.Call<bool>("areNotificationsEnabled");
    11.     }
    12. }
     
    Last edited: Jul 12, 2023
  12. maryam88

    maryam88

    Joined:
    Oct 4, 2023
    Posts:
    1
    @kiss-dev I am getting this error

    2023/10/12 12:42:04.186 9143 9250 Error Unity AndroidJavaException: java.lang.ClassNotFoundException: androidx.core.app.NotificationManagerCompat
    2023/10/12 12:42:04.186 9143 9250 Error Unity java.lang.ClassNotFoundException: androidx.core.app.NotificationManagerCompat
    2023/10/12 12:42:04.186 9143 9250 Error Unity at java.lang.Class.classForName(Native Method)
    2023/10/12 12:42:04.186 9143 9250 Error Unity at java.lang.Class.forName(Class.java:536)
    2023/10/12 12:42:04.186 9143 9250 Error Unity at com.unity3d.player.UnityPlayer.nativeRender(Native Method)
    2023/10/12 12:42:04.186 9143 9250 Error Unity at com.unity3d.player.UnityPlayer.access$300(Unknown Source:0)
    2023/10/12 12:42:04.186 9143 9250 Error Unity at com.unity3d.player.UnityPlayer$e$1.handleMessage(Unknown Source:83)
    2023/10/12 12:42:04.186 9143 9250 Error Unity at android.os.Handler.dispatchMessage(Handler.java:102)
    2023/10/12 12:42:04.186 9143 9250 Error Unity at android.os.Looper.loopOnce(Looper.java:226)
    2023/10/12 12:42:04.186 9143 9250 Error Unity at android.os.Looper.loop(Looper.java:313)
    2023/10/12 12:42:04.186 9143 9250 Error Unity at com.unity3d.player.UnityPlayer$e.run(Unknown Source:20)
    2023/10/12 12:42:04.186 9143 9250 Error Unity Caused by: java.lang.ClassNotFoundException: androidx.core.app.NotificationManagerCompat
    2023/10/12 12:42:04.186 9143 9250 Error Unity ... 9 more
    2023/10/12 12:42:04.186 9143 9250 Error Unity at UnityEngine.AndroidJNISafe.CheckException () [0x00000] in <00000000000000000000000000000000>:0
    2023/10/12 12:42:04.186 9143 9250 Error Unity at UnityEngine.AndroidJNISafe.FindClass (System.String name) [0x00000] in <00000000000000000000000000000000>:0
    2023/10/12 12:42:04.186 9143 9250 Error Unity at UnityEngine.AndroidJavaC