Unable to request notification permission in android 13

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_NOTIFICATIONSpermission.

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?

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.

2 Likes

Awesome, thank you. I look forward to the next release.

@Aurimas-Cernius
Any estimation for the fix?

Looks like the latest version now handles it: https://docs.unity3d.com/Packages/com.unity.mobile.notifications@2.1/api/Unity.Notifications.Android.PermissionRequest.html

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?

When installing 2.1.0 into Unity 2019 (via updating manifest.json) I get the following compile error:

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]

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.

Ah, so it looks like I need to update my legacy project to 2022 for continued Unity support…

2.1.0 has other bug https://github.com/Unity-Technologies/com.unity.mobile.notifications/issues/233

Unfortunately, that not a package bug, it’s Unity. I’ll look into it.

1 Like

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?

I tested this on android 13

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/com.unity.mobile.notifications@2.1/api/Unity.Notifications.Android.AndroidNotificationCenter.html#Unity_Notifications_Android_AndroidNotificationCenter_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.

1 Like

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?

There is no issue to fix. That’s how Android works.

“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?

1 Like

============

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.

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).

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.