deltachat-ios icon indicating copy to clipboard operation
deltachat-ios copied to clipboard

add Notification Service Extension

Open r10s opened this issue 1 year ago • 1 comments

this PR adds a "Notification Service Extension" that is called for every push notification received, allowing to modify the notification content, which is (localized) "You have new messages" otherwise.

as we do not know anything about the message the push notification belongs to (nor does apple or others then), we do a background-fetch over all accounts and change the notification accordingly, concretely setting title to chatname and title to message text beginning.

this also filters out reactions or other messages that do not come with DC_EVENT_INCOMING_MSG

if we get more than one message via IMAP (because of races or additional accounts), we use texts as "N messages" or "N messages in M chats".

because of that and because of muting, it may happen, that we do not want to display a notification at all. by default this is not possible (we can only downtune via sound, interruptionLevel, relevanceScore etc.), but there is a special entitlement that makes not displaying possible

nb: the "Notification Service Extension" is an own process (similar to DcShare), that is started independently of the main deltachat-ios app (which may or may not run at the same time). group-id makes it possible that the same data are accessible, however.

bigger todo parts:

  • [ ] make sure, NSE-fetch and main-app-io do not run at the same time (otherwise last-seen etc. gets issues, also double messages and double notificarions) EDIT: there is an attempt at https://github.com/deltachat/deltachat-ios/pull/2107, but it solves the issue not optimal
  • [x] set real message content
  • [x] handle mute
  • [x] set badge counter accordingly
  • [x] if a notification spans more than one chat, add all of them to the title (iOS will truncate and the summary be sth as "N messages in M chats")
  • [x] switch accounts as needed when tapping notifications
  • [x] for notifications referring exactly to one chat, open the chat instead of the chatlist (requires using eg. userInfo instead of notificationIdentifier)
  • [x] remove PUSH notifications when the chat is opened; for unspecific notifications ("N messages"), remove them when the chatlist is opened
  • [x] make localisation work, probably just some string files/references missing in the extension
  • [x] force update of chatlist similar to DcShare (needed in case the app is suspended to ram)
  • [x] stop showing images in notifications: the current implementation is slow and would need some more love - but the effort is questionable as showing images unexpectedly is also easily compromising. better just show a text "Image" as on android (in general "📷" instead of "Image" would be cool, btw)
  • [x] sync threading and other data with normal notifications

finally:

  • see what happens with entitlement - however, this is no blocker. even without entitlement, this PR is an improvement as unwanted messages do no longer sound nor turn on display. and, of course, wanted notifications get the correct text

before (showing generic "You have new messages" in german) / after (showing real message content):

successor of https://github.com/deltachat/deltachat-ios/pull/2080

r10s avatar Mar 06 '24 13:03 r10s

future question: can we also send/trigger local notifications from this function/extension?

Simon-Laux avatar Mar 06 '24 16:03 Simon-Laux

future question: can we also send/trigger local notifications from this function/extension?

if so we could send a notification for each message that was received, but maybe that would spam too much. so the current way of telling about "you have multiple new messages" is completely fine, maybe even better 👀

Simon-Laux avatar Mar 21 '24 11:03 Simon-Laux

i did a small test at some point, one can get a pointer to UNUserNotificationCenter from NSE, however, basic functionality as removing notifications did not work for me (otherwise one would not need an entitlement :)

therefore, i did not investigate further, even if some hacks may work on some OS, this seems not to be the gist of the NSE, and there may be dragons now or in the future :)

r10s avatar Mar 21 '24 11:03 r10s