Thank you for this tip!
What fixed it for me was enabling Prevent automatic sleeping on power adapter when the display is off in Settings -> Battery -> Options. (The Options-button is at the bottom of the Battery screen, you may have to scroll down to reach it.)
More on my configuration: I have a MacBook Air that is connected to (and drawing power from) a Studio Display. The OS is Sonoma 14.5. I use Messages in iCloud and message syncing between devices was working great for me until a few months ago. (It could be that that was when I upgraded to Sonoma, I can't think of any other recent configuration change.) It basically stopped syncing when the computer was asleep. I was able to manually sync the MacBook with my iPhone by initiating a sync on my iPhone and then on my MacBook. But this is very cumbersome, because you have to do it every time the computer wakes up from sleep -- I was ready to give up on Messages on the Mac entirely. For what it's worth, I also have the following related settings:
Enable Power Nap: Only on Power Adapter
Wake for network access: Only on Power Adapter
Even if you have a different configuration than me you should try adjusting these settings -- perhaps they should be slightly different for different configurations, but I'm convinced the root cause lies here.
Good luck!