0

Using the Notifications REST API and JavaScript, we are subscribing our Progressive Web App through FCM and then calling the registrations endpoint to register on our ANH

The registration completes fine and we can see the registration on our hub with the correct platform and a populated PNS Identifier

When we attempt to send a test message to all registered devices, we get the following error in ANH

The token obtained from the token provider is wrong

We have tried sending the entire endpoint object returned by Firebase, just the subscriptionId and various other combinations

Does the error message mean that we have subscribed using the wrong key pair or is the format of the token incorrect? There is nowhere that shows an example of what the GcmRegistrationId format should be when registering with the registrations endpoint

Mike
  • 2,391
  • 6
  • 33
  • 72
  • Have you tried checking out these similar threads? [39694294](https://stackoverflow.com/questions/39694294/azure-send-test-message-to-android-with-fcm-results-in-the-token-obtained-from), [37011339](https://stackoverflow.com/questions/37011339/notificationhub-push-notification-returns-the-token-obtained-from-the-token-pr) – Jacque Jan 04 '19 at 05:44

1 Answers1

0

You can use below approach to register your Devices:

In case of GCM follow this approach:

use Nuget package for Notification Hubs.

For DeviceRegistration.cs

 public class DeviceRegistration
    {
        public string Platform { get; set; } 
        public string Handle { get; set; }
        public string[] Tags { get; set; }
    }

For NotificationClient.cs

 using Microsoft.Azure.NotificationHubs; // Namespace to be used

// Use below method to get registrationID

 public async Task<string> GetRegistrationID(NotificationHubClient Hub, string handle = null)
        {
            string newRegistrationId = null;

            // make sure there are no existing registrations for this push handle (used for iOS and Android)
            if (handle != null)
            {
                var registrations = await Hub.GetRegistrationsByChannelAsync(handle, 100);
                foreach (RegistrationDescription registration in registrations)
                {
                    if (newRegistrationId == null)
                    {
                        newRegistrationId = registration.RegistrationId;
                    }
                    else
                    {
                        await Hub.DeleteRegistrationAsync(registration);
                    }
                }
            }

            if (newRegistrationId == null)
                newRegistrationId = await Hub.CreateRegistrationIdAsync();

            return newRegistrationId;
        }

// Use below method to upsert registration to azure

 public async Task UpsertRegistration(string registrationid, DeviceRegistration deviceUpdate, NotificationHubClient Hub)
            {
    string[] tags = { "abc","def" }; // These are used to send notifications
     DeviceRegistration deviceRegistration = new DeviceRegistration
                    {
                        Handle = newDeviceToken, // Device token given by Firebase
                        Platform = "gcm", // Specify gcm for android and "apns" for ios  
                        Tags = tags
                    };

                RegistrationDescription registration
                         = new GcmRegistrationDescription(deviceRegistration.Handle);



                registration.RegistrationId = registrationid;

                // add check if user is allowed to add these tags
                registration.Tags = new HashSet<string>();
                foreach (string tag in deviceUpdate.Tags)
                {
                    registration.Tags.Add(tag);
                }

               await Hub.CreateOrUpdateRegistrationAsync(registration);           
            }
Raghvender Kataria
  • 1,457
  • 7
  • 14
  • we are still getting the error and we are not using the Nuget package, we are using JavaScript. When you have run your code, what format is your `GcmRegistrationId` in? (the `registration.RegistrationId` ) – Mike Jan 07 '19 at 18:02
  • RegistrationId is obtained from Azure Notification hubs. i have mentioned the method in my answer. Example of registration ID is "7879195269520841161-4412273044718808258-3" – Raghvender Kataria Jan 08 '19 at 09:08
  • I am not talking about the registrationId returned by Azure - I already have that. When we try and send a message, Azure is telling us the token (PNS Handle) is incorrect, so I want to know what the format is of the PNS Handle (GcmRegistrationId) I should send to Azure when calling the registration REST API endpoint – Mike Jan 08 '19 at 09:11
  • 1
    PNS Handle and GCMRegistrationID are different. PNS Handle is actually the mobile device token that you receive from "Google firebase" which is like "eIiZYb98yG8:APA91bFmRozDiXoPkQolbnsFnpGaeRp8TUi_7kcOl2s0W-GavRJW07XDaUPNJlZOScWVsUwLRvxg2fbow45QIE1Pf4-HKBet_vCbdW7zvofkATkDRYrPRcP0AMOd6m2wl7uo1J5WgLD1" . Once you get this from Firebase, then you call azure hub to give you the registrationID for which I have mentioned the method. After getting both, you register your PNS Handle and registrationID into hub by method mentioned above as "UpsertRegistration" – Raghvender Kataria Jan 08 '19 at 09:16
  • @Mike Do you require any further help on this ? – Raghvender Kataria Jan 10 '19 at 03:18