title: Xamarin Firebase for iOS giving fcm 501 error
body: <p>I have been struggling with this for several days and have found many other posts on the subject, but nothing seems to fix the problem I have. I have a feeling this might be simple as I have tried this several times with cut down projects and always get the same failure. I can receive Firebase messages fine in Android, but iOS consistently defeats me.</p>
  
  <p>I am using a phone with iOS 10 as a test. The debug code always gets a token back, but on connecting, an error 'The operation couldn’t be completed. (com.google.fcm error 501.)' is returned. I attach some sample code - this has been bodged around in frustration</p>
  
  <pre><code>    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)
      {
          global::Xamarin.Forms.Forms.Init ();
          LoadApplication (new FirebaseTest.App (""));
  
          RegisterForNotificationFCM();
          return base.FinishedLaunching (app, options);
      }
      void RegisterForNotificationFCM()
      {
          // Register your app for remote notifications.
          if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0))
          {
              // iOS 10 or later
              var authOptions = UNAuthorizationOptions.Alert | UNAuthorizationOptions.Badge | UNAuthorizationOptions.Sound;
              UNUserNotificationCenter.Current.RequestAuthorization(authOptions, (granted, error) =&gt; {
                  Console.WriteLine(granted);
              });
  
              // For iOS 10 display notification (sent via APNS)
              UNUserNotificationCenter.Current.Delegate = this;
  
              // For iOS 10 data message (sent via FCM)
              Messaging.SharedInstance.Delegate = this;
          }
          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();
  
          Firebase.Analytics.Loader loader1 = new Firebase.Analytics.Loader();
          Firebase.InstanceID.Loader loader2 = new Firebase.InstanceID.Loader();
  
          //Firebase.Core.App.Configure();
  
          Firebase.InstanceID.InstanceId.Notifications.ObserveTokenRefresh((sender, e) =&gt;
          {
              var newToken = Firebase.InstanceID.InstanceId.SharedInstance.Token;
              System.Diagnostics.Debug.WriteLine(newToken);
  
              connectFCM();
          });
      }
      public override void DidEnterBackground(UIApplication uiApplication)
      {
          Messaging.SharedInstance.ShouldEstablishDirectChannel = false;
      }
  
      public override void OnActivated(UIApplication uiApplication)
      {
          base.OnActivated(uiApplication);
      }
  
      public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
      {
          Messaging.SharedInstance.ApnsToken = deviceToken;
          connectFCM();
          //Firebase.InstanceID.InstanceId.SharedInstance.SetApnsToken(deviceToken, Firebase.InstanceID.ApnsTokenType.Prod);
      }
  
      public void DidRefreshRegistrationToken(Messaging msg, string str)
      {
      }
  
      //Fire when background received notification is clicked
      public override void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action&lt;UIBackgroundFetchResult&gt; completionHandler)
      {
          //Messaging.SharedInstance.AppDidReceiveMessage(userInfo);
          System.Diagnostics.Debug.WriteLine(userInfo);
  
          // Generate custom event
          NSString[] keys = { new NSString("Event_type") };
          NSObject[] values = { new NSString("Recieve_Notification") };
          var parameters = NSDictionary&lt;NSString, NSObject&gt;.FromObjectsAndKeys(keys, values, keys.Length);
  
          // Send custom event
          Firebase.Analytics.Analytics.LogEvent("CustomEvent", parameters);
  
          if (application.ApplicationState == UIApplicationState.Active)
          {
              System.Diagnostics.Debug.WriteLine(userInfo);
              var aps_d = userInfo["aps"] as NSDictionary;
              var alert_d = aps_d["alert"] as NSDictionary;
              var body = alert_d["body"] as NSString;
              var title = alert_d["title"] as NSString;
              //debugAlert(title, body);
          }
      }
  
      [Export("userNotificationCenter:willPresentNotification:withCompletionHandler:")]
      public void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action&lt;UNNotificationPresentationOptions&gt; completionHandler)
      {
          // Do your magic to handle the notification data
          System.Console.WriteLine(notification.Request.Content.UserInfo);
      }
  
      // Receive data message on iOS 10 devices.
      public void ApplicationReceivedRemoteMessage(RemoteMessage remoteMessage)
      {
          Console.WriteLine(remoteMessage.AppData);
      }
  
      private void connectFCM()
      {
          //Messaging.SharedInstance.ShouldEstablishDirectChannel = true;
          //Messaging.SharedInstance.Subscribe("/topics/topic");
          Messaging.SharedInstance.Connect((error) =&gt;
          {
              if (error == null)
              {
                  Messaging.SharedInstance.Subscribe("/topics/topic");
              }
              System.Diagnostics.Debug.WriteLine(error != null ? "error occured" : "connect success");
          });
      }
  </code></pre>
  
  <p>These functions have been removed from many other posts on the same subject as happens when you get more and more desperate. I think the firebase console has been set up okay. I have added the iOS as an app along side the working Android one and have added an authentication key created on the Mac. (surprisingly I still get a token back even without this iOS app as part of the firebase project - not what I expected) A certificate has been created for this app and the provisioning profile has been recreated and old ones deleted.</p>
  
  <p>I'm not sure what else to add - someone please put me out of my misery...</p>