Xamarin.Forms IOS在IOS物理设备上未显示FCM推送通知

问题描述

1。我在firebase控制台中创建了项目。

2。在Apple帐户上创建用于推送通知的证书

3。添加了GoogleService-info.plist,作为BundleResource

  1. 允许在Entitlements.plist中使用推送通知

  2. 允许在Info.plist中启用Backgroud模式和远程通知

这是我在AppDelegate中的代码

  [Register("AppDelegate")]
    public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate,IUNUserNotificationCenterDelegate,IMessagingDelegate
    {
        //
        // This method is invoked when the application has loaded and is ready to run. In this 
        // method you should instantiate the window,load the UI into it and then make the window
        // visible.
        //
        // You have 17 seconds to return from this method,or iOS will terminate your application.
        //
        public override bool FinishedLaunching(UIApplication app,NSDictionary options)
        {
            // We have checked to see if the device is running iOS 8,if so we are required to ask for the user's permission to receive notifications    

            // check for a notification    
            if (UIDevice.CurrentDevice.CheckSystemVersion(8,0))
            {
                var notificationSettings = UIUserNotificationSettings.GetSettingsForTypes(
                    UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.sound,null
                );

                app.RegisterUserNotificationSettings(notificationSettings);
            }
           if (options != null){    
                if (options.ContainsKey(UIApplication.LaunchOptionslocalnotificationKey)){    
                    UIlocalnotification localnotification = options[UIApplication.LaunchOptionslocalnotificationKey] as UIlocalnotification;    
                    if (localnotification != null){
                        var okCancelAlertController = UIAlertController.Create(localnotification.AlertAction,localnotification.AlertBody,UIAlertControllerStyle.Alert);
                        okCancelAlertController.AddAction(UIAlertAction.Create("OK",UIAlertActionStyle.Default,alert => Console.WriteLine("Okay was clicked")));
                        // reset our badge    
                        UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(okCancelAlertController,true,null);
                        UIApplication.SharedApplication.ApplicationIconBadgeNumber = 0;    
                    }    
                }    
            }
            UINavigationBar.Appearance.BarTintColor = Color.FromHex("07987f").ToUIColor();
            UINavigationBar.Appearance.TintColor = Color.White.ToUIColor();
            UINavigationBar.Appearance.TitleTextAttributes = new UIStringAttributes { ForegroundColor = UIColor.White };
            UIApplication.SharedApplication.SetStatusBarStyle(UIStatusBarStyle.LightContent,false);
            Forms.SetFlags("swipeview_Experimental");
            global::Xamarin.Forms.Forms.Init();
            Xamarin.FormsMaps.Init();
            Firebase.Core.App.Configure();
            Messaging.SharedInstance.Delegate = this;

            LoadApplication(new App());
            // 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,new NSSet());
                UIApplication.SharedApplication.RegisterUserNotificationSettings(settings);
                UIApplication.SharedApplication.RegisterForRemoteNotifications();
            }
            var token = Messaging.SharedInstance.FcmToken ?? "";
            Console.WriteLine($"FCM token: {token}");
            UIApplication.SharedApplication.RegisterForRemoteNotifications();
            return base.FinishedLaunching(app,options);
        }
        //Receive Local Notifications
        public override void Receivedlocalnotification(UIApplication application,UIlocalnotification notification)
        {

            UIAlertController okayAlertController = UIAlertController.Create(notification.AlertAction,notification.AlertBody,UIAlertControllerStyle.Alert);
            okayAlertController.AddAction(UIAlertAction.Create("OK",null));
            UIApplication.SharedApplication.ApplicationIconBadgeNumber = 0;
        }
        public override void OnActivated(UIApplication uiApplication)
        {
 
            if (UIDevice.CurrentDevice.CheckSystemVersion(13,0))
            {
                // If VS has updated to the latest version,you can use StatusBarManager,else use the first line code
                // UIView statusBar = new UIView(UIApplication.SharedApplication.StatusBarFrame);
                UIView statusBar = new UIView(UIApplication.SharedApplication.KeyWindow.WindowScene.StatusBarManager.StatusBarFrame);
                statusBar.BackgroundColor = Color.FromHex("07987f").ToUIColor(); 
                statusBar.TintColor= Color.FromHex("07987f").ToUIColor();    
                
                UIApplication.SharedApplication.KeyWindow.AddSubview(statusBar);
            }
            else
            {
                UIView statusBar = UIApplication.SharedApplication.ValueForKey(new Nsstring("statusBar")) as UIView;
                if (statusBar.RespondsToSelector(new ObjCRuntime.Selector("setBackgroundColor:")))
                {
                    statusBar.BackgroundColor = Color.FromHex("07987f").ToUIColor(); 
                    statusBar.TintColor= Color.FromHex("07987f").ToUIColor();
                }
            }
            base.OnActivated(uiApplication);
        }
        [Export("messaging:didReceiveRegistrationToken:")]
        public void DidReceiveRegistrationToken(Messaging messaging,string fcmToken)
        {
            Console.WriteLine($"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.
        }
        public override void RegisteredForRemoteNotifications(UIApplication application,NSData devicetoken)
        {

            Messaging.SharedInstance.ApnsToken = devicetoken;
        }
        public override void ReceivedRemoteNotification(UIApplication application,NSDictionary userInfo)
        {
            // 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);

            // Print full message.
            Console.WriteLine(userInfo);
        }
        public override void DidReceiveRemoteNotification(UIApplication application,NSDictionary userInfo,Action<UIBackgroundFetchResult> completionHandler)
        {
            // If you are receiving a notification message while your app is in the background,for Analytics
            //Messaging.SharedInstance.AppDidReceiveMessage (userInfo);

            // Print full message.
            Console.WriteLine(userInfo);

            completionHandler(UIBackgroundFetchResult.NewData);
        }
        // Receive displayed notifications for iOS 10 devices.
        // Handle incoming notification messages while app is in the foreground.
        [Export("userNotificationCenter:willPresentNotification:withCompletionHandler:")]
        public void WillPresentNotification(UNUserNotificationCenter center,UNNotification notification,Action<UNNotificationPresentationoptions> completionHandler)
        {
            var userInfo = notification.Request.Content.UserInfo;

            // With swizzling disabled you must let Messaging kNow about the message,for Analytics
            //Messaging.SharedInstance.AppDidReceiveMessage (userInfo);

            // Print full message.
            Console.WriteLine(userInfo);

            // Change this to your preferred presentation option
            completionHandler(UNNotificationPresentationoptions.Alert);
        }
        // Handle notification messages after display notification is tapped by the user.
        [Export("userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:")]
        public void DidReceiveNotificationResponse(UNUserNotificationCenter center,UNNotificationResponse response,Action completionHandler)
        {
            var userInfo = response.Notification.Request.Content.UserInfo;

            // Print full message.
            Console.WriteLine(userInfo);

            completionHandler();
        }
    }

坚持这一周。我不知道接下来要做什么。有什么建议吗?我从应用程序复制了fcmToken,然后将其粘贴到Firebase控制台中以获取测试消息。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)