invertase / notifee

⚛️ A feature rich notifications library for React Native.
https://notifee.app
Apache License 2.0
1.82k stars 215 forks source link

issue in setBackgroundMessageHandler() , not able to display notification #1104

Open abdulragib opened 1 week ago

abdulragib commented 1 week ago

i have declare setBackgroundMessageHandler in index.js , at run time , event is trigger in background but onDisplayNotificaitonNotification does not called

messaging().setBackgroundMessageHandler(async remoteMessage => { const data = JSON.parse(remoteMessage.data.message); -->> parsing bcz receiving json in string format console.log('Message handled in the Background mode!', data); requestbatteryOptimizationTurnOff(); onDisplayNotificaiton(data); });

export const onDisplayNotificaiton = async data => { if (Platform.OS === 'ios') { await notifee.requestPermission(); }

const channelId = await notifee.createChannel({ id: 'message_notification', name: 'Notification', visibility: AndroidVisibility.PUBLIC, importance: AndroidImportance.HIGH, sound: 'oxi_tune', });

const notificationId = 'notify';

await notifee.displayNotification({ id: notificationId, title: ${data.senderId.fullname}, body: ${data.content}, android: { channelId, // Ensure this matches your notification channel ID largeIcon: 'ic_launcher', // Add your icon here sound: 'oxi_tune', // Use default sound or specify custom sound // Recommended to set a category category: AndroidCategory.MESSAGE, visibility: AndroidVisibility.PUBLIC, importance: AndroidImportance.HIGH, fullScreenAction: { id: channelId, }, }, ios: { sound: 'default', foregroundPresentationOptions: { badge: true, sound: true, banner: true, list: true, }, }, }); // Set timeout to hide the notification after a few seconds (e.g., 5 seconds) setTimeout(async () => { await notifee.cancelNotification(notificationId); console.log('Notification cancelled after 7 seconds'); }, 7000); // 5000 ms = 5 seconds };

abdulragib commented 1 week ago

problem solved,small mistake in index.js

fixed add in onBackgroundEvent: // Keep the notification for a bit longer before cancelling setTimeout(async () => { await notifee.cancelNotification(notification.id); console.log('Notification cancelled after delay'); }, 10000); // Delayed cancellation after 10 seconds