Fix: Delphi FMX Android Notifications Permissions

Fix: Delphi FMX Android Notifications Permissions

Privacy changes to the Android OS, affecting versions 13 and above.

Overview

Recent changes to the Android OS, version 13 to be specific, are changing how users can allow permissions for notifications. This article is a follow-up on implementing notifications for iOS. We will take that code and add support for notifications in Android 13. If you have not read the previous article, I suggest you do to get a sense of how to use the TNotificationCenter in Delphi.

Implementation

For updated the code to support notifications on Android 13, we are going to add to the User Interface (UI) a memo field to display failure when the user declines to grant permissions for the application.

User Interface Updates

In the Design view of our Embarcadero IDE, we are going to add a TMemo component, right under the button. Make sure to anchor the Left, Right, and Bottom of the new TMemo component, and DO NOT anchor the Top. We want to have the field visible at different resolutions, and if we do a Windows or MacOS build for when we resize the application. Your application should look as follows on the Design view:

When we run the application on the device, we will have the following on our Android phone:

That is it for all the UI changes and updates.

Code Updates

There are two main places to update: (1) we will update the Manifest, as this is critical for the new changes on Android 13; and (2) we will update the code in the FormCreate method. Once we have completed the changes we will test on our mobile device. I have tested this solution on the following devices: Google Pixel 7, Samsung S23, Samsung S8+, and Samsung A54. For this specific repository, I have tested on a Samsung S8+ and a Samsung A54.

Manifest

We need to update the AndroidManifest.template.xml in a few places. We will change/add the following:

  1. The android:targetSdkVersion should be set to 33

  2. The user-permission should be added under the <%uses-permission%> for the android.permission.POST_NOTIFICATIONS

  3. We need to add the android:exported property to the main activity and set the value to true

After completing all these three changes your template manifest should look like the code below.

NOTE: Make sure to recompile all your code after changing the Android manifest template file, otherwise changes will not take effect!

Permission Request

Next, we are going to update the FormCreate method. We are going to add a check for the AutherizationStatus from the instance of the TNotificationCenter. For the Android-only code, we are also going to add the check from the PermissionService to make sure access is granted. If the access is not granted, we will prompt the user to allow push notifications. The code below shows the updates to the FormCreate method.

The code will run every time the application is launched, if the user does not have allowed the permissions it will prompt for the notification permission to be granted as shown in the screenshot below.

All the code has been updated, and next, we will test the notification!

Notification Testing

Once the permissions have been granted, when you click on the "Create Notification" button the noise will chime, and the notification will appear on the top bar as shown on the screenshot below for our "Test Tile" notification.

Tip

If you have updated from a previous version of Delphi (any previous version will have obsolete files), you need to follow the steps described in this link to update your project Java templates for Android.

https://github.com/DelphiWorlds/HowTo/tree/main/Solutions/AndroidServiceDelphi111

NOTE: This is critical to fix service issues that might make your application crash

Conclusion

There you have it, we have updated our code to support notifications in our Android 13 devices and above. This can be a show-stopper for your application and generate a lot of user complaints about why it is not working. You can find the updated project on the following repo: https://github.com/an01f01/delphi-fmx-ios/tree/v0.2.0

References

https://en.delphipraxis.net/topic/8075-android-13-ask-permission-for-push-notification/

https://stackoverflow.com/questions/26327669/how-do-i-handle-platform-specific-code-in-a-delphi-xe7-app-meant-for-multiple-pl

Delphi Cookbook: Recipes to master Delphi for IoT integrations, cross-platform, mobile and server-side development, 3rd Editionis text

Expert Delphi: Robust and fast cross-platform application development

Did you find this article valuable?

Support Alessandro by becoming a sponsor. Any amount is appreciated!