Azure notification hub best practice in social net

2019-06-13 18:55发布

问题:

I am wondering which is the best practice in Azure notification hub to send push notifications to a couple of certain users (e.g. about followed activities).

Imagine, users can follow other users and these followers should be notified about new activities (facebook, twitter principle).

How to send these notifications in detail, there are two options I am thinking about, but which one is the best?

1) looping through all desired users by code and calling SendTemplateNotificationAsync for each individual user?

foreach(user in allSubscribedUsers) {
    tags.Add("userid:" + userId);
    await hub.SendTemplateNotificationAsync(notification, tags);
}

2) Having special tags a user can register to

registration.Tags = new HashSet<string>(deviceUpdate.Tags);

// the current user follows other users with the ids 1,2 and 3
registration.Tags.Add("followerUserid:1");
registration.Tags.Add("followerUserid:2");
registration.Tags.Add("followerUserid:3");
...
// an acivity by user with id 2 is happen
// send a push notification to all users who follow the user with id 2
tags.Add("followerUserid:2");
await hub.SendTemplateNotificationAsync(notification, tags);

It's possible that a user follows up to 1.000 other users, so he needs to register 1.000 of the followerUserId-Tag.

Is the second approach a reliable solution?

Are there any tag limitations a user can register to?

回答1:

This answer describes tag limits on NH. There is a limit of 60 tags per registration, so you won't be able to use your second approach if you want to allow 1k followers.

You can use the SendNotificationAsync(Notification, IEnumerable<String>) overload of the method to "batch" calls in your first scenario in groups of 20 max to save on the number of calls you make to the service.