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.