Search code examples
iosswiftpush-notificationsilent-notification

Using silent notifications to update screen


In my app I have implemented a chat feature with a socketio backend. All conversations grouped in a view like below:

enter image description here

Is silent notifications the best way to handle the update of this screen?

I also have "alert" notification sent by a nodejs server when I message is sent to a user.

How to handle both of those notifications, if the user allow notifications or not, if the app is in foreground, background etc.

It's a little bit confuse for now so if you have some explanation for this topic to maybe if it's not the right way to do it thank you.


Solution

  • You have no reason for silent notifications for messages as they are. You either send a normal (non-silent) notification or a silent one only depending on what you want when the app is not in foreground.

    When not in foreground silent will be discarded in your case and non-silent will be pressable by user and should navigate to appropriate screen inside the app if pressed.

    When on the screen you have mentioned it might seem best to get notifications and only process those. A notification may already contain all the data needed to display the message but there are 2 facts:

    1. Notifications may be a bit edgy and may not arrive at all. I have personally not experienced that but is what I hear.
    2. Notifications have limited size so may not contain long messages or large data sets like images

    That being said you will need to fetch data manually from server at some point. So when on screen like this one it might be best to simply ignore notifications and try fetching data every few seconds. A smart algorithm would be nice here where if user comes to this screen or gets a notification it resets the fetch frequency to a small one (update every 3 seconds) which then starts dropping if no event occurs to for instance 30 seconds to reduce calls to server.

    You can always do it the other way around. You could use a very low frequency on fetching data manually (1 minute for instance) just to have a fallback if notifications fail. Then fetch data every time a notifications is received. You do have to be sneaky again because if many notifications start coming in you will again have too many calls to the server. So you need to somehow lock your calls to the server or you can send your "preview" data in your notification and show only that. That means instead of full message you will only send a prefix and show that like "Hi, how are y..." or for images "Sent you a photo"...

    The other issue with this (showing only data from notifications) is things may get complicated when your device has insufficient data. Imagine you have 5 friends/conversations and suddenly a notification comes from the 6th which just connected with you. There is no way for you to display this in your view because you have no name of this person, no image, no data at all... So you need to again make a nice architecture so this data is retrieved on demand from the server.