You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
i have some problem about my notification no data while in app and when quit state. here is my code to handle notification, am i doing something wrong? please help me, below is the code:
useNotificationFCM:
import{storage}from"@/services/storage";import{RootStackParamList}from"@/types/navigation";importnotifee,{AndroidImportance,EventDetail,EventType,}from"@notifee/react-native";importmessaging,{FirebaseMessagingTypes,}from"@react-native-firebase/messaging";import{NavigationProp,useNavigation}from"@react-navigation/native";import{useEffect}from"react";import{PermissionsAndroid,Platform}from"react-native";exportconstAndroidNotificationChannelId="App";exportconstAndroidNotificationChannelName="App";constuseNotificationFCM=()=>{constnavigation=useNavigation<NavigationProp<RootStackParamList>>();// const authToken = useUserStore(store => store.authToken);asyncfunctiononReceiveMessage(remoteMessage: FirebaseMessagingTypes.RemoteMessage|EventDetail){console.log("Received notification message: ",remoteMessage);awaitnotifee.displayNotification({title: remoteMessage.notification?.title||"App",body: remoteMessage.notification?.body||"New notification",android: {channelId: AndroidNotificationChannelId,importance: AndroidImportance.HIGH,ongoing: true,smallIcon: "ic_stats_notification",color: "#000000",},});}constrequestUserPermission=async()=>{constauthStatus=awaitmessaging().requestPermission();if(Platform.OS==="ios"){constenabled=authStatus===messaging.AuthorizationStatus.AUTHORIZED||authStatus===messaging.AuthorizationStatus.PROVISIONAL;returnenabled;}awaitPermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.POST_NOTIFICATIONS);// Request permissions (required for iOS)awaitnotifee.requestPermission();// Create a channel (required for Android)awaitnotifee.createChannel({id: AndroidNotificationChannelId,name: AndroidNotificationChannelName,importance: AndroidImportance.HIGH,});returntrue;};asyncfunctionregisterFCMTokenOnAppStart(){if(Platform.OS==="android"){awaitmessaging().registerDeviceForRemoteMessages();}try{consttoken=awaitmessaging().getToken();console.log("registerFCMToken-->",token);if(token)storage.set("deviceToken",token);}catch(error){console.log("registerFCMToken Device Token error ",error);}}asyncfunctiononAppOpenedByNotification(){constinitialNotification=awaitnotifee.getInitialNotification();console.log("App opened by notification ",initialNotification);if(initialNotification){console.log("Notification caused application to open",initialNotification.notification);}}useEffect(()=>{// if (authToken) {voidregisterFCMTokenOnAppStart();},[]);useEffect(()=>{asyncfunctioninit(){console.log("Initializing FCM handler");awaitrequestUserPermission();awaitonAppOpenedByNotification();// messaging().setBackgroundMessageHandler(onReceiveMessage);messaging().onNotificationOpenedApp((remoteMessage: FirebaseMessagingTypes.RemoteMessage)=>{console.log("Notification received OPEN APP",remoteMessage);// navigate in here});}voidinit();constunsubscribe=messaging().onMessage(onReceiveMessage);return()=>{unsubscribe();};},[]);useEffect(()=>{constunsubscribe=notifee.onForegroundEvent(({ type, detail })=>{console.log("Notification received in foreground",type,detail);switch(type){caseEventType.DISMISSED:
console.log("User dismissed notification",detail.notification);break;caseEventType.PRESS:
console.log("User pressed notification",detail.notification);// handle navigate in herebreak;caseEventType.DELIVERED:
console.log("Notification delivered",detail.notification);break;caseEventType.FG_ALREADY_EXIST:
console.log("Notification already in foreground",detail.notification);break;caseEventType.TRIGGER_NOTIFICATION_CREATED:
console.log("Notification triggered");break;default:
break;}});notifee.onBackgroundEvent(async({ type, detail })=>{console.log("Notification received in background",type,detail);switch(type){caseEventType.ACTION_PRESS:
console.log("User pressed notification from background",type,detail);if(detail.notification?.data&&Object.keys(detail.notification?.data).length>0){// handle navigate in here}else{navigation.navigate("Notifications");}break;caseEventType.DISMISSED:
console.log("User dismissed notification from background",type,detail);break;caseEventType.DELIVERED:
console.log("Notification delivered from background",detail.notification);break;caseEventType.PRESS:
console.log("User pressed notification from background",type,detail);break;caseEventType.FG_ALREADY_EXIST:
console.log("Notification already in foreground from background",detail.notification);break;caseEventType.TRIGGER_NOTIFICATION_CREATED:
console.log("Notification triggered from background");break;default:
break;}});return()=>{unsubscribe();};},[]);};exportdefaultuseNotificationFCM;
`
Hello 👋, to help manage issues we automatically close stale issues.
This issue has been automatically marked as stale because it has not had activity for quite some time.Has this issue been fixed, or does it still require attention?
This issue will be closed in 15 days if no further activity occurs.
i have some problem about my notification no data while in app and when quit state. here is my code to handle notification, am i doing something wrong? please help me, below is the code:
useNotificationFCM:
NotificationHandler:
And I haven't done anything in index.js yet, thank you everyone for reading the article, I hope to get help.
The text was updated successfully, but these errors were encountered: