How to delay Android local notification permission request?

I’m struggling to find any documentation that talks about how an Android app can request permission to send local notifications. I have never used an Android device, but I assume it’s not just the wild west over there with any app sending any notification it wants.

I’m wanting to delay that request until a very specific time, not just have it pop up when the app starts or when the first notification gets sent. The package documentation only talks about that ability for iOS.

Do you know how to delay the notification permission request on Android?
Thanks

Did you find anything about delayed approval?

Any update on this?

Android doesn’t require special permission for sending notifications and there’s no prompt like on iOS. Only special cases like full-screen notifications or changing notification settings require runtime permission and those can be requested with Android.Permission.

What about now? Android 12 or greater is now show prompt like iOS also for notification

I think this has only been added in Android 13? You should be able to use Android.Permission.RequestUserPermission with the new permission string, though it’s not the new API that allows you show the reason why you’re requesting a permission.

Permission is only required on Android 13.
The API to request permission only works when app targets Android 13 and runs on it. If target SDK is lower, permission will be asked automatically by the OS.

You can request notification permission any time you want it by calling this Permission.

using UnityEngine;
using UnityEngine.Android;

public class RequestPermissions : MonoBehaviour
{
    void Start()
    {
        if (!Permission.HasUserAuthorizedPermission("android.permission.POST_NOTIFICATIONS"))
        {
            Permission.RequestUserPermission("android.permission.POST_NOTIFICATIONS");
        }
    }
}

@Aurimas-Cernius we’re also trying to delay asking for permission on Android. On an Android 13 device, we’re getting asked on app start for this permission. We’re using the sample code, which we modified to not call RequestNotificationPermission() in GameNotificationsManager.Initialize(). We’re not seeing any setting related to this for Android in the project settings.

Any idea what could be causing the device to ask for permission?

If your target SDK is less than 33 (Android 13), then OS will ask for permission automatically on first launch. The code for asking permissions only works (and is required) when targeting 33.

Understood, thanks. I’ve confirmed this works well.

@Aurimas-Cernius Hello, is there any issue known after granting permission with a custom activity name?

I am just calling:

var permissionRequest = new PermissionRequest();

which shows the notification prompt, however, after interacting with it, my app is no longer interactable.

windowFocusChanged: false
android.permission.POST_NOTIFICATIONS granted
onActivityResumed: com.company.unityplayer.AnrUnityPlayerActivity@a7d52b
onResume
windowFocusChanged: true

Notice I have a custom activity name above.
Once I use the default unity activity name, it works

Can you show an entire activity code?

what do u mean with entire activity code?
Before those logs, there is only games logs.

I removed
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" /> from the manifest and I have got a different output but same issue:

ActivityTaskManager     system_server                        W  Activity pause timeout for ActivityRecord{1c671cb u0 com.company.gametemplate/com.company.unityplayer.AnrUnityPlayerActivity} t48}
Timeout while trying to pause the Unity Engine.
ad1af0a com.company.gametemplate/com.company.unityplayer.AnrUnityPlayerActivity (server) spent 3492ms processing FocusEvent(hasFocus=false)
android.permission.POST_NOTIFICATIONS granted
onActivityResumed: com.company.unityplayer.AnrUnityPlayerActivity@4387346
onResume
windowFocusChanged: true
Channel [Gesture Monitor] swipe-up (server) is stealing touch from [ad1af0a com.company.gametemplate/com.company.unityplayer.AnrUnityPlayerActivity (server), [Gesture Monitor] edge-swipe (server)]
windowFocusChanged: false

Your activity Java code

We are using this plugin:
https://github.com/SalomonBrys/ANR-WatchDog

and below our activity Java code:

package com.company.unityplayer;

import com.github.anrwatchdog.ANRError;
import com.github.anrwatchdog.ANRWatchDog;

import com.unity3d.player.UnityPlayerActivity;

import android.os.Bundle;
import android.util.Log;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;


public class AnrUnityPlayerActivity extends UnityPlayerActivity
{
    private ArrayList<StringCallback> anrCallbacks;

    protected void onCreate(Bundle savedInstanceState)
    {
        anrCallbacks = new ArrayList<>();
        ANRWatchDog watchDog = new ANRWatchDog(5000);
        watchDog.setIgnoreDebugger(true);
        watchDog.setReportMainThreadOnly().setANRListener(new ANRWatchDog.ANRListener() {
                                                        @Override
                                                        public void onAppNotResponding(ANRError error) {
                                                            Log.e("AnrUnityPlayerActivity", "", error);
                                                            Log.d("AnrUnityPlayerActivity", "ANR Caught. Notifying " + anrCallbacks.size() + " listeners");

                                                            for (int i = 0; i < anrCallbacks.size(); i++)
                                                            {
                                                                anrCallbacks.get(i).onInvoke(getStackTrace(error));
                                                            }
                                                        }
                                                    })
                                                    .start();

        super.onCreate(savedInstanceState);

        // Prints debug message to Logcat
        Log.d("AnrUnityPlayerActivity", "onCreate called!");
    }

    String getStackTrace(Error error)
    {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        error.printStackTrace(pw);
        String stackTrace = sw.toString();
        return stackTrace;
    }

    public void addAnrListener(StringCallback callback)
    {
        Log.d("AnrUnityPlayerActivity", "addAnrListener called!");
        anrCallbacks.add(callback);
    }

    public void removeAnrListener(StringCallback callback)
    {
        Log.d("AnrUnityPlayerActivity", "removeAnrListener called!");
        anrCallbacks.remove(callback);
    }
}

Now it looks worthy a bug report, as I can’t spot anything wrong with it.