N
Novu•3w ago
David 😎

Novu Headless - notifications not showing for custom subscriber IDs

I have an app that accepts user's first name, last name, and first name. Then, it generates an ID similar 7sa7ejga which is also used to create the user's Subscriber ID immediately after signing in. Now, I'm trying to fetch the user's notifications via Novu Headless but it's empty but it's showing on my Novu Activity Dashboard. Here is the code snippet for fetching the notifications:
const fetchNotifications = useCallback(() => {
const headlessService = headlessServiceRef.current;
if (!headlessService) return;
headlessService.fetchNotifications({
listener: ({ data, error, isError, isFetching, isLoading, status }) => {
console.log({ data, error, isError, isFetching, isLoading, status });
},
onSuccess: (response: IPaginatedResponse<IMessage>) => {
console.log({
notifications: response,
});
setNotifications(response.data);
},
page: 1, // page number to be fetched
});
}, []);


useEffect(() => {
const headlessService = new HeadlessService({
applicationIdentifier: process.env.NEXT_PUBLIC_NOVU_APP_ID!,
subscriberId: localStorage.getItem("subscriberId")!,
});
headlessService.initializeSession({
listener: (res: FetchResult<ISession>) => {
headlessServiceRef.current = headlessService;
console.log(res);
},
onSuccess: (session: ISession) => {
fetchNotifications();
},
onError: (error) => {
console.error(error);
},
});
}, [fetchNotifications]);
const fetchNotifications = useCallback(() => {
const headlessService = headlessServiceRef.current;
if (!headlessService) return;
headlessService.fetchNotifications({
listener: ({ data, error, isError, isFetching, isLoading, status }) => {
console.log({ data, error, isError, isFetching, isLoading, status });
},
onSuccess: (response: IPaginatedResponse<IMessage>) => {
console.log({
notifications: response,
});
setNotifications(response.data);
},
page: 1, // page number to be fetched
});
}, []);


useEffect(() => {
const headlessService = new HeadlessService({
applicationIdentifier: process.env.NEXT_PUBLIC_NOVU_APP_ID!,
subscriberId: localStorage.getItem("subscriberId")!,
});
headlessService.initializeSession({
listener: (res: FetchResult<ISession>) => {
headlessServiceRef.current = headlessService;
console.log(res);
},
onSuccess: (session: ISession) => {
fetchNotifications();
},
onError: (error) => {
console.error(error);
},
});
}, [fetchNotifications]);
No description
No description
12 Replies
Ask Inkeep
Ask Inkeep•3w ago
Hi @David 😎, I'm an AI assistant for Novu. While a team member reviews this question, I'll see if I can find relevant sources for your question. ----------------------------- While I generate an answer, here are some sources that might be helpful: * Headless API Reference * Get Started * How to use Headless Notification Center * API Reference ----------------------------- Answer Based on the code snippet and the issue you're experiencing with fetching notifications using Novu Headless for custom subscriber IDs, it looks like you're properly initializing the session and attempting to fetch notifications. If notifications are visible in the Novu Activity Dashboard but not showing up when fetching via the Headless API, here are a few potential issues to check: 1. Subscriber ID Match: Ensure that the subscriberId used in the HeadlessService initialization matches exactly the Subscriber ID for which notifications are intended. Any discrepancy, even minor, would result in fetching no notifications because the Headless API fetches notifications specifically for the provided subscriberId. 2. Session Initialization: The session initialization seems to be correct. However, make sure that the applicationIdentifier and subscriberId are correctly fetched and not null or undefined. A common issue could be reading from localStorage before it's set, especially if this code runs immediately upon user login. 3. Environment Variables: Verify that process.env.NEXT_PUBLIC_NOVU_APP_ID is correctly set and accessible within the environment where your application is running. Environment variables not being properly set can lead to issues during the initialization of the HeadlessService. 4. Error Handling: Your current implementation logs errors to the console. Check the console for any errors that might indicate why the notifications are not being fetched. The onError callback in both initializeSession and fetchNotifications should provide insight into any issues occurring during these processes. 5. Permissions and Configuration: Ensure that the subscriber ID has the correct permissions and is configured to receive notifications within the Novu dashboard settings. If there are misconfigurations in the
Ask Inkeep
Ask Inkeep•3w ago
notification settings for the subscriber or the application, it could lead to issues fetching notifications. 6. Correct Use of useCallback and useEffect Hooks: Your usage of useCallback and useEffect seems appropriate, but double-check that the dependency array for useCallback and any other hook is correctly set up to trigger the desired behavior in your React component's lifecycle. 7. Check for Successful Session Initialization: Before calling fetchNotifications, ensure that the session has been successfully initialized. Your code does initiate fetching notifications upon successful session initialization, but it's worth double-checking that the session is indeed being established as expected. 8. API Version and Documentation: Ensure you're using a version of the Novu Headless package that matches the documentation you're following. The Novu ecosystem is actively developed, and methods or their signatures might change between versions. If after checking these areas the issue persists, consider reaching out to Novu's support or community forums with specifics about your implementation, as there might be configuration or account-specific details affecting the behavior (1) (2) .
Pawan Jain
Pawan Jain•3w ago
@David 😎 As per your above code, when do you call fetchNotifications function?
David 😎
David 😎•3w ago
When the page load. and I send a new notification when the users submits a form
Pawan Jain
Pawan Jain•3w ago
Do you see any error in console tab or network tab?
David 😎
David 😎•3w ago
No, it just fetches an empty array.
Pawan Jain
Pawan Jain•3w ago
I am suspecting, session is not yet initialized
David 😎
David 😎•3w ago
It has or does Subscriber ID have a desired length?
No description
Pawan Jain
Pawan Jain•3w ago
can you check if appId and subscriberid combination is correct?
David 😎
David 😎•3w ago
yes, it's correct.
Novu_Bot
Novu_Bot•3w ago
@David 😎, you just advanced to level 5!
David 😎
David 😎•3w ago
@Pawan Jain It's showing in the notification center but not showing in the notification API call. I even tried hard-coding the credentials but the API call is not returning the recent notifications.
No description
Want results from more Discord servers?
Add your server
More Posts
Not able to see the WEB UI on 4200I have been trying to setup the novu on my local machine after too many tries. i was able to run apiHow can I integrate in-app notifications in React Native?The Novu documentation contains integration guides for 'React' and other web apps. I need to integraGetting error "POST https://mydomain.com/api/v1/widgets/session/initialize 404 (Not Found)my .env example # Root URL REACT_APP_WS_URL=$HOST_NAME/ws API_ROOT_URL=$HOST_NAME/api DISABLE_USER_Firebase is forcing users to migrate from FCM to HTTP v1 (Does Novu supports HTTP v1)??Hi everyone I've got an email from firebase (check the attached email on the bottom) And I was wondWhere to report a broken Novu Cloud API?Looking at the support channel seeing little action, where should I report a broken API endpoint? hCannot get global preference for a subscriber (status: 400)If I try to use the endpoint `https://api.novu.co/v1/subscribers/{subscriberId}/preferences/global` Is there any way to open the notification container automatically when getting new notification?Avatar not showing in notification center when triggering in-app messagesHi everyone, The subscriber's avatar is not showing in the notification center, even after creatingAre there any plans for Echo in Novu cloud?In order to have my workflows defined in code and version control, I should use Echo. If I understaTriggering a workflow using topics does not do anythingHi everyone, We've self-hosted Novu and were able get it in a working state. We're able to send noti