Skip to main content

I want to display the message count on my custom intercom launcher but I find the count is not being accurately reported by the onUnreadCountChange function until the Intercom widget is opened. When the page is loaded and the handler is initially registered it always returns 0 unread messages until the widget is opened and then it will return the correct number (in this case 2).

 

Here’s an example of what is being logged to the handler before (on load) and after the intercom launcher is clicked. Once clicked the handler receives the wrong number of messages (1) before finally returning the correct number of unread messages (2).

import Intercom, { shutdown, onUnreadCountChange, update, showNewMessage, hide } from "@intercom/messenger-js-sdk";
// rest of react setup ....
useEffect(() => {
const initialize = () => {
Intercom({
app_id: INTERCOM_APP_ID,
name:<redacted>
created_at: <redacted>,
custom_launcher_selector: '#support',
hide_default_launcher: true,
user_hash: <redacted>,
user_id: <redacted>
company: {
company_id: <redacted>,
name: <redacted>,
},
});

const handleUnreadCountChange = (count) => {
console.log("whats the count: ", count); // returns 0 until the intercom widget is opened
setUnreadCount(count);
};

onUnreadCountChange(handleUnreadCountChange);
// showNewMessage(); if run the widget will open and the correct count of unread messages will be displayed

return () => {
shutdown();
};
}
};
initialize();
}, ])

Hey ​@Michael Toth 👋 Jacques here from Support Engineering!

The issue you're experiencing with the onUnreadCountChange function not reporting the correct number of unread messages until the Intercom widget is opened seems like it could be related to the timing of when the function is called and when the Intercom system updates the unread count. The onUnreadCountChange method is designed to be called immediately when invoked and again whenever the current number of unread messages changes. However, it seems that the unread count is not being updated in real-time on page load, which is why it initially reports 0 unread messages.

You may need to manually trigger an update to the unread count after the page loads but before the user interacts with the widget. 

  • Register the onUnreadCountChange function after confirming the Intercom widget has loaded.
  • Consider manually updating the unread count on page load to ensure accuracy before the user interacts with the widget.

If the issue persists, you may need to reach out to our support team for further assistance so they can dig deeper into this issue for you!


Thanks! For anyone else reading, what resolved it for me was calling boot after initializing Intercom and before registering the `onUnreadCountChange` function. I was a little surprised calling Intercom({ … }) didn’t boot the app but happy to have it resolved.


@Michael Toth Glad you were able to get this resolved! :)


Reply