my mobile app uses notifee library to display and manipulate notifications, and it works fine with targetSdkVersion 33. however when I upgrade to 34, the onForegroundEvent event is no longer working (everything else works fine). like the code below console.log(
[onForegroundEvent] notification id: ${notification?.id}, event type: ${EventType[type]}${pressActionLabel},
); not shown). Help me please
"react-native": "0.71.17",
"@notifee/react-native": "^7.7.0",
buildToolsVersion = "34.0.0"
minSdkVersion = 23
compileSdkVersion = 34
targetSdkVersion = 34
ndkVersion = "26.1.10909125"
kotlinVersion = "1.9.22"
import React, {useEffect} from 'react';
import {Button, Platform, StyleSheet, Text, View} from 'react-native';
import notifee, {
EventType,
AuthorizationStatus,
AndroidImportance,
AndroidVisibility,
} from '@notifee/react-native';
import BootSplash from 'react-native-bootsplash';
function App() {
const requestUserPermission = async () => {
const settings = await notifee.requestPermission();
await BootSplash.hide({fade: true});
if (settings.authorizationStatus >= AuthorizationStatus.AUTHORIZED) {
console.log('Permission settings:', settings);
} else {
console.log('User declined permissions');
}
};
const checkChannelPermission = async () => {
const channel = await notifee.getChannel('default');
if (channel?.blocked) {
console.log('Channel is disabled');
} else {
console.log('Channel is enabled:', channel);
}
};
// Subscribe to events
useEffect(() => {
checkChannelPermission();
return notifee.onForegroundEvent(async ({type, detail}) => {
const {notification, pressAction} = detail;
const pressActionLabel = pressAction
? `, press action: ${pressAction?.id}`
: '';
console.log(
`[onForegroundEvent] notification id: ${notification?.id}, event type: ${EventType[type]}${pressActionLabel}`,
);
});
}, []);
useEffect(() => {
(async () => {
await requestUserPermission();
})();
}, []);
const notification = {
title: 'Basic',
body: 'notification',
android: {
channelId: 'default',
pressAction: {
id: 'default',
},
},
ios: {
sound: 'default',
},
};
const onDisplayNotificationPress = async () => {
await notifee.deleteChannel(notification.android?.channelId || 'default');
console.log('changelId:', notification.android?.channelId);
// Create a channel
await notifee.createChannel({
id: notification.android?.channelId || 'default',
name: notification.android?.channelId || 'default',
importance: AndroidImportance.HIGH,
});
try {
await notifee.displayNotification(notification);
} catch (e) {
console.error(e);
}
};
return (
<View style={styles.container}>
<View style={styles.container}>
<View style={styles.content}>
<View style={styles.contentItem}>
<View style={styles.contentItemText}>
<Text>{`Notification: ${notification.title}`}</Text>
</View>
<View style={[styles.button]}>
<Button
color={(Platform.OS === 'ios' && '#fff') || '#44337A'}
title={'Display Notification'}
onPress={onDisplayNotificationPress}
/>
</View>
</View>
</View>
</View>
</View>
);
}
export default App;
New contributor
Phan Anh Tuấn is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.