Xamarin C#中IOS应用程序上的FCM后台通知不工作

Xamarin C#中IOS应用程序上的FCM后台通知不工作,c#,ios,xamarin,notifications,firebase-cloud-messaging,C#,Ios,Xamarin,Notifications,Firebase Cloud Messaging,该团队正在c#中的Xamarin上开发IOS应用程序。现在我们想使用fcm的推送通知服务。已尝试部署应用程序,但问题是:如果应用程序位于后台,则不会在ios上收到通知。对其进行了一些研究,但发现该应用程序在进入后台时会与fcm断开连接。虽然通过不调用函数尝试不断开连接,但仍然没有收到通知。只是想知道当应用程序在后台时,是否可以在ios上接收通知。 共享相关链接以及后台代码,当应用程序进入后台时,该代码会断开应用程序与fcm的连接。也删除了函数调用,但它不起作用 public override v

该团队正在c#中的Xamarin上开发IOS应用程序。现在我们想使用fcm的推送通知服务。已尝试部署应用程序,但问题是:如果应用程序位于后台,则不会在ios上收到通知。对其进行了一些研究,但发现该应用程序在进入后台时会与fcm断开连接。虽然通过不调用函数尝试不断开连接,但仍然没有收到通知。只是想知道当应用程序在后台时,是否可以在ios上接收通知。 共享相关链接以及后台代码,当应用程序进入后台时,该代码会断开应用程序与fcm的连接。也删除了函数调用,但它不起作用

public override void DidEnterBackground (UIApplication application)
{
    // Use this method to release shared resources, save user data, 
    //invalidate timers and store the application state.
    // If your application supports background exection this method is 
    //called instead of WillTerminate when the user quits.
    Messaging.SharedInstance.Disconnect ();
    Console.WriteLine (“Disconnected from FCM”);
}
链接:

接下来将其放入AppDelegate.cs中

[Export("messaging:didReceiveRegistrationToken:")]
    public void DidReceiveRegistrationToken(Messaging messaging, string fcmToken)
    {
        // Monitor token generation: To be notified whenever the token is updated.

        LogInformation(nameof(DidReceiveRegistrationToken), $"Firebase registration token: {fcmToken}");

        // TODO: If necessary send token to application server.
        // Note: This callback is fired at each app startup and whenever a new token is generated.
    }

    // You'll need this method if you set "FirebaseAppDelegateProxyEnabled": NO in GoogleService-Info.plist
    //public override void RegisteredForRemoteNotifications (UIApplication application, NSData deviceToken)
    //{
    //  Messaging.SharedInstance.ApnsToken = deviceToken;
    //}

    public override void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult> completionHandler)
    {
        // Handle Notification messages in the background and foreground.
        // Handle Data messages for iOS 9 and below.
        // If you are receiving a notification message while your app is in the background,
        // this callback will not be fired till the user taps on the notification launching the application.
        // TODO: Handle data of notification
        // With swizzling disabled you must let Messaging know about the message, for Analytics
        //Messaging.SharedInstance.AppDidReceiveMessage (userInfo);

        if(ConnectionClass.CompanyID != null)
        {
            SyncData.SyncDataDB();
        }
        //FirebasePushNotificationManager.CurrentNotificationPresentationOption = UNNotificationPresentationOptions.Alert;
        FirebasePushNotificationManager.DidReceiveMessage(userInfo);
        //HandleMessage(userInfo);

        // Print full message.
        //LogInformation(nameof(DidReceiveRemoteNotification), userInfo);

        //completionHandler(UIBackgroundFetchResult.NewData);
    }

    [Export("messaging:didReceiveMessage:")]
    public void DidReceiveMessage(Messaging messaging, RemoteMessage remoteMessage)
    {
        // Handle Data messages for iOS 10 and above.
        HandleMessage(remoteMessage.AppData);

        LogInformation(nameof(DidReceiveMessage), remoteMessage.AppData);
    }

    void HandleMessage(NSDictionary message)
    {
        if (MessageReceived == null)
            return;

        MessageType messageType;
        if (message.ContainsKey(new NSString("aps")))
            messageType = MessageType.Notification;
        else
            messageType = MessageType.Data;

        var e = new UserInfoEventArgs(message, messageType);
        MessageReceived(this, e);
    }

    public static void ShowMessage(string title, string message, UIViewController fromViewController, Action actionForOk = null)
    {
        if (UIDevice.CurrentDevice.CheckSystemVersion(8, 0))
        {
            var alert = UIAlertController.Create(title, message, UIAlertControllerStyle.Alert);
            alert.AddAction(UIAlertAction.Create("Ok", UIAlertActionStyle.Default, (obj) => actionForOk?.Invoke()));
            fromViewController.PresentViewController(alert, true, null);
        }
        else
        {
            var alert = new UIAlertView(title, message, null, "Ok", null);
            alert.Clicked += (sender, e) => actionForOk?.Invoke();
            alert.Show();
        }
    }

    void LogInformation(string methodName, object information) => Console.WriteLine($"\nMethod name: {methodName}\nInformation: {information}");
[导出(“消息传递:didReceiveRegistrationToken:”)]
public void DidReceiveRegistrationToken(消息传递,字符串fcmToken)
{
//监视令牌生成:在令牌更新时通知。
登录信息(姓名(DidReceiverRegistrationToken),$“Firebase注册令牌:{fcmToken}”);
//TODO:如有必要,将令牌发送到应用程序服务器。
//注意:每次应用程序启动时以及每当生成新令牌时,都会触发此回调。
}
//如果在GoogleService-Info.plist中设置“FirebaseAppDelegateProxyEnabled”:否,则需要此方法
//公共覆盖无效注册更正(UIApplication应用程序,NSData deviceToken)
//{
//Messaging.SharedInstance.ApnsToken=deviceToken;
//}
public override void DidReceiveEmotentification(UIApplication应用程序、NSDictionary userInfo、Action completionHandler)
{
//在后台和前台处理通知消息。
//处理iOS 9及以下版本的数据消息。
//如果您在应用程序处于后台时收到通知消息,
//在用户点击启动应用程序的通知之前,不会触发此回调。
//TODO:处理通知的数据
//禁用swizzling后,您必须让消息传递了解消息,以便进行分析
//Messaging.SharedInstance.AppDidReceiveMessage(用户信息);
if(ConnectionClass.CompanyID!=null)
{
SyncData.SyncDataDB();
}
//FirebasePushNotificationManager.CurrentNotificationPresentationOption=UNNotificationPresentationOptions.Alert;
FirebasePushNotificationManager.DidReceiveMessage(用户信息);
//HandleMessage(用户信息);
//打印完整消息。
//登录信息(姓名(DidReceiveMemoteNotification)、用户信息);
//completionHandler(UIBackgroundFetchResult.NewData);
}
[导出(“消息传递:didReceiveMessage:”)]
public void DidReceiveMessage(消息传递、远程消息、远程消息)
{
//处理iOS 10及以上版本的数据消息。
HandleMessage(remoteMessage.AppData);
登录信息(名称(DidReceiveMessage)、remoteMessage.AppData);
}
无效HandleMessage(NSDictionary消息)
{
if(MessageReceived==null)
返回;
MessageType消息类型;
if(message.ContainsKey(新的NSString(“aps”))
messageType=messageType.Notification;
其他的
messageType=messageType.Data;
var e=新的UserInfoEventArgs(消息,消息类型);
MessageReceived(本,e);
}
公共静态void ShowMessage(字符串标题、字符串消息、UIViewController fromViewController、Action actionForOk=null)
{
if(UIDevice.CurrentDevice.CheckSystemVersion(8,0))
{
var alert=UIAlertController.Create(标题、消息、UIAlertControllerStyle.alert);
AddAction(UIAlertAction.Create(“确定”,UIAlertActionStyle.Default,(obj)=>actionForOk?.Invoke());
fromViewController.PresentViewController(警报、真、空);
}
其他的
{
var alert=新UIAlertView(标题、消息、空、“确定”、空);
alert.Clicked+=(发件人,e)=>actionForOk?.Invoke();
alert.Show();
}
}
void LogInformation(string methodName,object information)=>Console.WriteLine($“\n方法名称:{methodName}\n信息:{information}”);
[Export("messaging:didReceiveRegistrationToken:")]
    public void DidReceiveRegistrationToken(Messaging messaging, string fcmToken)
    {
        // Monitor token generation: To be notified whenever the token is updated.

        LogInformation(nameof(DidReceiveRegistrationToken), $"Firebase registration token: {fcmToken}");

        // TODO: If necessary send token to application server.
        // Note: This callback is fired at each app startup and whenever a new token is generated.
    }

    // You'll need this method if you set "FirebaseAppDelegateProxyEnabled": NO in GoogleService-Info.plist
    //public override void RegisteredForRemoteNotifications (UIApplication application, NSData deviceToken)
    //{
    //  Messaging.SharedInstance.ApnsToken = deviceToken;
    //}

    public override void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult> completionHandler)
    {
        // Handle Notification messages in the background and foreground.
        // Handle Data messages for iOS 9 and below.
        // If you are receiving a notification message while your app is in the background,
        // this callback will not be fired till the user taps on the notification launching the application.
        // TODO: Handle data of notification
        // With swizzling disabled you must let Messaging know about the message, for Analytics
        //Messaging.SharedInstance.AppDidReceiveMessage (userInfo);

        if(ConnectionClass.CompanyID != null)
        {
            SyncData.SyncDataDB();
        }
        //FirebasePushNotificationManager.CurrentNotificationPresentationOption = UNNotificationPresentationOptions.Alert;
        FirebasePushNotificationManager.DidReceiveMessage(userInfo);
        //HandleMessage(userInfo);

        // Print full message.
        //LogInformation(nameof(DidReceiveRemoteNotification), userInfo);

        //completionHandler(UIBackgroundFetchResult.NewData);
    }

    [Export("messaging:didReceiveMessage:")]
    public void DidReceiveMessage(Messaging messaging, RemoteMessage remoteMessage)
    {
        // Handle Data messages for iOS 10 and above.
        HandleMessage(remoteMessage.AppData);

        LogInformation(nameof(DidReceiveMessage), remoteMessage.AppData);
    }

    void HandleMessage(NSDictionary message)
    {
        if (MessageReceived == null)
            return;

        MessageType messageType;
        if (message.ContainsKey(new NSString("aps")))
            messageType = MessageType.Notification;
        else
            messageType = MessageType.Data;

        var e = new UserInfoEventArgs(message, messageType);
        MessageReceived(this, e);
    }

    public static void ShowMessage(string title, string message, UIViewController fromViewController, Action actionForOk = null)
    {
        if (UIDevice.CurrentDevice.CheckSystemVersion(8, 0))
        {
            var alert = UIAlertController.Create(title, message, UIAlertControllerStyle.Alert);
            alert.AddAction(UIAlertAction.Create("Ok", UIAlertActionStyle.Default, (obj) => actionForOk?.Invoke()));
            fromViewController.PresentViewController(alert, true, null);
        }
        else
        {
            var alert = new UIAlertView(title, message, null, "Ok", null);
            alert.Clicked += (sender, e) => actionForOk?.Invoke();
            alert.Show();
        }
    }

    void LogInformation(string methodName, object information) => Console.WriteLine($"\nMethod name: {methodName}\nInformation: {information}");