4

I am using Azure Notification Hubs to deliver push notifications to an android/iPhone devices.

My general flow is:

1.Every time the user opens the mobile app, a call is made to the PNS (APNS/GCM) and gets an updated device token.
2. The device token is then sent to the server.
3. The server executes the following code to register the token:

RegistrationDescription reg;

if (deviceType == DeviceType.Iphone)
{
    reg = new AppleRegistrationDescription(deviceToken, new string[] { userId.ToString() });
}
else
{
    reg = new GcmRegistrationDescription(deviceToken, new string[] { userId.ToString() });
}

reg = await hub.CreateRegistrationAsync(reg);

It works great, but my question is, should I be tracking those device tokens in my server for some reason? For example save them in a table for later use or other scenarios I could be facing, or it's intended to use that way(without saving them in a table).

Yaron Levi
  • 12,535
  • 16
  • 69
  • 118

1 Answers1

5

Your code creates a lot of duplicates, by duplicate I mean different registrations with same PNS handle (APNS device token or GCM registration id). NH has de-duplication logic which prevents your devices from receiving multiple copies of same message, but it increases internal storage space and slows system down.

So there is recommendation:

  • On each device your create and store some GUID-like identifier;

  • You pass that identifier on server together with PNS handle;

  • On server you do hub.GetRegistrationByTagAsync(deviceGuid, 100);

  • If registration returned, then update it with received PNS handle (even if PNS handle the same - just to prevent expiration);

  • If result is empty, then you create new registration specifying device GUID as a tag;

Also there is new API which allows you to do only one call and not use any tags if you don't need them. https://msdn.microsoft.com/en-us/magazine/dn948105.aspx Look at topic Case 2: The Back End Manages Devices in Notification Hubs. It is not very good explanation maybe but feature is new. If any questions about that API then I can answer.

efimovandr
  • 1,594
  • 9
  • 11
  • This looks great! I used the AppleRegistrationDescription() code as is, without taking a moment to see that it actually creates a new registration each time. The GUID-like identifier you mentioned is the userId in my case. – Yaron Levi Apr 21 '15 at 15:48
  • Just to clarify: what do you mean under the PNS in case of Apple device? Is that device token? Am I right? – Artyom Pranovich Jan 29 '16 at 08:25
  • PNS is Push Notification System (APNS in case of Apple). PNS handle is identifier issued by PNS for the device (device token in case of Apple). Generic notions confuse people from time to time but we use them because NH works with multiple platforms. – efimovandr Feb 01 '16 at 21:02