viktor.mozharovskiy
viktor.mozharovskiy
NNovu
Created by viktor.mozharovskiy on 7/11/2024 in #💬│support
@novu/headless: headlessService.fetchNotifications({onSuccess}) constantly receives latest state
Let me refine if we have an expected behaviour of fetchNotifications With code below we have onSuccess invoked on every notifications update, it looks like socket used there, but there is no direct reference to it in the documentation. You can also notice that we have this line of code listenNotificationReceiveChange(() => {}) - it's exactly the part that makes onSuccess handler invoked (as headlessService?.fetchNotifications option) Is this a behaviour we can rely on or it's kind of a bug? Is there any other way to get constant updates of the notifications with their latest state?
const fetchNotifications = async (onPushNotifications: OnPushNotificationsHandler) => {
if (!sessionInitialized) {
throw new Error('Session is not initialized')
}

headlessService?.fetchNotifications({
listener: () => {},
onSuccess: (response: IPaginatedResponse<IMessage>) => {
const { data } = response
const notifications = unifyNotifications(data)
onPushNotifications(notifications)
},
onError: (error) => {
// eslint-disable-next-line no-console
console.error('Failed to fetch notifications:', error)
},
query: { read: false },
page: 0,
})
}


initializeSession(userId, config.NovuIdentifier, novuSignature.toLocaleLowerCase())
.then(() => {
fetchNotifications(onPushNotifications)
listenNotificationReceiveChange(() => {})
return null
})
.catch((error: Error) => {
console.error('Failed to initialize session:', error)
})
const fetchNotifications = async (onPushNotifications: OnPushNotificationsHandler) => {
if (!sessionInitialized) {
throw new Error('Session is not initialized')
}

headlessService?.fetchNotifications({
listener: () => {},
onSuccess: (response: IPaginatedResponse<IMessage>) => {
const { data } = response
const notifications = unifyNotifications(data)
onPushNotifications(notifications)
},
onError: (error) => {
// eslint-disable-next-line no-console
console.error('Failed to fetch notifications:', error)
},
query: { read: false },
page: 0,
})
}


initializeSession(userId, config.NovuIdentifier, novuSignature.toLocaleLowerCase())
.then(() => {
fetchNotifications(onPushNotifications)
listenNotificationReceiveChange(() => {})
return null
})
.catch((error: Error) => {
console.error('Failed to initialize session:', error)
})
12 replies