Jump to content
Minox

Ask permission to read device notifications on android 13

Recommended Posts

Hi everyone,

i need to ask the permissions to read the notifications on the device, but i can't find the correct way to do it, in the project options i selected the "Bind notification listner service" permission.
The code I use is this:
 

uses

...

  Androidapi.JNI.GraphicsContentViewText, Androidapi.JNI.Provider, Androidapi.Helpers,
  Androidapi.JNI.JavaTypes, System.StrUtils

...

procedure TFormTestPerm.Button1Click(Sender: TObject); //to show the permissions dialog
      VAR LIntent : JIntent;
    begin
          LIntent := TJIntent.Create;
          LIntent.setAction(TJSettings.JavaClass.ACTION_NOTIFICATION_LISTENER_SETTINGS);
          TAndroidHelper.Context.startActivity(LIntent);
    end;

procedure TFormTestPerm.Button2Click(Sender: TObject); //to take the test
      VAR SecureS  : JString;
          NomeAppS : String;
    begin
          NomeAppS := JStringToString(TAndroidHelper.Context.getPackageName);

          SecureS := TJSettings_Secure.JavaClass.getString(SharedActivityContext.getContentResolver,
                                                           StringToJString('enabled_notification_listeners'));

          If AnsiContainsText(JStringToString(SecureS),NomeAppS)
             Then Label1.Text := 'SERVICE FOUND!'
             Else Label1.Text := 'NO SERVICE FOUND'+sLineBreak+'"'+NomeAppS+'"';
    end;

but my application does not appear in the list. I've found a few solutions on the net that I still can't get to work.
Does anyone know the proper way to ask for this type of permission?

Share this post


Link to post

Please provide a link to the documentation that led you this point, or at least examples in Java or Kotlin that achieve the same result

Share this post


Link to post

From the latest versions of Android, to read the notifications the user must manually approve this choice, therefore when starting the app you have to open the notification authorization list and let him select it.
To achieve this, as far as I understand, you need to set the "android.permission.BIND_NOTIFICATION_LISTENER_SERVICE" permission in the Manifest file (I do this in the project options), as described here: NotificationListenerService, and then at runtime open the permissions page to ask the user to activate them, using this entry: Settings Here a procedure in java: notificationlistenerservice I know my approach is wrong, but i don't understand where. For example, when I asked permission for brightness in other applications, I used the same approach, and it worked.

 

Share this post


Link to post

I have attached the code created so far, which once started it shows that my app does not appear in the list of those asking for permission. I found another site where they discuss this topic NotificationListenerService it looks like i have to declare a service before asking permissions, so now i will try to declare one, but i find it hard because java is too hard for me.

 

TestPerm.zip

Share this post


Link to post
On 4/15/2023 at 9:44 PM, Minox said:

I found another site where they discuss this topic NotificationListenerService it looks like i have to declare a service before asking permissions, so now i will try to declare one, but i find it hard because java is too hard for me

I took a keen interest in this, so have put together a demo, here: https://github.com/DelphiWorlds/Kastri/tree/master/Demos/NotificationListenerService

Please ensure that you read the readme, as it has information about how to configure a project of your own, should you use the code that the demo uses.

  • Like 1

Share this post


Link to post

Unfortunately I can't get the app to work, when opening it it either crashes or closes immediately. However it can help me to understand what the method is

Share this post


Link to post
5 hours ago, Minox said:

Unfortunately I can't get the app to work, when opening it it either crashes or closes immediately

Are you talking about the demo, or your own app? If your own app, did you follow the instructions in the readme? Either way, please indicate what version of Android you have on your device.

  • Thanks 1

Share this post


Link to post

ah yes sorry, I was laughing at the demo you indicated to me, it doesn't open, I can compile it but when it starts it closes immediately, the only way to start it is to remove the code in "onCreate".
The device on which I test is a s10e with android 12, and RAD studio 11.3.

Share this post


Link to post

I managed to get the demo working, I didn't realize I have to add the ".jar" file in "Projects" too, thanks again

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×