大纲
我正在尝试为我的应用游戏实现 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");
}
我收到每个用户名的通知,即个人通知。然后我更新用户收听的标签,查看集线器数据库,这似乎也有效:
- -用户名cph--gameID1151--gameID1152--gameID1153--gameID1154--gameID1155--gameID1156--gameID1157--gameID1158
- -gameID1157--用户名Fyn--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);
}
到目前为止,一切都很好。
然后,当我尝试发送到除用户名之外的其他标签之一时,我没有收到任何通知,并且我没有在日志中收到任何错误。我错过了什么吗?
更新 - 一半的解决方案
如果我使用服务器资源管理器并查看通知 HUB。我可以看到所有的标签,我可以使用测试发送发送给他们。但我似乎无法在其他在线函数调用中做到这一点。
是否必须将功能设置为 post 或 get?
是的
所以插入[HttpPost]
似乎启用了推送。
当我更新注册时,用户似乎被删除了(应该有三个,当我用正确的标签订阅启动应用程序时,又是三个)。所以也许问题是如何正确更新集线器中的注册
当前更新代码:
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);
}
}
调用函数的代码:
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>() };
}
新图 我真的不明白,有时候我注册了三个,然后在柜台底部仍然只说2?怎么会这样?(视图来自VS2013中的服务器资源管理器)