maui
maui copied to clipboard
Add permission request for POST_NOTIFICATION to essentials for Android 13 and up
Description
Android 13 now requires a new permission, POST_NOTIFICATION, to allow notifications in the notifications tray. I did an extension to add it but having it built in would be nice and easier as it is not going away.
Here is the info on it
https://developer.android.com/develop/ui/views/notifications/notification-permission
Public API Changes
Essentials would just need to add the permission checks and request.
Intended Use-Case
This would probably apply to any app that uses the notifications tray.
We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.
This is a permission that every app that uses notifications needs with android 13 which was released 4 months ago.
It's worth noting that you can extend the Essentials' API for your own use for any permission as well. Take for example ContactsRead.
If you follow this pattern you could create something like:
public class PostNotifications : BasePlatformPermission
{
public override (string androidPermission, bool isRuntime)[] RequiredPermissions =>
new (string, bool)[] { (Manifest.Permission.PostNotifications, true) };
}
Then use it something like:
var result = await Permissions.RequestAsync<PostNotifications>();
It's worth noting that you can extend the Essentials' API for your own use for any permission as well. Take for example ContactsRead.
If you follow this pattern you could create something like:
public class PostNotifications : BasePlatformPermission { public override (string androidPermission, bool isRuntime)[] RequiredPermissions => new (string, bool)[] { (Manifest.Permission.PostNotifications, true) }; }
Then use it something like:
var result = await Permissions.RequestAsync<PostNotifications>();
@Redth when I try your solution, I get this error:
The current Activity can not be detected. Ensure that you have called Init in your Activity or Application class.
Any ideas? :)
It's worth noting that you can extend the Essentials' API for your own use for any permission as well. Take for example ContactsRead. If you follow this pattern you could create something like:
public class PostNotifications : BasePlatformPermission { public override (string androidPermission, bool isRuntime)[] RequiredPermissions => new (string, bool)[] { (Manifest.Permission.PostNotifications, true) }; }
Then use it something like:
var result = await Permissions.RequestAsync<PostNotifications>();
@Redth when I try your solution, I get this error:
The current Activity can not be detected. Ensure that you have called Init in your Activity or Application class.
Any ideas? :)
Hmm it worked using Xamarin.Essentials.Permissions instead of Microsoft.Maui.ApplicationModel
As a work around, add this to MainActivity:
if (ContextCompat.CheckSelfPermission(this, Manifest.Permission.PostNotifications) != Permission.Granted)
{
ActivityCompat.RequestPermissions(this, new[] { Manifest.Permission.PostNotifications }, 0);
}
I hope it will be added to .net 8
@Redth Will the team be willing to accept a PR for this one?