wix / react-native-notifications

React Native Notifications
MIT License
3.25k stars 764 forks source link

Docs Update: Ask Android 33 Permission #937

Closed upngo closed 1 year ago

upngo commented 1 year ago

For Android 33, permission must be asked for manually. Would be nice if this was mentioned in the docs.

If a user installs your app on a device that runs Android 13 or higher, your app's notifications are off by default. Your app must wait to send notifications until after you request the new permission and the user grants that permission to your app. The time at which the permissions dialog appears is based on your app's target SDK version: If your app targets Android 13 or higher, your app has complete control over when the permission dialog is displayed. Use this opportunity to explain to users why the app needs this permission, encouraging them to grant it.For android 33 https://developer.android.com/develop/ui/views/notifications/notification-permission

A potential code sample could be

import { PermissionsAndroid } from 'react-native'; PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.POST_NOTIFICATIONS);

seco35 commented 1 year ago

Hey I was starting with this library and were stuck at exactly where my android phone never asked me for the Notification Permission.

VSCode automatically suggested me the PERMISSIONS after typing PermissionsAndroid. However nothing was suggested after PERMISSIONS as to the error "permission is null" made sense. Could you think of a possibility why I'm getting the "permission is null" error? Would you mind to tell me your React-Native version. I'm using 0.67.4 right now

upngo commented 1 year ago

I'm using 0.71 which mentions POST_NOTIFICATIONS permission. Looking at the 0.67 docs it doesn't. Probably because this was a permission that could be handled in your AndroidManifest.xml. Not sure why you're getting an error about permission being null though. Presumably you have a PermissionsAndroid you could log. Alternatively there are plugins to manage permissions you could try.

stale[bot] commented 1 year ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

stale[bot] commented 1 year ago

The issue has been closed for inactivity.

tamlyn commented 1 year ago

This has cost us days of wasted debugging time. It's very unexpected that permissions are handled transparently on iOS but not on Android. Vote to re-open this issue.

The code snippet above works on recent versions of React Native, but older ones (e.g. 0.68) don't have that constant defined. In that case you can use the permission value directly like this:

PermissionsAndroid.request("android.permission.POST_NOTIFICATIONS" as any);
jakecurreri commented 2 months ago

This remains an unresolved issue. Please re-open.

Thanks @tamlyn and @upngo for the solve!