question

Stesvis-5434 avatar image
0 Votes"
Stesvis-5434 asked TheSloth-9098 commented

Receiving chat messages with SignalR

I tested the SignalR solution in Xamarin Forms and .net core backend and it works great:
https://montemagno.com/real-time-communication-for-mobile-with-signalr/

However, I am wondering how to keep receiving the messages when the app is in background mode, as this solution (in the link) only works when you have the app open on the chat page....
Do I need to implement my own push notifications? If that is the case it defeats the purpose of SignalR so I hope there is another way that I haven't found yet :)

Thanks

dotnet-xamarindotnet-entity-framework-core
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

In iOS it is not possible to keep the signalr connection alive while in the background. From what I've read (never tried it) if you use continuous tracking in the app, iOS will allow signalr to stay alive so it can send tracking data to the backend server.

With Android you can create a foreground service. This will allow signalr to stay alive in the background. So it's only iOS you will have a problem with in the background.

You will need to handle signalr hub connections in OnSleep and OnResume. Depending on how you handle the connection, when your clients come out of the background, they may create a new connection, it could time out, or it could deliver all the backlogged messages to the client at one time.

0 Votes 0 ·
mouadcherkaoui avatar image
0 Votes"
mouadcherkaoui answered mouadcherkaoui commented

Hi Stesvis-5434,

in fact it is possible to receive messages in background mode, all you need is whether a Background Worker for UWP apps :
https://docs.microsoft.com/en-us/dotnet/api/system.componentmodel.backgroundworker?view=net-5.0

or another example is Service Workers in PWA :
https://docs.microsoft.com/en-us/microsoft-edge/progressive-web-apps-chromium/serviceworker

it depend more on the platform you are using, for example here is an article about creating an Android service using xamarin:

https://docs.microsoft.com/en-us/xamarin/android/app-fundamentals/services/creating-a-service/

Hope it helps!

Best Regards.

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks for your reply!

This is for iOS and Android.
The problem is that, i think that in iOS you can't run background services anymore?

0 Votes 0 ·

Okey, I see it is more related to the iOS platform features, but for sure there is a way to deal with this constraint for example I saw in the documentation a topic about backgrounding:

https://docs.microsoft.com/en-us/xamarin/ios/app-fundamentals/backgrounding/ios-backgrounding-techniques/

0 Votes 0 ·
Angelru-4290 avatar image
1 Vote"
Angelru-4290 answered Angelru-4290 edited

There is a solution to address this problem:

Since signalR disconnects when the app goes to the background and some time passes. The OnResume and OnSleep events are where you have to do all the magic. When it goes to OnSleep it disconnects SignalR and when you return to OnResume you reconnect it and check the new messages.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.