Jump to content
John van de Waeter

Android 13 ask permission for push notification

Recommended Posts

Hi All,

 

Using Delphi 11.2

 

The Android PermissionsService from System.Permissions seems to lack a permission for push-notifications, which is needed (afaik) for Android 13 and up.

 

Without the permission, a valid device-token for FCM is generated, it's nicely registered at Google, and Google even says it succeeded when sending a push to this device, but the phone's app settings says that reception (or showing?) of push notification is blocked.

If I turn it on in the apps settings, it works, but that is not a friendly method to tell my users....

 

So.. How to ask for and set this permission at runtime?

 

tia!

John

 

Share this post


Link to post

you tryed:

  • Project->Options->Entitlement List->Receive push notifications [x]
  • requirement on app code: 
  • Quote

      PermissionsService.RequestPermissions(                                   { Android permissions... }
        [                                                                      { }
        JStringToString(TJManifest_permission.JavaClass.XXXXXXXX)    { }
        ]                                                                      { }
     

     

  • and Android recommendations:  https://developer.android.com/develop/ui/views/notifications/notification-permission

Share this post


Link to post
5 hours ago, John van de Waeter said:

The Android PermissionsService from System.Permissions seems to lack a permission for push-notifications, which is needed (afaik) for Android 13 and up

Yes, you will need to manually add the permission to the manifest - one way is to modify AndroidManifest.template.xml to add:

    <uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>

A suitable location would be just below <%uses-permission%>
 

You will also need to request the permission in your code, e.g:

  PermissionsService.RequestPermissions(['android.permission.POST_NOTIFICATIONS'],
    procedure(const APermissions: TClassicStringDynArray; const AGrantResults: TClassicPermissionStatusDynArray)
    begin
      if AGrantResults[0] = TPermissionStatus.Granted then
        // Permission was granted
    end
  );

 

  • Like 1

Share this post


Link to post

in fact, you DONT NEED any reference to "RequestPermissionsResult" or "DisplayRationale" procedures! Only in special case where you need process something more specific for you app

Only "permissions strings" it's enough to conclude the OS requirement

implementation

{$R *.fmx}

uses
  System.Permissions,
  Androidapi.Jni.Os,
  Androidapi.Helpers;

const
  MyPermissionsRequired: array [0 .. 0] of string = ('android.permission.POST_NOTIFICATIONS');

procedure TForm1.FormCreate(Sender: TObject);
begin
  // Place where the first call of the app 's requests will be made. Put where the verification will be carried out
  //
  PermissionsService.RequestPermissions( { }
    [MyPermissionsRequired[0]],          { }
    nil,                                 { TRequestPermissionsResultEvent proc not really necessary at all}
    nil                                  { TDisplayRationaleEvent)  proc not really necessary at all }
    );

  // ------------
  //
  // if the Permissions have already been given or not, so that later you can execute the desired procedure.E.g.send a notification!
  // NOTE: this command calls Android permissions dialog if not granted yet!
  //
  // all
  if PermissionsService.IsEveryPermissionGranted([MyPermissionsRequired[0]]) then
    // send my notification procedure
      ;
  // just one
  if PermissionsService.IsPermissionGranted(MyPermissionsRequired[0]) then
    // send my notification procedure
      ;
end;

 

Edited by programmerdelphi2k

Share this post


Link to post

Hi Dave,

This I tried too, but the inner procedure (if AGrantresult[0]... ) is never called.

Also weird that requesting permission to RECEIVE push notifications is calles POST_NOTIFICATIONS...

Share this post


Link to post
1 minute ago, programmerdelphi2k said:

'android.permission.POST_NOTIFICATION'

This value is incorrect - it should be android.permission.POST_NOTIFICATIONS

Share this post


Link to post
1 minute ago, John van de Waeter said:

is never called

Yes, I'm having the same problem - I'm looking into it

Share this post


Link to post

by Android Developer link above

Quote

<manifest ...>
    <uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
    <application ...>
        ...
    </application>
</manifest>

 

Share this post


Link to post
12 minutes ago, Dave Nottage said:

Yes, I'm having the same problem - I'm looking into it

In AndroidManifest.template.xml, change %targetSdkVersion% to 33

Share this post


Link to post
14 minutes ago, Dave Nottage said:

In AndroidManifest.template.xml, change %targetSdkVersion% to 33

I did, the procedure was invoked, 1 GrantResult, but there was no dialog from Android to ask the user for permission. And it was NOT granted.

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

×