WNS推送仅发送到某些标签

JTI*_*TIM 9 c# azure push-notification azure-notificationhub wns

大纲

我正在尝试为我的应用游戏实施WNS.我目前在创建具有以下功能的新用户时发送通知,该功能有效:

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");
}
Run Code Online (Sandbox Code Playgroud)

我得到了每个用户名的通知,即个人通知.然后我更新用户监听的标签,查看集线器数据库,这似乎也有效:

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

使用此检查从集线器中提取标签

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);
}
Run Code Online (Sandbox Code Playgroud)

到现在为止还挺好.

然后,当我尝试发送到除用户名之外的其他标签之一时,我没有收到任何通知,并且我在日志中没有收到任何错误.我错过了什么吗?

更新 - 半解决方案

如果我使用服务器资源管理器并查看通知HUB.我可以看到所有标签,我可以使用测试发送给他们.但我似乎无法在其他函数调用中执行此操作.

它是否像函数必须设置为post或get?

所以插入[HttpPost]似乎启用推送.

但是当我查看图像上显示的服务器资源管理器时: Server Explorer图片

当我更新注册时,用户似乎被删除了(应该有三个,当我使用正确的标签订阅启动应用程序时再次出现).所以也许问题是如何正确更新集线器中的注册

当前的更新代码:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

调用函数的代码:

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>() };
}
Run Code Online (Sandbox Code Playgroud)

新形象 我真的不明白它,有时我有三个注册,但那时在柜台底部仍然只说2?怎么会这样?(该视图来自VS2013中的服务器资源管理器) Server explorer的新版本