2

My app targets API 33, and runs a foreground service to play online music.

Once the notification is started via startForeground(notification_Id, notification) I can update the notification via notificationManager.notify(notification_Id, notification), however Android Studio tells me that the android.permission.POST_NOTIFICATIONS permission is missing.

Even though I include that permission in the AndroidManifest, I don't need to ask the user to grant the permission to update the notification.

Can anyone confirm if this method of updating the notification is valid or is it a security hole? Obviously the method works but I have not found any reference in the documentation of Android 13+

Thank you.

Amc
  • 88
  • 7

1 Answers1

0

It seems that you have encountered a problem related to the MediaStyle notification update in Android 13+ with the POST_NOTIFICATIONS permission. Android has introduced changes related to security and permissions in recent versions, which may affect the way notifications are handled.

In Android 11 (API 30) and later, the POST_NOTIFICATIONS authorization has been introduced to strengthen security around notifications. To use this authorization, you'll need to ask the user for permission.

Here's how you can manage this situation:

  1. Make sure you have included the POST_NOTIFICATIONS authorization in your AndroidManifest.xml file:
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
  1. Request this permission from the user using the requestPermission() function in your code. For example:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
    NotificationManager notificationManager = getSystemService(NotificationManager.class);
    if (notificationManager != null) {
        NotificationManager.Policy policy = notificationManager.getNotificationPolicy();
        if (!policy.isCategoryAllowed(Notification.CATEGORY_CALLS)) {
            Intent intent = new Intent(Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS);
            startActivity(intent);
        }
    }
}
  1. When you want to update your notification, you can always use NotificationManager.notify(). Make sure that notification_Id matches the ID of the notification you wish to update.

This should allow you to update the MediaStyle notification of your foreground service without error. However, bear in mind that notification permissions are important for security reasons, so it's important to ask the user for permission when necessary.

yannick
  • 52
  • 2