Yaron Levi
Yaron Levi

Reputation: 13077

Azure Notification Hubs - Should I save the device token in my server?

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).

Upvotes: 4

Views: 3383

Answers (1)

efimovandr
efimovandr

Reputation: 1604

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.

Upvotes: 5

Related Questions