After 10 minutes of turning off the the phone screen (my device is set to immediately lock after screen turning off), I stop receiving notifications from Messenger, WhatsApp, Gmail and so on. This happens only when the phone is connected to WiFi and NOT charging. On cellular data, notifications arrive timely, as is the case with WiFi when the phone is charging. I discovered this "solution" after months of missing important calls and messages.
The phone came with Android 4.4 Sense, which I didn't use for more than a few days, hence can't really confirm if the problem was present there (it most likely wasn't; those few days I did get timely notifications, as far as I remember). I switched to LineageOS 14.1, Android 7.1.2, and noticed this problem for the first time. After quite some time of thinking it's a bug in LineageOS, I converted the device to GPE, installed stock Android 6.0 (the last version of Android shipped by Google for this device), only to see the problem was worse there. At least in LineageOS 14.1 notifications would stop after 10 minutes. In GPE Marshmallow, it would stop after 2-4 minutes, and the problem persisted even while charging. Now I'm using LineageOS 15.1 from this thread. Notifications STILL stop arriving timely when not charging and connected to WiFi.
To make it clear, I tried pretty much everything suggested by people for similar problems in other devices. I have battery optimization turned off for EVERY app on the list (this DOES seem to be a power management issue, since while charging, notifications work fine). WiFi was set to be active "Always" in Android 6.0 and 7.1.2 (didn't find that option on LineageOS 15.1/Android 8.1). Made unrestricted data access available for all apps. Battery is set to "Performance". All apps allowed to notify. Installed "WiFi Keep Alive" kind of apps. No use!
Yes, every ROM installation was "clean". Yes, the device firmware is the latest one shipped by Google (from here). The device is S-OFF. I only have Google Apps installed, never rooted it. This making me go nuts. If anyone can provide me with a solution, I'll be grateful beyond words.