capacitor-plugins icon indicating copy to clipboard operation
capacitor-plugins copied to clipboard

Push Notifications: Android 13 requires POST_NOTIFICATIONS permission

Open hermitdemschoenenleben opened this issue 2 years ago • 3 comments

Feature Request

Plugin

push-notifications

Description

As Android 13 was released yesterday, I noticed that it requires that the user grants the runtime permission POST_NOTIFICATIONS before a user may receive push messages. Am I right that the capacitor push-notifications plugin doesn't implement this yet? As far as I know, requestPermissions() only does something on ios, but not on android.

More Info: https://firebase.google.com/docs/cloud-messaging/android/client#request-permission13

Platform(s)

Android 13

Preferred Solution

requestPermissions() should ask for POST_NOTIFICATIONS on Android 13.

hermitdemschoenenleben avatar Aug 17 '22 13:08 hermitdemschoenenleben

I found out that a corresponding issue already exists for local notifications: https://github.com/ionic-team/capacitor-plugins/issues/1116 Problem is the same, just has to be built in for local notifications as well as for push notifications

hermitdemschoenenleben avatar Aug 19 '22 07:08 hermitdemschoenenleben

The link below explains best practices on how new apps (Android 13+) should use this feature (for your reference). The corresponding Capacitor docs for the plugin might need to be updated as well..

https://developer.android.com/develop/ui/views/notifications/notification-permission#new-apps

kemalcany avatar Aug 29 '22 10:08 kemalcany

as a workaround, I created a simple capacitor plugin that can be used for obtaining POST_NOTIFICATIONS permission on android: https://github.com/hermitdemschoenenleben/capacitor-plugin-android-post-notifications-permission

hermitdemschoenenleben avatar Sep 14 '22 13:09 hermitdemschoenenleben

just tried your plugin @hermitdemschoenenleben

error: cannot find symbol @Permission(strings = { Manifest.permission.POST_NOTIFICATIONS }, alias = AndroidPostNotificationsPermissionPlugin.POST_NOTIFICATIONS) ^ symbol: variable POST_NOTIFICATIONS location: class permission

What does this mean I wonder ?

jadsy2107 avatar Oct 23 '22 09:10 jadsy2107

just tried your plugin @hermitdemschoenenleben

error: cannot find symbol @Permission(strings = { Manifest.permission.POST_NOTIFICATIONS }, alias = AndroidPostNotificationsPermissionPlugin.POST_NOTIFICATIONS) ^ symbol: variable POST_NOTIFICATIONS location: class permission

What does this mean I wonder ?

fixed by

compileSdkVersion = 33

in variables.gradle

jadsy2107 avatar Oct 23 '22 09:10 jadsy2107

just tried your plugin @hermitdemschoenenleben error: cannot find symbol @Permission(strings = { Manifest.permission.POST_NOTIFICATIONS }, alias = AndroidPostNotificationsPermissionPlugin.POST_NOTIFICATIONS) ^ symbol: variable POST_NOTIFICATIONS location: class permission What does this mean I wonder ?

fixed by

compileSdkVersion = 33

in variables.gradle

Still doesn't work,

Im running latest Ionic @caoacitor/push-notifications plugin and it doesnt ask for notifications when opening app, however it only asks weirdly on second app open immediately

(preferably should ask after toggle my enable notifications button, like on IOS - not at beginning of app open)

jadsy2107 avatar Oct 23 '22 09:10 jadsy2107

Without this plugin, we are also getting the prompt on second app launch. According to ionic team, they should get it on first app launch. I wonder how this is done?

joshuavillano avatar Nov 04 '22 01:11 joshuavillano

I think on older SDKs you get it on the first app launch after you've initialized push messages – which is typically the second app launch.

hermitdemschoenenleben avatar Nov 04 '22 08:11 hermitdemschoenenleben

I'm not getting the request for permission until after the first push notification is received while the app is backgrounded. This results in the first push being lost. I have targetSdkVersion = 32.

indiebloom avatar Jan 18 '23 19:01 indiebloom

Having same issue here as @indiebloom, after second launch while app is in background notification permissions prompt shows up, even though I have implemented the actual permission check as a result of the user pushing a button to enable notifications, like this:

let permStatus = await PushNotifications.checkPermissions();

if (permStatus.receive === 'prompt') {
    permStatus = await PushNotifications.requestPermissions();
}

I would like for it to ask for permissions when calling PushNotifications.requestPermissions();, not after the first notification has already arrived (and gotten lost). I am targeting SDK 32.

khromov avatar Apr 11 '23 23:04 khromov

I got help in the Capacitor discord to install the Capacitor 5 beta and that made notifications work! Here is some more info: https://discord.com/channels/520266681499779082/908091931295375380/1095795167635701800

khromov avatar Apr 12 '23 20:04 khromov

this was fixed in @capacitor/push-notifications 5 alpha 1, can be installed using capacitor 5 and next npm tag

check the capacitor 5 upgrade guide (it's still in beta) https://capacitorjs.com/docs/next/updating/5-0

jcesarmobile avatar Apr 14 '23 11:04 jcesarmobile

tried to update to the beta version (both capacitor and the capacitor plugin) and it actually works again and asks for permission.

I'd like to know if they will fix it in capacitor 4, because i dont want to rush updating my apps

ciccilleju avatar Apr 14 '23 12:04 ciccilleju

no, it won't, this is a breaking change, so can't be backported.

jcesarmobile avatar Apr 14 '23 12:04 jcesarmobile

no, it won't, this is a breaking change, so can't be backported.

I see. Well, I noticed that using target 32 instead of 33 seems to be fine at the moment, am I wrong?

ciccilleju avatar Apr 14 '23 12:04 ciccilleju

No, you are not wrong, if you target SDK 32 then you don't need to request the push notification permission yourself, but you can't control when it will be requested, it's usually requested when you or a plugin creates a notification channel, but in some devices it can happen on next app launch after a channel was created.

Main reason of Capacitor 5 to exist is to properly target SDK 33. It will support same iOS and Android versions Capacitor 4 supports, so should be an easy update.

jcesarmobile avatar Apr 14 '23 12:04 jcesarmobile

No, you are not wrong, if you target SDK 32 then you don't need to request the push notification permission yourself, but you can't control when it will be requested, it's usually requested when you or a plugin creates a notification channel, but in some devices it can happen on next app launch after a channel was created.

Main reason of Capacitor 5 to exist is to properly target SDK 33. It will support same iOS and Android versions Capacitor 4 supports, so should be an easy update.

Thank you so much for your excellent explanation. You are a boss 😀

ciccilleju avatar Apr 14 '23 12:04 ciccilleju

Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of the plugin, please create a new issue and ensure the template is fully filled out.

ionitron-bot[bot] avatar May 14 '23 12:05 ionitron-bot[bot]