Telegram-FOSS-Team / Telegram-FOSS

Unofficial, FOSS-friendly fork of the original Telegram client for Android
GNU General Public License v2.0
2.85k stars 366 forks source link

Unreliable Message Delivery since 3.1.3 #36

Closed vanitasvitae closed 8 years ago

vanitasvitae commented 8 years ago

Hi Since the latest update, push messages are wa, less reliable than in earlier versions. I didnt change any settings. Often messages dont appear even when I have the chat open. When Im in a group chat, i often get multiple messages at once delivered.

Im using Paranoid Android 5.1

ghost commented 8 years ago

@slp Ok I'll test it. Anything to catch this bug :)

ghost commented 8 years ago

Tests are done. @slp I'd rather prefer send you the logs in private since I don't know if there is sensitive data about my user etc. How do we do this? Email, telegram username...? Maybe I should delete previous logs too.

slp commented 8 years ago

@helfio Just sent you an email to your account at hiperones.es

slp commented 8 years ago

@helfio Thanks for the logs. Looking at them, I've noticed that the timeout functionality of epoll_wait, called from the JNI code, is not working properly.

I suspect the problem comes from Android's power management. Telegram doesn't set any kind of wakelock or alarm for its push service, so if your phone is working properly, going to deep sleep, and has no other background service waking it periodically, the notifications will never arrive.

At this point you're probably wondering how this works for the official app. Well, the answer is easy, Google's GCM wakes the phone for them.

I've just built a version which re-enables a present but disabled Alarm service. I have no easy way to test it, so I'm not sure if it works, but I think it's worth a shot: https://sinrega.mooo.com/index.php/s/9i8X6kkLwIXHJNt

If still don't fix the problem, I'll try to implement a more sophisticated solution.

ghost commented 8 years ago

Thank you again, and of course testing begins :)

vanitasvitae commented 8 years ago

I do have GApps and GCM on my phone. Telegram Messages do arrive, though they are delayed from time to times.

ghost commented 8 years ago

It works as expected but I get a lot of wakelocks and the battery drains quickly.

It has to be another way since the official app works (AFAIK) just fine without GCM. I mean, somehow Telegram-FOSS must be able to use Telegram upstream own push notification system, right? Maybe something similar to webhook system which is used in bots API. Any idea?

@vanitasvitae do you also use a Google account? I think GCM doesn't work without an account.

vanitasvitae commented 8 years ago

Yes i use GCM with my Google acc.

xavihernandez commented 8 years ago

@helfio @vanitasvitae Telegram-FOSS does not contain GCM part. So even if you enable it (by disabling Telegram Notifications Service), it won't be used. As noticed in description :

Several proprietary parts were removed from the original Telegram client, including Google Play Services for the location services and HockeySDK for self-updates. Push notifications through Google Cloud Messaging and the automatic SMS receiving features were also removed.

To use GCM you have to download Telegram client from Play Store.

vanitasvitae commented 8 years ago

@xavihernandez Ok, I just thought my installed GCM (I use it for Signal) wakes the phone anyways.

It makes sense, that measages are delayed if GCM does not wakr the phone on incoming telegram messages, but because of other events.

slp commented 8 years ago

@vanitasvitae

It makes sense, that measages are delayed if GCM does not wakr the phone on incoming telegram > messages, but because of other events.

Yup, that's exactly what's happening.

@helfio Telegram-FOSS is using a push connection (you can see it mentioned in your *_net.txt logs). But said push connection requires some maintenance, which is done at fixed intervals (each 180 seconds, in this case) waking the phone. If the phone oversleeps, the connection is broken, and notifications never arrive.

But don't worry. The current approach, while better than holding a permanent partial wakelock, it's still quite crude, as it wakes the phone every 30 seconds, even if there's nothing to do.

I can easily coordinate the timeout from the epoll_wait call in JNI code with the native Alarm, waking up only when it's really needed for maintaining the push connection (aprox. 1/6 less wake ups).

ghost commented 8 years ago

OK so since we know the last beta at least receive notifications well enough, and you can adjust the alarms and so, the next step is testing another beta with this improvements, right?

slp commented 8 years ago

I finally found a phone where I can reproduce this problem. I've been testing a new version with a coordinated Alarm for a few hours, and seems to work fine, while keeping the phone asleep most of the time.

@helfio If you don't mind giving it a try, I've uploaded it here: https://sinrega.mooo.com/index.php/s/ZSpKoZkEkFmzxDb

ghost commented 8 years ago

@slp testing!

slp commented 8 years ago

I've just commited a fix for this to the merge-3.3.1 branch, but I'm not comfortable releasing this on F-Droid until more people as tested it. You can download a binary from here: https://sinrega.mooo.com/index.php/s/H3CkzzR3Olhy7VM

If you're having this issue, or you don't but want to check if this change has any impact on your experience, please give it a try.

After working on this, I know why everyone is just using GCM. On recent versions of Android, maintaining a background connection in your app is ridiculously hard. The methods for waking the phone are not 100% reliable, and its behavior seem to change between phones.

The change I've implemented keeps the push connection alive, while keeping the CPU in Deep Sleep 95,4% of the time (on my test phone, at least). There are some punctual disconnections/reconnections from time to time, but those seem to be expected and innocuous.

xavihernandez commented 8 years ago

@slp

Thanks you! Lots of people disable "Telegram Notification Service" to use GCM instead?

Can we test this version, and keep our secret chats ? Or do we have to uninstall and reinstall due to different key signing?

slp commented 8 years ago

@xavihernandez Telegram-FOSS doesn't support GCM, so if you disable the Notification Service, you'll get no notifications at all (well, unless some other app wakes the phone, if Telegram is still in background).

Can we test this version, and keep our secret chats ? Or do we have to uninstall and reinstall due to > different key signing?

Yes, Marcus prepared the merge-3.3.1 branch to be a good neighbour and install alongside the old one, with name "Telegram Beta".

xavihernandez commented 8 years ago

@slp

Lmao I know that, but since you said "After working on this, I know why everyone is just using GCM" I assume you were talking about upstream... which has GCM support.

Oh ok thanks @Bubu too then!

ghost commented 8 years ago

@slp Testing from right now. Thank you again for work on this :)

ghost commented 8 years ago

In my case the app didn't miss a single notification in two days of normal use.

I think notification system is good enough :)

slp commented 8 years ago

@helfio Great, thanks for the report!

slp commented 8 years ago

@vanitasvitae @geileszeuch Version 3.3.1 is available on F-Droid. Could you please check if this issue is fixed for you in that version?

vanitasvitae commented 8 years ago

I already updated yesterday and until now I didnt face the problem again. Seems to work now :)

Ecron commented 8 years ago

@slp when disabling Telegram's Notification Service, a message is shown warning users about using GCM. As Telegram-FOSS doesn't have it, should you change a little that warning saying that, disabling the Service will leave the app without notifications?

Bubu commented 8 years ago

No problems here either with the update.

@Ecron: We could probably update the Messagebox... but then this should be done in all languages which I cannot do.

I'd like to close this issue though. The message box string would be a new one.

geileszeuch commented 8 years ago

No problems so far, but haven't been using Telegram heavily recently. I can tell that search and channels started working. Well done.

Ecron commented 8 years ago

@Bubu You have a point, sir.

xavihernandez commented 8 years ago

@Bubu I still cannot get my messages on time. I just received a SMS teling me that the sender has sent a message to me @ Telegram. WHen I opened Telegram I had 3 people who had talked to me without I have been noticed :(. Older message was sent 28 minutes before I opened Telegram.

Note that I'm connected in EDGE. And when I opened Telegram it said "Connecting..." then "Updating..." and ten messages came.

geileszeuch commented 8 years ago

Unfortunately, I experienced similar behaviour. Sometimes receiving several messages from hours ago when opening the app.

Ecron commented 8 years ago

@xavihernandez @geileszeuch did you check what happens if you use official Telegram app? (sorry if it's a silly question :sweat:)

xavihernandez commented 8 years ago

@Ecron Nop I haven't, because I have no Gapps on my phone, then no Play Store.

geileszeuch commented 8 years ago

Since I installed microG the problems seem to be gone. This is exactly what @slp described above.

xavihernandez commented 8 years ago

I got microG installed, but I have hade some delay once. It may be a ponctual issue (I meant on v3.3.1) I'll let you know, guys, if it happens again.

slp commented 8 years ago

@xavihernandez @geileszeuch I you're still experiencing problems, please consider running the beta version (https://sinrega.mooo.com/index.php/s/H3CkzzR3Olhy7VM) for a while. It will generate some logs I can use to diagnose the Alarm code (as I said before, I'm not surprised of its erratic behavior).

If you don't want to make such logs public (the *_net.txt ones may contain sensitive data), contact me on Twitter (@sergiolpascual).

slp commented 8 years ago

BTW, I've just took a look at microG GCM code (https://github.com/microg/android_packages_apps_GmsCore/blob/master/play-services-core/src/main/java/org/microg/gms/gcm/McsService.java), and they're just setting a repeating Alarm, which wakes the phone every 60 secs (by default, can be configured).

We're trying to be smarter than that, setting the alarm when the JNI's code expects it'll need to do some maintenance on the socket (usually, every 180 secs), but if microG's strategy works better (due to AlarmManager weird behavior on some Android versions), we can adopt (it's way easier than what we're doing right now).

Or, perhaps, we can make it configurable. That would add a purpose to that Notification Service check on the Settings screen ;-)

geileszeuch commented 8 years ago

@slp The last time I tried using that Beta I was unable to create secret chats, always being promted the message "An error occurred". It seems that the Beta does not create a separate account in Android's environment, but uses the same account name " Telegram" in Android's system, which may cause problems when running the official FOSS version and the Beta side by side with different numbers. So, some appropriate renaming in the source could easily fix that.

xavihernandez commented 8 years ago

@slp ok. I'll let you know if it happens again, thanks for your work :)

xavihernandez commented 8 years ago

@slp I just have had a delayed message. Sent at 17:26, received at 18:05. By the way I have received a second message at 18:04. I want to run Beta version but as @geileszeuch said it seems to be a little bit unsuable. I haven't installed it.

Want to notice you that delayed message appeared in Edge. Last time, it was when I was in Edge too. May be due to that. And then it may be due to my ISP. I would like to know if someone else experienced delayed messages in latest version.

francescortiz commented 8 years ago

I faced a similar situation and I solved it by using onTaskRemoved to relaunch the background service.

ThoAppelsin commented 7 years ago

I have resolved this issue by terminating many other (3-4) sessions I had open apparently, somehow, says Telegram Desktop, in the lands I've never been on. I may have used VPN at some point, and that could be the reason I had those sessions on my account. Anyway, I don't have this issue anymore. A shoutout to devs to investigate that, because I don't think this should cause such issues.

ryecoder commented 6 years ago

I encountered this problem also and found out that it is my Autostart manager shutting it down. Check you Autostart manager guys and whitelist it.

ghost commented 6 years ago

Oh god I didn't believe my friends when they had this issue. Now i changed my phone and I am having it.

It's been three years and it hasn't been solved.

I had a Motorola MotoG 3G and everything worked fine, I now have a Samsung J7 Prime and it went to Mars. I have to actually open Telegram when I am on the streets, or driving or else. There's no problem when I am home or when I am working cuz I use the desktop or web app and notifications work just fine. Is the phone that doesn't work

Bubu commented 6 years ago

This particular problem has looong been solved. But notifications in recent android versions are hard and there are potentially new problems now.

@javotroya please open a new issue posting your android version (old and new) and if you did exempt Telegram from the power-management-optimizations in android settings.

FreeSpirit9009 commented 4 years ago

We seem to be experiencing similar issues using 5.15.0. We also have WeChat and WhatsApp on that same device and their notifications are working fine. Android 9, Huawei Mate 10.

igorovic commented 3 years ago

Seems like the issue is back again. Not on my phone but many friends are reporting this issue.

martinorob commented 3 years ago

Same here. The annoying problem is back. And for this, my friends thins that telegram is not working and use WhatsApp instead 😡

iOS