Search Unity

How to delay Android local notification permission request?

Discussion in 'Editor & General Support' started by James-Sullivan, Aug 9, 2019.

  1. James-Sullivan

    James-Sullivan

    Joined:
    Jun 15, 2015
    Posts:
    128
    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
     
  2. a_vallberg

    a_vallberg

    Joined:
    Jan 7, 2018
    Posts:
    5
    Did you find anything about delayed approval?
     
  3. Estellise-Yukihime

    Estellise-Yukihime

    Joined:
    Aug 9, 2018
    Posts:
    9
    Any update on this?
     
  4. Adrian

    Adrian

    Joined:
    Apr 5, 2008
    Posts:
    1,066
    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.
     
  5. its_sagar_bro

    its_sagar_bro

    Joined:
    Jul 8, 2020
    Posts:
    2
    What about now? Android 12 or greater is now show prompt like iOS also for notification
     
  6. Adrian

    Adrian

    Joined:
    Apr 5, 2008
    Posts:
    1,066
  7. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,736
    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.
     
  8. its_sagar_bro

    its_sagar_bro

    Joined:
    Jul 8, 2020
    Posts:
    2
    You can request notification permission any time you want it by calling this Permission.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Android;
    3.  
    4. public class RequestPermissions : MonoBehaviour
    5. {
    6.     void Start()
    7.     {
    8.         if (!Permission.HasUserAuthorizedPermission("android.permission.POST_NOTIFICATIONS"))
    9.         {
    10.             Permission.RequestUserPermission("android.permission.POST_NOTIFICATIONS");
    11.         }
    12.     }
    13. }
    14.  
     
  9. Swah

    Swah

    Joined:
    May 13, 2015
    Posts:
    80
    @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?
     
  10. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,736
    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.
     
  11. Swah

    Swah

    Joined:
    May 13, 2015
    Posts:
    80
    Understood, thanks. I've confirmed this works well.
     
  12. joaok

    joaok

    Joined:
    May 2, 2022
    Posts:
    9
    @Aurimas-Cernius Hello, is there any issue known after granting permission with a custom activity name?

    I am just calling:
    Code (CSharp):
    1. var permissionRequest = new PermissionRequest();
    which shows the notification prompt, however, after interacting with it, my app is no longer interactable.

    Code (JavaScript):
    1. windowFocusChanged: false
    2. android.permission.POST_NOTIFICATIONS granted
    3. onActivityResumed: com.company.unityplayer.AnrUnityPlayerActivity@a7d52b
    4. onResume
    5. windowFocusChanged: true
    Notice I have a custom activity name above.
    Once I use the default unity activity name, it works
     
    Last edited: Oct 26, 2023
  13. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,736
    Can you show an entire activity code?
     
  14. joaok

    joaok

    Joined:
    May 2, 2022
    Posts:
    9
    what do u mean with entire activity code?
    Before those logs, there is only games logs.

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

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

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,736
    Your activity Java code
     
  16. joaok

    joaok

    Joined:
    May 2, 2022
    Posts:
    9
    We are using this plugin:
    https://github.com/SalomonBrys/ANR-WatchDog

    and below our activity Java code:

    Code (JavaScript):
    1. package com.company.unityplayer;
    2.  
    3. import com.github.anrwatchdog.ANRError;
    4. import com.github.anrwatchdog.ANRWatchDog;
    5.  
    6. import com.unity3d.player.UnityPlayerActivity;
    7.  
    8. import android.os.Bundle;
    9. import android.util.Log;
    10.  
    11. import java.io.PrintWriter;
    12. import java.io.StringWriter;
    13. import java.util.ArrayList;
    14.  
    15.  
    16. public class AnrUnityPlayerActivity extends UnityPlayerActivity
    17. {
    18.     private ArrayList<StringCallback> anrCallbacks;
    19.  
    20.     protected void onCreate(Bundle savedInstanceState)
    21.     {
    22.         anrCallbacks = new ArrayList<>();
    23.         ANRWatchDog watchDog = new ANRWatchDog(5000);
    24.         watchDog.setIgnoreDebugger(true);
    25.         watchDog.setReportMainThreadOnly().setANRListener(new ANRWatchDog.ANRListener() {
    26.                                                         @Override
    27.                                                         public void onAppNotResponding(ANRError error) {
    28.                                                             Log.e("AnrUnityPlayerActivity", "", error);
    29.                                                             Log.d("AnrUnityPlayerActivity", "ANR Caught. Notifying " + anrCallbacks.size() + " listeners");
    30.  
    31.                                                             for (int i = 0; i < anrCallbacks.size(); i++)
    32.                                                             {
    33.                                                                 anrCallbacks.get(i).onInvoke(getStackTrace(error));
    34.                                                             }
    35.                                                         }
    36.                                                     })
    37.                                                     .start();
    38.  
    39.         super.onCreate(savedInstanceState);
    40.  
    41.         // Prints debug message to Logcat
    42.         Log.d("AnrUnityPlayerActivity", "onCreate called!");
    43.     }
    44.  
    45.     String getStackTrace(Error error)
    46.     {
    47.         StringWriter sw = new StringWriter();
    48.         PrintWriter pw = new PrintWriter(sw);
    49.         error.printStackTrace(pw);
    50.         String stackTrace = sw.toString();
    51.         return stackTrace;
    52.     }
    53.  
    54.     public void addAnrListener(StringCallback callback)
    55.     {
    56.         Log.d("AnrUnityPlayerActivity", "addAnrListener called!");
    57.         anrCallbacks.add(callback);
    58.     }
    59.  
    60.     public void removeAnrListener(StringCallback callback)
    61.     {
    62.         Log.d("AnrUnityPlayerActivity", "removeAnrListener called!");
    63.         anrCallbacks.remove(callback);
    64.     }
    65. }
    66.  
     
  17. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,736
    Now it looks worthy a bug report, as I can't spot anything wrong with it.