My project is running on react-native 0.68.5 and I can't upgrade it, is there a way to ask for notifications using this react native version?
Right now it's working with previous Android versions, but I want my app to ask for these kind of permissions on newer android versions.
My project is running on react-native 0.68.5 and I can't upgrade it, is there a way to ask for notifications using this react native version?
Right now it's working with previous Android versions, but I want my app to ask for these kind of permissions on newer android versions.
Share Improve this question asked Jul 19, 2023 at 16:13 juliol1juliol1 272 silver badges7 bronze badges 1- Take a look at reactnative.dev/docs/0.68/permissionsandroid. – user18309290 Commented Jul 19, 2023 at 16:42
3 Answers
Reset to default 6You need past the below line in AndroidManifest.xml
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
Create a function for request the permission by using below code.
import { Platform,PermissionsAndroid } from 'react-native';
const requestNotificationPermission = async () => {
if(Platform.OS ==="android"){
try {
PermissionsAndroid.check('android.permission.POST_NOTIFICATIONS').then(
response => {
if(!response){
PermissionsAndroid.request('android.permission.POST_NOTIFICATIONS',{
title: 'Notification',
message:
'App needs access to your notification ' +
'so you can get Updates',
buttonNeutral: 'Ask Me Later',
buttonNegative: 'Cancel',
buttonPositive: 'OK',
})
}
}
).catch(
err => {
console.log("Notification Error=====>",err);
}
)
} catch (err){
console.log(err);
}
}
};
Call this requestNotificationPermission() function when you want to ask permission for Android Notification permission.
There's no defined permission for notifications.
There is a case when you are using RN v0.6x and developing an app on latest android 31 or higher. In this case, permission for notification has to be asked explicitly.
Since there isn't such permission available in these legacy versions, there is no other way but to move to 0.70+ version or prompt the the user to manually give permission to app by going to settings.
Previous solutions didn't work for me so i needed to add manualy this permission to react-native.
- Add permission in file /node_modules/react-native/Libraries/PermissionsAndroid/PermissionsAndroid.js
const PERMISSIONS = Object.freeze({
...
POST_NOTIFICATIONS: 'android.permission.POST_NOTIFICATIONS', // <- add this
...
});
class PermissionsAndroid {
PERMISSIONS: {|
...
POST_NOTIFICATIONS: string, // <- add this
...
|} = PERMISSIONS;
RESULTS: {|
DENIED: $TEMPORARY$string<'denied'>,
GRANTED: $TEMPORARY$string<'granted'>,
NEVER_ASK_AGAIN: $TEMPORARY$string<'never_ask_again'>,
|} = PERMISSION_REQUEST_RESULT;
- patch react-native
I used react-native-patch from https://www.npmjs./package/patch-package and then
yarn patch-package react-native