Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Unable to request notification permission in android 13

Discussion in 'Android' started by Danief_, Oct 9, 2022.

  1. Danief_

    Danief_

    Joined:
    Feb 2, 2019
    Posts:
    20
    Hi team, as of Android 13, players are required to opt-in to notifications. Source. I'm looking for a simple way to request the
    POST_NOTIFICATIONS
    permission.

    I see that the Unity Mobile Notifications package authors are at least aware of it.

    I've tried using the permission class, as follows:
    Permission.RequestUserPermission("android.permission.POST_NOTIFICATIONS");

    but this appears to trigger an infinite loop.

    So far, I've only been able to request this permission by sending the player a notification from my personal server.

    Is there a recommended way of doing this?
     
  2. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,722
    This infine loop happens when your target SDK is less than 33 (being fixed). You can only ask for this permission when you target API 33. When your app targets lower SDK, OS asks for permission automatically, your app can't do that, best it can do is to open the settings page for itself.

    If you wonder, new version of notifications package will be out very soon.
     
    xinatcg and KateKomar like this.
  3. Danief_

    Danief_

    Joined:
    Feb 2, 2019
    Posts:
    20
    Awesome, thank you. I look forward to the next release.
     
  4. IgorGalimskiFMGames

    IgorGalimskiFMGames

    Joined:
    Jul 21, 2021
    Posts:
    16
  5. Danief_

    Danief_

    Joined:
    Feb 2, 2019
    Posts:
    20
  6. TillmaniaLtd

    TillmaniaLtd

    Joined:
    Jan 29, 2013
    Posts:
    65
    Newest package doesn't seem to be compatible with Unity 2019. Any word on a fix for the MobileNotifications package that's compatible with Unity 2019?
     
  7. TillmaniaLtd

    TillmaniaLtd

    Joined:
    Jan 29, 2013
    Posts:
    65
    When installing 2.1.0 into Unity 2019 (via updating manifest.json) I get the following compile error:

    Code (CSharp):
    1. Library/PackageCache/com.unity.mobile.notifications@2.1.0/Runtime/Android/NotificationPermission.cs(72,33): error CS0246: The type or namespace name 'PermissionCallbacks' could not be found (are you missing a using directive or an assembly reference?)/code]
     
  8. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,722
    Unity 2019 has reached it's end of life, and the 2.1.0 version of package was released after that happened.
    The 1.4.4 version should be compatible with 2019.
     
  9. TillmaniaLtd

    TillmaniaLtd

    Joined:
    Jan 29, 2013
    Posts:
    65
    Ah, so it looks like I need to update my legacy project to 2022 for continued Unity support...
     
  10. IgorGalimskiFMGames

    IgorGalimskiFMGames

    Joined:
    Jul 21, 2021
    Posts:
    16
  11. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,722
    IgorGalimskiFMGames likes this.
  12. ToanLeTaggle

    ToanLeTaggle

    Joined:
    Dec 14, 2022
    Posts:
    10
    Hi @Aurimas-Cernius, I am using Unity Mobile Notification v2.1.0. I get issue once request permission for push notification, after request permission I get these log continuously and permission popup isn't appear:
    12-20 21:11:26.338 28039 28039 D Unity : onActivityResumed: com.unity3d.player.UnityPlayerActivity@1fd01db
    12-20 21:11:26.338 28039 28039 I Unity : onResume
    12-20 21:11:26.341 28039 28039 I Unity : onPause
    ...
    I called this code:
    Unity.Notifications.Android.PermissionRequest permissionRequest = new PermissionRequest();
    It's correct?
     
  13. ToanLeTaggle

    ToanLeTaggle

    Joined:
    Dec 14, 2022
    Posts:
    10
    I tested this on android 13
     
  14. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,722
    Not exactly and based on description your app is not configured correctly.
    First, before requesting permission you should call this: https://docs.unity3d.com/Packages/c...ndroidNotificationCenter_UserPermissionToPost

    That should immediately solve the problem you see.
    The real problem is that you app must target API 33 to be able to request for permission. If you target lower than 33, the permission request will pop automatically (handled by OS).
    The endless pause-resume loop is a Unity bug, should be fixed in latest Unity versions.
     
    ToanLeTaggle likes this.
  15. Mayis

    Mayis

    Joined:
    Feb 1, 2014
    Posts:
    6
    Hello, was this issue fixed in Unity versions newer than 2020.3.41, so we can build using Target API level less than API 33 and be able to request for permission on Android 13 devices without any issue?
     
  16. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,722
    There is no issue to fix. That's how Android works.
     
  17. Mayis

    Mayis

    Joined:
    Feb 1, 2014
    Posts:
    6
    "This infine loop happens when your target SDK is less than 33 (being fixed)."

    In this sentence you said being fixed, I thought anyway it is possible to build with target SDK level less than 33 and request permission on Android 13 if the infine loop bug fixed in newer Unity versions.

    SO... Is this conclusion correct?

    If we build with target API level less than 33 no matter which Unity version we use:
    1. RequestPermission will not work because of loop issue.
    2. Without RequestPermission there is no way to make notifications work.

    The only way build the project using API level 33?
     
    langinteger likes this.
  18. langinteger

    langinteger

    Joined:
    May 23, 2022
    Posts:
    4
    ============

    In my test, I have to set my app's targetSdkVersion to 33 in order to ask for notification permission in Android 13 devices correctly. I'm using Unity 20210313 btw.
     
  19. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,722
    You are confusing two things here. The infinite loop was a Unity bug that has been fixed. But you still cannot ask for notification permission, if you target less than 33.
    If your app targets <33, Android OS considers it an older app that does not know the permission even exists and ask for permission on apps behalf (and does not allow the app itself to ask for it).
     
  20. Mayis

    Mayis

    Joined:
    Feb 1, 2014
    Posts:
    6
    Thank you for reply. As I understood from your explanation, if we target less than 33 we can't ask for permission, but the Android 13 OS will detect app is using older target and will ask permission automatically, correct?

    If this is correct, then currently for our build Android 13 OS not asking automatically for notifications permission after removing RequestPermission from the code. Do we need to add something to AndroidManifest or must other build setup be done to make this work? We are targeting API Level 31.
     
  21. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,722
    Correct. I believe the actual request for permission happens when you touch Android notification code for the first time.

    Remove the application from device to reset the state. OS remembers whether notifications were granted or denied and the only way to switch is in Settings. Otherwise the request for notifications should happen the first time you try to use them.
     
  22. UnityDWD

    UnityDWD

    Joined:
    Jun 16, 2016
    Posts:
    1
    I'm getting an infinite loop on Unity 2020.3.31f1 when calling
    Permission.RequestUserPermission("android.permission.POST_NOTIFICATIONS");

    And we are targeting Android API 33 (android 13) and I'm testing on an android running 13 as well. Do we have to update unity to make it not infinite loop?
     
  23. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,722
    How does that infinite loop manifests?
     
  24. joaok

    joaok

    Joined:
    May 2, 2022
    Posts:
    9
    @Aurimas-Cernius I have the same problem as described by @UnityDWD - I am getting an ANR once the permission prompt shows up.

    - Unity2022.3.4f1
    - Android API 33
    - "com.unity.mobile.notifications": "2.2.0"

    I have tried:
    Code (CSharp):
    1. if(!Permission.HasUserAuthorizedPermission("android.permission.POST_NOTIFICATIONS"))
    2. {
    3.     Permission.RequestUserPermission("android.permission.POST_NOTIFICATIONS");
    4. }
    and
    Code (CSharp):
    1. var permissionRequest = new PermissionRequest();
    Any idea what would be the problem?

    Btw, I have a custom activity in the AndroidManifest.xml and have noticed UnityEngine.Android.Permission has "com.unity3d.player.UnityPlayer" hard-coded and it is not taking in the consideration the the one I set in Project Settings -> Mobile Notification -> Use Custom Activity, however, I assume PermissionRequest would do that as it is part of the package.
     
    Last edited: Aug 31, 2023
  25. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,722
    Maybe the permission has been permanently denied for your app already? In such case your code keeps asking for it and OS keeps rejecting it without any dialog being shown?

    Custom activity should not be related to this in any way.
     
  26. joaok

    joaok

    Joined:
    May 2, 2022
    Posts:
    9
    @Aurimas-Cernius
    I always see the dialog, and once it shows up the ANR starts. If I confirm, after quitting and starting the game again, then I dont have a problem. So, the ANR / infinite loop is happening bc of the dialog.
     
  27. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,722
    Have you placed the code in an Update loop with no guards?
     
  28. joaok

    joaok

    Joined:
    May 2, 2022
    Posts:
    9
    No, I call it once upon game starting. And I have a proguard file in place.
     
  29. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,722
    Could you show the full script?
     
  30. joaok

    joaok

    Joined:
    May 2, 2022
    Posts:
    9
    I gave up on this issue but now I am back to it and still didnt find the issue:
    I have asked u in another post thou: https://forum.unity.com/threads/how...ation-permission-request.725273/#post-9432557

    regarding my full script, it's just that:

    Code (CSharp):
    1. public class AndroidNotificationPermission
    2. {
    3.     public void RequestPermission()
    4.     {
    5.         var permissionRequest = new PermissionRequest();
    6.     }
    7. }
     
  31. IneptStudio

    IneptStudio

    Joined:
    Mar 7, 2022
    Posts:
    12
    Hi just a simple question how to ask for notification permission in android when game launch for the first time.
    My target sdk version is 33
    and unity version is 2022.3.11f1
    I have included notification permission in android menifest but that does not work in android 13.
     
  32. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,722
  33. sathyarajktsamy

    sathyarajktsamy

    Joined:
    Feb 10, 2024
    Posts:
    1
    Requesting push notification permissions will not work in the traditional way for android API Level 33+. It can be achieved with android layer on top of it. Make the below changes in MainActivity.java file, to prompt user for notification permission.

    MainActivity.java File - android\app\src\main\java\project\app\MainActivity.java

    package com.app;
    import android.Manifest;
    import android.os.Bundle;
    import android.os.Build;
    import android.os.Handler;
    import androidx.core.content.ContextCompat;
    import android.content.pm.PackageManager;
    import androidx.core.app.ActivityCompat;
    import androidx.activity.result.ActivityResultLauncher;
    import androidx.activity.result.contract.ActivityResultContracts;
    import com.getcapacitor.BridgeActivity;

    public class MainActivity extends BridgeActivity {

    public static final int DOWNLOAD_REQUEST_CODE = 1024;

    @override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    CheckNotifications();
    }

    public void CheckNotifications() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {

    if (ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) ==
    PackageManager.PERMISSION_GRANTED) {

    } else if (shouldShowRequestPermissionRationale(Manifest.permission.POST_NOTIFICATIONS)) {

    } else {
    requestPermission.launch(Manifest.permission.POST_NOTIFICATIONS);
    }
    }
    }

    private ActivityResultLauncher<String> requestPermission =
    registerForActivityResult(new ActivityResultContracts.RequestPermission(), isGranted -> {
    if (isGranted) {

    } else {

    }
    });

    }
     
    ilywkaa likes this.