Reputation: 39
I want to check push notification permission for both ios and android. I want to see if user has switched off the push notification permission from his device settings. Is there any plugin or any code i can take reference from if needed to be coded in native.
Upvotes: 2
Views: 38052
Reputation: 597
import { check,request, PERMISSIONS, openSettings, RESULTS,requestNotifications } from 'react-native-permissions';
//In Funciton or Class
var [perm, setPerm] = useState(false);
useEffect(() => {
setTimeout(function(){
requestNotifications(['alert', 'sound']).then(({status, settings}) => {
console.log(status);
if(status=='granted'){
setPerm(true);
}else if(status=='denied'){
openSettings();
}
});
},600);
},[]);
Possible statuses:
UNAVAILABLE: 'unavailable',
BLOCKED: 'blocked',
DENIED: 'denied',
GRANTED: 'granted',
LIMITED: 'limited',
Upvotes: 0
Reputation: 29
Android:
Actually the Push Notification permission lie in Normal Category Permission like INTERNET permission not in Dangerous Category Permission.
You don't have to ask for Push Notification permissions.
More details is here, https://stackoverflow.com/a/37294287/7188778
Update: Policy was changed see details here - source.android.com/docs/core/display/notification-perm
Upvotes: -1
Reputation: 29
You can use Notifee Library package to request for notification permission for both (ios and android) and also you can check the permission status but i don't think there is a package which states whether permission is switched off by user.
Upvotes: -1
Reputation: 9426
Official way https://github.com/react-native-community/react-native-permissions#checknotifications
import {requestNotifications} from 'react-native-permissions';
requestNotifications(['alert', 'sound']).then(({status, settings}) => {
// …
});
Upvotes: 5
Reputation: 10172
You can check react-native-permissions npm. After integrating you can use it like:
componentDidMount() {
Permissions.check('notification').then(response => {
// Response is one of: 'authorized', 'denied', 'restricted', or 'undetermined'
this.setState({ photoPermission: response })
})
}
There is another library that can help (I have not used it).
There is also native implementation for as suggested here.
Upvotes: 5