I try to program an application for Apple IOS. I use a .NET MAUI Blazor App for this project, because our whole development is in C#. What I want to do: I want to receive messages from a MQTT Server. Therefore I use MQTTnet and subscribe the topics I need. If a message is received I want to send a local push notification with some information. My problem is: I need an service, task, thread or something like this. To listen to the topics from the MQTT Server which is always active!
I tried: Background Task from UIApplication Events like DidEnterBackground and Foreground etc.
Solution: As @paulw11 said, it is not possible to run a background service for a long time. We have to send the Notifications via APN Servers. You can Prepare a Worker Service which is subscribing every topic and with the NuGet Package "PushNotifications.Server.AspNetCore" you can prepare a Message, but you need the permission of the user to end them messages and a token from the device. If he has Given the permission, you can get the token from the following code in the AppDelegate.cs
public virtual void RegisteredForRemoteNotifications(UIKit.UIApplication application, NSData deviceToken)
{
byte[] result = new byte[deviceToken.Length];
Marshal.Copy(deviceToken.Bytes, result, 0, (int)deviceToken.Length);
var token = BitConverter.ToString(result).Replace("-", "");
}
Again Thanks to @paulw11