WNS pushnotification only sending to some tags
Asked Answered
D

0

10

Outline

I am trying to implement WNS for my app game. I currently send a notification upon creation of a new user with the following function which works:

public async void SendNotificationToTag(string tag, string content)
{
    var wnsToast = "<toast><visual><binding template=\"ToastText01\">"
        + "<text id=\"1\">Breaking " +content + "An WNS News!"
        + "</text></binding></visual></toast>";

    WindowsPushMessage wnsMessage = new WindowsPushMessage();
    wnsMessage.XmlPayload = wnsToast;

    await Services.Push.HubClient.SendWindowsNativeNotificationAsync(wnsToast, tag);
    Services.Log.Info("WNS TEST - SendWindowsNativeNotificationAsync - done");
}

I get a notfication to each username, i.e. a personal notificaion. I then update the tags the user listens to, looking in the hub database this also seems to work:

  1. -Usernamecph--gameID1151--gameID1152--gameID1153--gameID1154--gameID1155--gameID1156--gameID1157--gameID1158
  2. -gameID1157--UsernameFyn--gameID1151--gameID1153--gameID1155--gameID1156-

This check to extract the tags from the hub is done using

foreach (Microsoft.ServiceBus.Notifications.RegistrationDescription t in a)
{
    string tempstring = "";
    foreach (string x in t.Tags)
          tempstring += "-" + x + "-";
    Services.Log.Info(tempstring + t.RegistrationId + t.ETag);
}

So far so good.

When I then try to send to one of the other tags than the usernames I do not receive any notification, and I do not receive any error in the log. Am I missing something?

Update - Half Solution

If I use the server Explorer and look at the notification HUB. I can see all the tags, and I can send to them by using the test send. But I cannot seem to do it in other function calls online.

Is it something like the function has to be set as post or get?

YES

So inserting [HttpPost] Seems to enable the push.

However when I look in the Server explorer shown here on the image: Server Explorer Picture

The user seems to be deleted when I update the registrations (There should be three, and there is again when I start the app with the correct tag subscriptions). So Maybe the question is How to Update a registration in the hub correctly

The current update code:

public async void updateRegistration(RegistrationDescription registration, List<string> TAGS)
{
    registration.Tags = new HashSet<string>(TAGS);

    try
    {
        var x = await hub.CreateOrUpdateRegistrationAsync(registration);

        // apiServices.Log.Info(x.ExpirationTime + " " + x.Tags);
    }
    catch (MessagingException e)
    {
        ReturnGoneIfHubResponseIsGone(e);
    }
}

The code that calls the function:

private async Task<bool> RegisterHubTag(User user, string Tag)
{
    List<string> sendTAGs = new List<string>();
    Services.Log.Info("RegisterHubTag Function");
    using (Db db = new Db())
    {
        List<DataObjects.NotificationTag> userTags = db.NotificationTags.Where(t => t.User.UserId == user.UserId).ToList<DataObjects.NotificationTag>();
        if (userTags.Count < 1)
        {
            //Register
            RegisterController.DeviceRegistration Reg = CreateDeviceRegistration(user.PushChannelUri, sendTAGs);
            Microsoft.Azure.NotificationHubs.RegistrationDescription registration = null;
            //Microsoft.ServiceBus.Notifications.RegistrationDescription registration = null;
            Services.Log.Info(Reg.Handle);
            Services.Log.Info(Reg.Platform);
            IEnumerable<string> tagsToRegister;
            List<string> test = new List<string>() { user.Username };
            if(Tag != user.Username)
                test.Add(Tag);
            tagsToRegister = test.AsEnumerable<string>();
            switch (Reg.Platform)
            {
                case "mpns":
                    registration = new MpnsRegistrationDescription(Reg.Handle);
                    break;
                case "wns":
                    registration = new WindowsRegistrationDescription(Reg.Handle);
                    break;
                case "apns":
                    registration = new AppleRegistrationDescription(Reg.Handle);
                    break;
                case "gcm":
                    registration = new GcmRegistrationDescription(Reg.Handle);
                    break;
                default:
                    throw new HttpResponseException(HttpStatusCode.BadRequest);
            }
            var regID = await hub.Post(Services);
            registration.RegistrationId = regID;
            db.NotificationTags.Add(new DataObjects.NotificationTag() { User = user, tag = user.Username, RegistrationID = registration.RegistrationId });

            hub.updateRegistration(registration, test);
            db.SaveChanges();
        }
        else
        {
            RegisterController.DeviceRegistration Reg = CreateDeviceRegistration(user.PushChannelUri, sendTAGs);
            Microsoft.Azure.NotificationHubs.RegistrationDescription registration = null;
            //Microsoft.ServiceBus.Notifications.RegistrationDescription registration = null;
            switch (Reg.Platform)
            {
                case "mpns":
                    registration = new MpnsRegistrationDescription(Reg.Handle);
                    break;
                case "wns":
                    registration = new WindowsRegistrationDescription(Reg.Handle);
                    break;
                case "apns":
                    registration = new AppleRegistrationDescription(Reg.Handle);
                    break;
                case "gcm":
                    registration = new GcmRegistrationDescription(Reg.Handle);
                    break;
                default:
                    throw new HttpResponseException(HttpStatusCode.BadRequest);
            }
            registration.RegistrationId = userTags[0].RegistrationID;
            IEnumerable<string> tagsToRegister;
            List<string> test = new List<string>();
            foreach (DataObjects.NotificationTag t in userTags)
                test.Add(t.tag);
            test.Add(Tag);
            tagsToRegister = test.AsEnumerable<string>();
            hub.updateRegistration(registration, test);
        }
    }
    return true;
}

private RegisterController.DeviceRegistration CreateDeviceRegistration(string channelUri, List<string> tags)
{
    return new RegisterController.DeviceRegistration() { Platform = "wns", Handle = channelUri, Tags = tags.ToArray<string>() };
}

New image I really do not understand it, sometimes I have three registrations, but then in the counter in the bottom is still only saying 2 ? How can this be? (The view is from the server explorer in VS2013) New Version from Server explorer

Dallapiccola answered 21/10, 2015 at 21:21 Comment(15)
If you provide namespace name and date+time you sent message then I'll take a look at logs.Kitts
Do you mean you get notifications if you send to 'Usernamecph' tag in your example, but not when sent to 'gameID1151'? Are you using the same code to associate user name tags and game id tags with respective users?Quincyquindecagon
@NikitaG. Exactly as you sayDallapiccola
@Kitts i Will Update the question with more extensive code included namespaces. Thank you. Will Be Updates in 10 hoursDallapiccola
@Kitts I have updated the question, and will put a bounty on the question when it is eligible, just so you know you will get the extra bonus for your help :)Dallapiccola
Sorry bu t by namespace I mean Notification Namespace name, you specified one creating Notification Hub on the portal.Kitts
@Kitts BattleCrestHub-nsDallapiccola
Regarding your updated question: Please provide your code for updating a registration. Are you sure, you provide the user tag again, when you update the registration?Tindall
@BarisAkar I will do this later, as I do not have the code available here. But it is basicly hub.CreateorUpdate(reg,tag) as far as I remember. Reg, has the registration ID I have saved in my Db. I will update the question to reflect itDallapiccola
@BarisAkar The update code is added. If I run the code on an existing user he is removed. If I run it once more he is added again. And I do not understand why?Dallapiccola
Please add the code where you call updateRegistration() and where you create/populate the list of tags. All of your provided code looks OK to me. Either there is a bug in creating the list of tags or a bug in Azure Notification Hubs...Tindall
@BarisAkar Added the code. The idea is that I have a db with the tags, I then check if it exists and update the db. I then make a call to the registrations with all the tags in a list. I do not understand how it can delete the tags, and another call makes it appear again.Dallapiccola
@JTIM: Sorry, but I have no idea what's going wrong there. Try to narrow the problem down by unit testing your code.Tindall
@BarisAkar, sad to hear it :) Okay, I am a bit unsure how to accomplish this on the Mobile Service. Could it be something with the version of Mobile service API? Or could it be something with me utilizing the same registration.ID, i.e. the line ` registration.RegistrationId = userTags[0].RegistrationID;` since I then override the found Registration ID, maybe I have a misconception here on how to register and use the registration ID?Dallapiccola
@JTIM: no, it's basically correct to provide the same registration ID to update a given registration...Tindall

© 2022 - 2024 — McMap. All rights reserved.