title: Firebase Cloud messaging and Azure Notification Hubs device registration mutual implementation
body: <p>I'm working on a xamarin app where I need to implement notifications. We decided to use azure to manage them, however the one that actually sends the notification is firebase so the devices need to be register in the NotificationHub and in the Firebase service.</p>
  
  <p><strong>I already succeded</strong> doing this directly on the phone, using <strong>WindowsAzure.Messaging</strong> component and <strong>Xamarin - firebase messaging</strong> nuget.</p>
  
  <p>But I read somewhere that it is recommended to do this from the app backend so I'm now trying to implement it like that.</p>
  
  <p>So my <strong>new</strong> "backend" (wich is actually a wcf service) was based on this <a href="https://docs.microsoft.com/en-us/azure/notification-hubs/notification-hubs-aspnet-backend-gcm-android-push-to-user-google-notification" rel="nofollow noreferrer">guide</a> (simplified version of my ws):</p>
  
  <pre><code>public async Task&lt;string&gt; RegistrerCampusHubs(Device device)
  {
      RegistrationDescription registration = null;
      string newRegistrationId = device.Id;
  
      try
      {
          if (device.Regitration.Handle != null)
          {
              var registrations = await hub.GetRegistrationsByChannelAsync(device.Regitration.Handle, 100);
  
              foreach (RegistrationDescription reg in registrations)
              {
                  if (newRegistrationId == null)
                  {
                      newRegistrationId = reg.RegistrationId;
                  }
                  else
                  {
                      await hub.DeleteRegistrationAsync(reg);
                  }
              }
          }
  
          if (newRegistrationId == null)
          {
              newRegistrationId = await hub.CreateRegistrationIdAsync();
          }
  
          switch (device.Regitration.Platform)
          {
              case "mpns":
                  registration = new MpnsRegistrationDescription(device.Regitration.Handle);
                  break;
              case "wns":
                  registration = new WindowsRegistrationDescription(device.Regitration.Handle);
                  break;
              case "apns":
                  registration = new AppleRegistrationDescription(device.Regitration.Handle);
                  break;
              case "gcm":
                  registration = new GcmRegistrationDescription(device.Regitration.Handle);
                  break;
          }
  
          registration.RegistrationId = newRegistrationId;
          registration.Tags = new HashSet&lt;string&gt;(device.Regitration.Tags);
  
          await hub.CreateOrUpdateRegistrationAsync(registration);
  
          return "Success";
      }
      catch (Exception e)
      {
          return "Failed: " + e.Message;
      }
  }
  </code></pre>
  
  <p>and this seems to work as I can see the registered devices on the notification hub, but when I try sending a message I get the following error from the notification hub test sender in visual studio:</p>
  
  <blockquote>
    <p>The Push Notification System handle for the registration is no longer valid</p>
  </blockquote>
  
  <p>I did some tests and I concluded that the problem is the device is not being registered in firebase.</p>
  
  <pre><code>await hub.CreateOrUpdateRegistrationAsync(registration);
  </code></pre>
  
  <p>This line of code registers the device in the NotificationHub but not in firebase. Any ideas on how to fix this problem?</p>