How to notify users when the application is closed in xamarin forms (IOS and Android )?

DevQc 61 Reputation points
2022-07-19T17:49:58.68+00:00

I am developing an example app based on notifications , with concepts (websockets SignalR ).
all working fine , but when applications are closed users doesn't receive notifications.
i created a service background and recviedbrodcast
MainActivity :

protected override void OnCreate(Bundle savedInstanceState){  
var intent = new Intent(this, typeof(BackgroundServiceAndroid));  
StartService(intent );  
}  

BackgroundServiceAndroid:

[Service]  
    public class BackgroundServiceAndroid: Service  
    {  
        public override void OnCreate()  
        {  
            base.OnCreate();  
        }  
  
        private async void StartWebSocketService()  
        {  
            await HubClientService.StartWebSocketService();// HubConnectionBuilder ,hubConnection.StartAsync and hubConnection.On<string>("groupName",()) etc.. (working fine , the join group and subscribe ) all users recieve notifications when the app is open  
        }    
  
 public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)  
        {  
            StartWebSocketService();  
            return StartCommandResult.Sticky;  
        }  
  
        public override IBinder OnBind(Intent intent)  
        {  
            return null;  
        }  
  
        public override void OnDestroy()  
        {  
            base.OnDestroy();  
        }  
  

BroadcastReceiver :

[BroadcastReceiver]  
    [IntentFilter(new[] { Intent.ActionBootCompleted })]  
    public class ReceiveBoot : BroadcastReceiver  
    {  
        public override void OnReceive(Context context, Intent intent)  
        {  
  
            /* if ((intent.Action != null) &&  
                 (intent.Action ==  
                     Intent.ActionBootCompleted))  
             {  
                 Intent start = new Intent(context, typeof(BackgroundReceiver));  
                 context.StartService(start);  
             }*/  
  
  
  
            PowerManager pm = (PowerManager)context.GetSystemService(Context.PowerService);  
            PowerManager.WakeLock wakeLock = pm.NewWakeLock(WakeLockFlags.Partial, "BackgroundReceiver");  
            wakeLock.Acquire();  
            wakeLock.Release();  
  
        }  

If my application is running, the notifications appear , else if the app is closed nothing happen.

thanks in advance.

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,296 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,282 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 56,846 Reputation points
    2022-07-19T18:14:11.32+00:00

    background tasks do not support web socket (signal/r). when moved to the background the network connections are disabled. In general the network is only available when the application is in the foreground. if a background task needs to use the network, it needs to remember until the app is in use, then do the network work.

    note: the native O/S notification services is used to send notification to an app when it is not active.