ntfy icon indicating copy to clipboard operation
ntfy copied to clipboard

Bug: iOS notifications sometimes received twice

Open robfox92 opened this issue 2 years ago • 12 comments

I have a script that runs every day at 00:00, and will sometimes send a notification. This morning I saw two notifications on my phone - one at 00:00 and one at 00:07. On my (self-hosted) ntfy I only see a single notification in the web UI (at 00:00). In the ntfy ios app I also see a single notification for 00:00.

It seems like the popup notification was sent twice for some reason? I'll try and replicate it later today.

robfox92 avatar Jun 07 '22 00:06 robfox92

Do they show in the UI twice? I am pretty sure I have experienced this as well, but I thought I imagined it. Since I don't use an iOS device myself during day to day, it is much less likely to happen to me.

Try hard-killing the app and seeing if you see one or two notifications in the list.

binwiederhier avatar Jun 07 '22 00:06 binwiederhier

Note to self: my guess is that this is another one of those Core Data inconsistencies across processes.

binwiederhier avatar Jun 07 '22 01:06 binwiederhier

I made the mistake of clearing the notifications before taking a screenshot - I'm trying to replicate again and will grab one if I can.

The duplicate notifications just appeared in the notifications tray. Only a single one showed in the ntfy app.

Site note - how do I put a label on this issue?

robfox92 avatar Jun 07 '22 01:06 robfox92

It's potentially related to "do not disturb" mode. I just turned it on, sent 1 notification via the web ui and after a few minutes I turned off dnd mode.

image

image

robfox92 avatar Jun 07 '22 02:06 robfox92

That's a good lead, thank you. I'll investigate this when I get to it.

Here's my current hunch:

Every 20 minutes or so, the app polls for new messages from the server. If a message is not in the local database, it'll display it as a local notification. If the remote message eventually arrives via Firebase, there is no way for me to silence the notification. It's impossible without the "notification filtering entitlement", which Apple denied me when I requested it.

It could be that in DND mode, messages arrive neither via Firebase, nor is polling allowed to execute. Once you come out of DND, both run at the same time and race, and two notifications are displayed. That's just a guess though. I'd have to test it.

binwiederhier avatar Jun 08 '22 00:06 binwiederhier

I got this overnight. I have a parrot tell me everything is still working at 4am.

Latest iOS app, Latest ntfy server via docker self hosted, it was during nightly fixed DND time for both notifications. Screen shots if it's helpful.

Screen Shot 2022-06-12 at 08 59 39 Screen Shot 2022-06-12 at 09 01 03

milksteakjellybeans avatar Jun 12 '22 16:06 milksteakjellybeans

Thanks. Definitely helpful to confirm it. I wish I could ask you for logs, but I haven't figured out how to write/persist them in iOS so you can share them (like I did in Android). That would make things so much easier.

binwiederhier avatar Jun 12 '22 16:06 binwiederhier

I set up a cron job to send myself a notification at 1am every day (while quiet hours are on) and this morning the notifications had timestamps of 01:00 and 01:15.

Are more screenshots helpful at this point?

If you get a testflight build set up to try and pinpoint this I'm keen to run it.

robfox92 avatar Jun 13 '22 00:06 robfox92

It seems like what might be happening here is that the iOS instant notification is received and if there is no interaction with the phone or ntfy app, the non-instant version of the notification also eventually makes its way to the phone and you get the double notification. This is only conjecture from my observations using the iOS app and not anything deeper than that. (and I have not upgraded beyond 1.25.2 server yet)

milksteakjellybeans avatar Jun 17 '22 21:06 milksteakjellybeans

I can also confirm duplicate notifications arriving 5-15 minutes later than the original. I will add that I do not use Do Not Disturb but I still experience the issue when I don't open the app to look at the notification. The correlation to Do Not Disturb may be a coincidence. The app itself does not show duplicate messages - the issue is only with notifications.

I've been loving this service and the new iOS app. Keep up the great work!

majoragee avatar Jul 16 '22 15:07 majoragee

So my guess is that this is caused by two things:

  1. The first message is received via Firebase/APNS and persisted to Core Data. Messages received via APNS run in the Notification Service Extension (NSE), not in the main app. They do, however, persist to the same Core Data database.
  2. The second message is received from the polling mechanism: Every 20 minutes or so, the app receives a "poll all topics" message on the internal ~poll topic. That will instruct the main app to poll for new messages. To find out the last message it received, it uses the lastNotificationId field and then polls using /mytopic/json?since=$id. This ID is coming from Core Data.

The issue is this (this is a GUESS!): The Core Data database is not properly in sync between the main app and the NSE, which means the lastNotificationId is out of sync, so messages are delivered/displayed twice. This is bug https://github.com/binwiederhier/ntfy/issues/316.

This causes another bug too, where the UI does not reflect all messages all the time (this is: https://github.com/binwiederhier/ntfy/issues/337). It is annoying and it just shows that I don't know how Core Data works internally. It could also be a bug, but it's annoying.

Again, this is just a guess.

Debugging this is next to impossible, because

  • (a) You'll have to compile and run the iOS app yourself and hook it up to Firebase and APNS. It doesn't work in a Simulator because Firebase doesn't work in a simulator.
  • (b) Because the NSE is a separate process, the logs for it do not show up in the log console of Xcode. You have to manually attach a debugger and such. It is surpremely annoying.

Before you ask "what the heck": 90% of this is Apple's fault, of how the NSE and all that work. The problem with Core Data is obviously my fault, but I have no time currently to debug this or spend any time with it.

binwiederhier avatar Jul 27 '22 23:07 binwiederhier

Fyi, arriving shortly after another within a few seconds (i think it was less than ~ 10 seconds.) Can not reproduce again right after this screenshot...

IMG_1438

notDavid avatar Nov 09 '22 14:11 notDavid

Seeing the same issue. Self hosted install. Notifications always seem to be 7 minutes apart.

  • I do use DND but pretty sure it's not always on when I receive the duplicate notifications, but mostly happens at night
  • Checked my script logs to make sure it only executed once around that time
  • Only one notification shows within the iOS app and web UI at the time of the original run (not the 7 minutes later)
  • upstream-base-url: "https://ntfy.sh" is set for instant notifications

Thanks for everything

vodium-vastro avatar Jan 30 '23 07:01 vodium-vastro

I do believe this is fixed by #677/#509 -- If it still happens, please comment here and I'll re-open.

binwiederhier avatar Mar 31 '23 01:03 binwiederhier