簡體   English   中英

Xamarin C#中IOS應用上的FCM背景通知不起作用

[英]FCM Background Notification on IOS app in Xamarin C# not working

團隊正在使用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”);
}

鏈接: https//components.xamarin.com/gettingstarted/firebaseioscloudmessaging/true

public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
    {
        App.Configure ();

        // Register your app for remote notifications.
        if (UIDevice.CurrentDevice.CheckSystemVersion (10, 0)) {
            // For iOS 10 display notification (sent via APNS)
            UNUserNotificationCenter.Current.Delegate = this;

            var authOptions = UNAuthorizationOptions.Alert | UNAuthorizationOptions.Badge | UNAuthorizationOptions.Sound;
            UNUserNotificationCenter.Current.RequestAuthorization (authOptions, (granted, error) => {
                Console.WriteLine (granted);
            });
        } else {
            // iOS 9 or before
            var allNotificationTypes = UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound;
            var settings = UIUserNotificationSettings.GetSettingsForTypes (allNotificationTypes, null);
            UIApplication.SharedApplication.RegisterUserNotificationSettings (settings);
        }

        UIApplication.SharedApplication.RegisterForRemoteNotifications ();

        Messaging.SharedInstance.Delegate = this;

        // To connect with FCM. FCM manages the connection, closing it
        // when your app goes into the background and reopening it 
        // whenever the app is foregrounded.
        Messaging.SharedInstance.ShouldEstablishDirectChannel = true;
        return true;
    }

接下來將其放在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}");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM