maui icon indicating copy to clipboard operation
maui copied to clipboard

Add permission request for POST_NOTIFICATION to essentials for Android 13 and up

Open norton287 opened this issue 2 years ago • 6 comments

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.

norton287 avatar Nov 18 '22 13:11 norton287

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.

ghost avatar Nov 18 '22 15:11 ghost

This is a permission that every app that uses notifications needs with android 13 which was released 4 months ago.

trampster avatar Nov 30 '22 21:11 trampster

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 avatar Nov 30 '22 21:11 Redth

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? :)

vallgrenerik avatar Dec 22 '22 15:12 vallgrenerik

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

vallgrenerik avatar Dec 22 '22 15:12 vallgrenerik

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);
        }

TonyLugg avatar Dec 23 '22 22:12 TonyLugg

I hope it will be added to .net 8

DeepWorksStudios avatar Jun 19 '23 19:06 DeepWorksStudios

@Redth Will the team be willing to accept a PR for this one?

AmSmart avatar Aug 06 '23 00:08 AmSmart