zo0r / react-native-push-notification

React Native Local and Remote Notifications
MIT License
6.75k stars 2.05k forks source link

[Android 13] Missing notifications permission #2329

Open storm2513 opened 1 year ago

storm2513 commented 1 year ago

Bug

Since Android API 33 developers are obliged to manually request permissions for push notifications

This library is not doing that for Android, so push notifications do not work on Android 13.

Would be nice to have that permissions request added when we use requestPermissions: true

komailabbas12 commented 1 year ago

@storm2513 I already add this but still not working in android 13

PushNotification.configure({ onRegister: function (token) { if (Platform.OS === 'android') { AsyncStorage.setItem('fcmToken', token.token); } }, permissions: { alert: true, badge: true, sound: true, }, popInitialNotification: true, requestPermissions: true, });

And i check the permission in setting of mobile of this app that was already granted

tdammy92 commented 1 year ago

@storm2513 Please can you explain more on this please ??. having the same issues, not getting notifications on android. but works on iOS.

komailabbas12 commented 1 year ago

@tdammy92 add these two line in AndroidManifest.xml

`

`

tdammy92 commented 1 year ago

@tdammy92 add these two line in AndroidManifest.xml

<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/> <uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />

@komailabbas12 I did, even requsted Notification permission. did not work..

komailabbas12 commented 1 year ago

@tdammy92

Did you create a channel like this PushNotification.createChannel( { channelId: 'uzzapp', channelName: 'Uzzapp', channelDescription: 'Notification for special message', importance: 4, vibrate: true, }, (created) => console.log(createChannel returned '${created}') );

and then call the notification like this

PushNotification.localNotification({ channelId: 'uzzapp', channelName: 'Uzzapp', vibrate: true, allowWhileIdle: true, message:Your booking schedule on ${moment(paymentDetails?.selectedDate).format('MMMM Do YYYY')} at ${returnCheckinTime(paymentDetails)}, });

levepic commented 1 year ago

For me, first run of the app doesnt bring up permission request, it freezes (ANR) on real device but runs on emulator. For the second run it correctly shows the permission request and everything works fine. Google Play console is full of ANRs originating from this on android 13. Solved it by upgrading to android sdk level 33 to request permission with https://github.com/zoontek/react-native-permissions (although automatic launch for the app on emulator doesnt work with sdk 33 but it works on real device)

cristian1206 commented 1 year ago

Good afternoon everyone, today the same thing happened to me with my application and the solution for me was the following:

  1. Update the version of react-native-push-notification to the latest version (8.1.1).
  2. Add in your manifest the following two permissions:

<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>

<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />

  1. By default notification permissions in android 13 are disabled so your notification will not be displayed. To solve this problem you can execute the following two solutions:

3.1. The first and quickest solution to see if your app works is to enable notification permissions manually. To do this go to settings>notifications>app settings>all apps and activate notifications in your app.

3.2. The second solution is to use a package like react-native-permissions(3.6.1) and evaluate the notifications permission (request(PERMISSIONS.ANDROID.POST_NOTIFICATIONS)) so that it is displayed on the screen and the user can give the permissions . You could do something like the following

import { Platform } from 'react-native';
import { request, PERMISSIONS, RESULTS } from 'react-native-permissions';

class NotificationsPermissions {
    static async requestPermissionsNotifications() {
        if (Platform.OS === "android" && Platform.Version >= 33) {
            try {
                const result = await request(PERMISSIONS.ANDROID.POST_NOTIFICATIONS);
                // Handling the result of the permit request
                if (result === RESULTS.GRANTED) {
                    console.log('Permissions granted');
                } else {
                    console.log('Permissions not granted');
                }
            } catch (error) {
                // Error handling during permission request
                console.error(error);
            }
        }
    }
}

// use example 
NotificationsPermissions.requestPermissionsNotifications();

Note: Is important to take into account that every time a new version of Android comes out, focus your application on that latest version of your OS, in this case Android 14(34) or android 13(33).

Captura de pantalla 2024-03-16 a la(s) 12 17 28 p  m

I hope the answer can be of help to those who are having this problem. Greetings

gerardcastell commented 1 year ago

Hi @cristian1206 , I think you forgot the two permissions you added at step 2. Can you give us some insights on this? Thanks!

17Amir17 commented 1 year ago

Good afternoon everyone, today the same thing happened to me with my application and the solution for me was the following:

  1. Update the version of react-native-push-notification to the latest version (8.1.1).

  2. Add in your manifest the following two permissions:

  3. By default notification permissions in android 13 are disabled so your notification will not be displayed. To solve this problem you can execute the following two solutions: 3.1. The first and quickest solution to see if your app works is to enable notification permissions manually. To do this go to settings>notifications>app settings>all apps and activate notifications in your app. 3.2. The second solution is to use a package like react-native-permissions(3.6.1) and evaluate the notifications permission (request(PERMISSIONS.ANDROID.POST_NOTIFICATIONS)) so that it is displayed on the screen and the user can give the permissions .

I hope the answer can be of help to those who are having this problem. Greetings

Worked! I also had to bump my targetSDKVersion to 33

MehmoodArib commented 1 year ago

Any update on this issue?

phhuong256 commented 1 year ago

permission was granted successfully but i don't get the notification. Help me

oddie13 commented 1 year ago

any update ? I still got error even after adding <uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" android:maxSdkVersion="32" /> <uses-permission android:name="android.permission.USE_EXACT_ALARM" android:maxSdkVersion="32" />

needs to hold android.permission.SCHEDULE_EXACT_ALARM or android.permission.USE_EXACT_ALARM to set exact alarms.

cristian1206 commented 1 year ago

Hi @cristian1206 , I think you forgot the two permissions you added at step 2. Can you give us some insights on this? Thanks!

Hello everyone, how are you? I already updated my comment about the solution, apparently github did not take the code that I put. I hope it works for you.

cristian1206 commented 1 year ago

Good afternoon everyone, today the same thing happened to me with my application and the solution for me was the following:

  1. Update the version of react-native-push-notification to the latest version (8.1.1).
  2. Add in your manifest the following two permissions:
  3. By default notification permissions in android 13 are disabled so your notification will not be displayed. To solve this problem you can execute the following two solutions: 3.1. The first and quickest solution to see if your app works is to enable notification permissions manually. To do this go to settings>notifications>app settings>all apps and activate notifications in your app. 3.2. The second solution is to use a package like react-native-permissions(3.6.1) and evaluate the notifications permission (request(PERMISSIONS.ANDROID.POST_NOTIFICATIONS)) so that it is displayed on the screen and the user can give the permissions .

I hope the answer can be of help to those who are having this problem. Greetings

Worked! I also had to bump my targetSDKVersion to 33

This is also important. In addition to this, it is important to take into account that every time a new version of Android comes out, focus your application on that latest version of your OS, in this case Android 13.

harry524483 commented 1 year ago

Good afternoon everyone, today the same thing happened to me with my application and the solution for me was the following:

  1. Update the version of react-native-push-notification to the latest version (8.1.1).
  2. Add in your manifest the following two permissions:

<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>

<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />

  1. By default notification permissions in android 13 are disabled so your notification will not be displayed. To solve this problem you can execute the following two solutions:

3.1. The first and quickest solution to see if your app works is to enable notification permissions manually. To do this go to settings>notifications>app settings>all apps and activate notifications in your app.

3.2. The second solution is to use a package like react-native-permissions(3.6.1) and evaluate the notifications permission (request(PERMISSIONS.ANDROID.POST_NOTIFICATIONS)) so that it is displayed on the screen and the user can give the permissions .

I hope the answer can be of help to those who are having this problem. Greetings

Used this to configure notification permission with Android 13 (SDK 33) - https://github.com/zoontek/react-native-permissions#requestnotifications

IceColdLight commented 1 year ago

Bruh... I just had DND on... I debugged for 3 hours...

umerdogar commented 1 year ago

Any soluion to this as I am stucked in notifications. Cannot get them on android. It comes in the console but the popup doesnot appear

jumandika commented 1 year ago

I was following the solution and still not working, but in my case I have to manually request notification permission to make notification showing up in the status bar and notification bar.

try { await PermissionsAndroid.request( PermissionsAndroid.PERMISSIONS.POST_NOTIFICATIONS, ); } catch (error) { }

yashnerkar commented 8 months ago

Any soluion to this as I am stucked in notifications. Cannot get them on android. It comes in the console but the popup doesnot appear

same thing is happening with me also

Faizansiddiqui287 commented 6 months ago

How can I ask notification permission for Android 13 and above in react native version 0.61 using POST_NOTIFICATIONS?

cristian1206 commented 6 months ago

Hello @Faizansiddiqui287 , you can do something like the following:

import { Platform } from 'react-native';
import { request, PERMISSIONS, RESULTS } from 'react-native-permissions';

class NotificationsPermissions {
    static async requestPermissionsNotifications() {
        if (Platform.OS === "android" && Platform.Version >= 33) {
            try {
                const result = await request(PERMISSIONS.ANDROID.POST_NOTIFICATIONS);
                // Handling the result of the permit request
                if (result === RESULTS.GRANTED) {
                    console.log('Permissions granted');
                } else {
                    console.log('Permissions not granted');
                }
            } catch (error) {
                // Error handling during permission request
                console.error(error);
            }
        }
    }
}

// use example 
NotificationsPermissions.requestPermissionsNotifications();
Faizansiddiqui287 commented 6 months ago

Hello @Faizansiddiqui287 , you can do something like the following:

import { Platform } from 'react-native';
import { request, PERMISSIONS, RESULTS } from 'react-native-permissions';

class NotificationsPermissions {
    static async requestPermissionsNotifications() {
        if (Platform.OS === "android" && Platform.Version >= 33) {
            try {
                const result = await request(PERMISSIONS.ANDROID.POST_NOTIFICATIONS);
                // Handling the result of the permit request
                if (result === RESULTS.GRANTED) {
                    console.log('Permissions granted');
                } else {
                    console.log('Permissions not granted');
                }
            } catch (error) {
                // Error handling during permission request
                console.error(error);
            }
        }
    }
}

// use example 
NotificationsPermissions.requestPermissionsNotifications();

Hey thanks for your reply. This is my code. Below Android 13 it is working fine but for Android 13 and above it is giving permission is null error. Because I am using react native version 0.61 This is my code - if (Platform.OS === "android") {       try {         const OsVer = Platform.Version;         if (+OsVer >= 33) {           const res = await PermissionsAndroid.request(             PermissionsAndroid.PERMISSIONS.POST_NOTIFICATIONS,           );           if (res === "granted") {             getTokenFCM();             onNotificationOpenedAppFromQuit();             listenToBackgroundNotifications();             listenToForegroundNotifications();             onNotificationOpenedAppFromBackground();           }         }         else {           getTokenFCM();           onNotificationOpenedAppFromQuit();           listenToBackgroundNotifications();           listenToForegroundNotifications();           onNotificationOpenedAppFromBackground();         }

Faizansiddiqui287 commented 6 months ago

Hello @Faizansiddiqui287 , you can do something like the following:

import { Platform } from 'react-native';
import { request, PERMISSIONS, RESULTS } from 'react-native-permissions';

class NotificationsPermissions {
    static async requestPermissionsNotifications() {
        if (Platform.OS === "android" && Platform.Version >= 33) {
            try {
                const result = await request(PERMISSIONS.ANDROID.POST_NOTIFICATIONS);
                // Handling the result of the permit request
                if (result === RESULTS.GRANTED) {
                    console.log('Permissions granted');
                } else {
                    console.log('Permissions not granted');
                }
            } catch (error) {
                // Error handling during permission request
                console.error(error);
            }
        }
    }
}

// use example 
NotificationsPermissions.requestPermissionsNotifications();

Hey thanks for your reply. This is my code. Below Android 13 it is working fine but for Android 13 and above it is giving permission is null error. Because I am using react native version 0.61 This is my code - if (Platform.OS === "android") {       try {         const OsVer = Platform.Version;         if (+OsVer >= 33) {           const res = await PermissionsAndroid.request(             PermissionsAndroid.PERMISSIONS.POST_NOTIFICATIONS,           );           if (res === "granted") {             getTokenFCM();             onNotificationOpenedAppFromQuit();             listenToBackgroundNotifications();             listenToForegroundNotifications();             onNotificationOpenedAppFromBackground();           }         }         else {           getTokenFCM();           onNotificationOpenedAppFromQuit();           listenToBackgroundNotifications();           listenToForegroundNotifications();           onNotificationOpenedAppFromBackground();         }

If you have any solution for using POST_NOTIFICATION for react native version 0.61 please let me know

cristian1206 commented 6 months ago

@Faizansiddiqui287 Try to follow these steps and if you can also review the note:

Good afternoon everyone, today the same thing happened to me with my application and the solution for me was the following:

  1. Update the version of react-native-push-notification to the latest version (8.1.1).
  2. Add in your manifest the following two permissions:

<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>

<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />

  1. By default notification permissions in android 13 are disabled so your notification will not be displayed. To solve this problem you can execute the following two solutions:

3.1. The first and quickest solution to see if your app works is to enable notification permissions manually. To do this go to settings>notifications>app settings>all apps and activate notifications in your app.

3.2. The second solution is to use a package like react-native-permissions(3.6.1) and evaluate the notifications permission (request(PERMISSIONS.ANDROID.POST_NOTIFICATIONS)) so that it is displayed on the screen and the user can give the permissions . You could do something like the following

import { Platform } from 'react-native';
import { request, PERMISSIONS, RESULTS } from 'react-native-permissions';

class NotificationsPermissions {
    static async requestPermissionsNotifications() {
        if (Platform.OS === "android" && Platform.Version >= 33) {
            try {
                const result = await request(PERMISSIONS.ANDROID.POST_NOTIFICATIONS);
                // Handling the result of the permit request
                if (result === RESULTS.GRANTED) {
                    console.log('Permissions granted');
                } else {
                    console.log('Permissions not granted');
                }
            } catch (error) {
                // Error handling during permission request
                console.error(error);
            }
        }
    }
}

// use example 
NotificationsPermissions.requestPermissionsNotifications();

Note: Is important to take into account that every time a new version of Android comes out, focus your application on that latest version of your OS, in this case Android 14(34) or android 13(33).

Captura de pantalla 2024-03-16 a la(s) 12 17 28 p  m

I hope the answer can be of help to those who are having this problem. Greetings

cristian1206 commented 6 months ago

Hola@cristian1206Creo que olvidó los dos permisos que agregó en el paso 2. ¿Puede darnos alguna idea sobre esto? ¡Gracias!

Hello @gerardcastell , of course if these permissions are added within the AndroidManifest.xml, this file is located in the following path android/app/src/main/AndroidManifest.xml, as soon as you are in that path you add the following permissions:

chendhur007 commented 4 months ago

@cristian1206 i getting this error after updating this code FYI : this issues happened while Importing this import { request, PERMISSIONS, RESULTS } from 'react-native-permissions';

ERROR Invariant Violation: TurboModuleRegistry.getEnforcing(...): 'RNPermissions' could not be found. Verify that a module by this name is registered in the native binary. [Tue May 07 2024 16:18:06.651] ERROR Invariant Violation: Module AppRegistry is not a registered callable module (calling runApplication) [Tue May 07 2024 16:18:06.655] ERROR Invariant Violation: Module AppRegistry is not a registered callable module (calling runApplication)

cristian1206 commented 4 months ago

Hi @chendhur007, this seems to be an error with the compilation. In this case it indicates that your RNPermissions module is not in the binary, to solve this error you must install your artifact again, that is, you must compile your app again at the native level.

For iOS: npx react-native run-ios

For android: npx react-native run-android

mcanikhilprajapati commented 1 month ago

@Faizansiddiqui287 Try to follow these steps and if you can also review the note:

Good afternoon everyone, today the same thing happened to me with my application and the solution for me was the following:

  1. Update the version of react-native-push-notification to the latest version (8.1.1).
  2. Add in your manifest the following two permissions:

<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/> <uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />

  1. By default notification permissions in android 13 are disabled so your notification will not be displayed. To solve this problem you can execute the following two solutions:

3.1. The first and quickest solution to see if your app works is to enable notification permissions manually. To do this go to settings>notifications>app settings>all apps and activate notifications in your app.

3.2. The second solution is to use a package like react-native-permissions(3.6.1) and evaluate the notifications permission (request(PERMISSIONS.ANDROID.POST_NOTIFICATIONS)) so that it is displayed on the screen and the user can give the permissions . You could do something like the following

import { Platform } from 'react-native';
import { request, PERMISSIONS, RESULTS } from 'react-native-permissions';

class NotificationsPermissions {
    static async requestPermissionsNotifications() {
        if (Platform.OS === "android" && Platform.Version >= 33) {
            try {
                const result = await request(PERMISSIONS.ANDROID.POST_NOTIFICATIONS);
                // Handling the result of the permit request
                if (result === RESULTS.GRANTED) {
                    console.log('Permissions granted');
                } else {
                    console.log('Permissions not granted');
                }
            } catch (error) {
                // Error handling during permission request
                console.error(error);
            }
        }
    }
}

// use example 
NotificationsPermissions.requestPermissionsNotifications();

Note: Is important to take into account that every time a new version of Android comes out, focus your application on that latest version of your OS, in this case Android 14(34) or android 13(33). Captura de pantalla 2024-03-16 a la(s) 12 17 28 p  m I hope the answer can be of help to those who are having this problem. Greetings

this saved my time

Thanks a lot