Search code examples
c#push-notificationxamarin.androidfirebase-cloud-messagingazure-notificationhub

Firebase Cloud messaging and Azure Notification Hubs device registration mutual implementation


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.

I already succeeded doing this directly on the phone, using WindowsAzure.Messaging component and Xamarin - firebase messaging nuget.

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.

So my new "backend" (which is actually a wcf service) was based on this guide (simplified version of my ws):

public async Task<string> 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<string>(device.Regitration.Tags);
        
        await hub.CreateOrUpdateRegistrationAsync(registration);

        return "Success";
    }
    catch (Exception e)
    {
        return "Failed: " + e.Message;
    }
}

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:

The Push Notification System handle for the registration is no longer valid

I did some tests and I concluded that the problem is the device is not being registered in firebase.

await hub.CreateOrUpdateRegistrationAsync(registration);

This line of code registers the device in the NotificationHub but not in firebase. Any ideas on how to fix this problem?


Solution

  • According to your description, I followed the code sample from this tutorial for using NotificationHub.Register and Enable Firebase Cloud Messaging to check this issue. I could receive the notification by using the Test Send via Visual Studio Server Explorer and Test Send from Azure Portal.

    The Push Notification System handle for the registration is no longer valid

    Based on your scenario, I assumed that your registrations on Azure Notification Hub may be invalid (e.g. expired). We recommend that you need to re-register whenever your app start-up. Additionally, you could refer to this tutorial for troubleshooting with Azure Notification Hubs. For Service Bus Explorer, you could refer to here.