Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Notification not showing on Android 13 and higher #246

Open
andidev opened this issue Oct 10, 2024 · 2 comments
Open

Notification not showing on Android 13 and higher #246

andidev opened this issue Oct 10, 2024 · 2 comments

Comments

@andidev
Copy link

andidev commented Oct 10, 2024

A notification will be shown when the task is running, it is not possible to start the service without it. The notification will only be visible in Android.

this is no longer true for Android 13 and higher when using React Native 0.73 or higher (I think or maybe all versions)
Think it has to do with the target sdk bump.
For some reason the backround task is still running in background for me without a notification which I thought was required.
Though maybe it will get killed eventually because of this?

This is how I solved it but maybe this should be in docs? and maybe in implementation?

useEffect(() => {
    const askForPermissionsAndStartBackgroundService = async () => {
        const notificationsAllowed = await askForPostNotificationsPermission();
        if (!notificationsAllowed) return;
        await BackgroundService.start(
            ...config,
        );
    };
    askForPermissionsAndStartBackgroundService();
}, []);
export async function askForPostNotificationsPermission(): Promise<boolean> {
    if (Platform.OS !== 'android') return false;
    if (Platform.Version < 33) return false; // Only request for Android versions 13 and above

    try {
        const granted = await PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.POST_NOTIFICATIONS, {
            title: 'Notification Permission',
            message: 'We need your permission to show you notifications',
            buttonNeutral: 'Ask Me Later',
            buttonNegative: 'Cancel',
            buttonPositive: 'OK',
        });
        if (granted === PermissionsAndroid.RESULTS.GRANTED) {
            console.log('Permission Granted. You can now receive notifications.');
            return true;
        } else if (granted === PermissionsAndroid.RESULTS.DENIED) {
            console.log('Permission Denied. Notification permission is required.');
        } else if (granted === PermissionsAndroid.RESULTS.NEVER_ASK_AGAIN) {
            console.log('Permission Blocked. Notification permission has been blocked.');
            Alert.alert('Notification has not been granted!', 'Please long press on app icon, select App Info, select Notifications and enable all notifications.');
        }
    } catch (e) {
        console.warn('requestPostNotificationsPermission error', e);
    }
    return false;
}
@muramidaza
Copy link

I tried asking for permission POST_NOTIFICATIONS on Android 13, but application crashes

@smitha-2020
Copy link

@andidev @muramidaza Any progress? I am facing the same issue. App Crashes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants