我在使用 Windows Azure 移动服务时遇到问题。我创建了 PushServiceMobileService
,在我的移动应用程序中是 notyifyAllUsers
服务调用,一切正常,但是如何制作 Controller ,该 Controller 仅向指定用户发送通知(例如“您有 2 个新 friend ”)?我知道移动应用程序启动时生成的 channel.Uri
是我的请求的目的地,但所有在此 href 上发送 http 请求时,我都有 Http 400 响应。您能告诉我如何构建该请求吗?非常感谢。
诗。对不起我的英语;)
最佳答案
我想向您展示我创建的在我的应用程序中使用的示例。 我还使用 Azure 移动服务和通知中心。
让我将其分为两部分:
- Windows Phone 8.1 代码。
测试用于发送推送通知代码的应用程序。
在 App.xaml.cs(Windows Phone 项目)类中,我创建了此方法:
public static async void InitNotificationsAsync(string userName) { channel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync(); string[] subscription = { userName }; receivingHub = new NotificationHub("yourappnotificationhub", "Endpoint=sb://yourappnotificationhub-ns.servicebus.windows.net/;...."); var result = await receivingHub.RegisterNativeAsync(channel.Uri, subscription); // Displays the registration ID so you know it was successful if (result.RegistrationId != null) { channel.PushNotificationReceived += OnPushNotification; } }
在订阅数组中,您可以输入当前登录您的应用程序的登录名或姓名。 填充订阅数组后,必须将其作为 channel Uri 旁边的参数附加到 RegisterNativeAsync 方法。
现在,在您的测试推送通知控制台应用程序中,您可以使用以下方法检查它:
private static async void SendNotificationAsync()
{
NotificationHubClient hub = NotificationHubClient
.CreateClientFromConnectionString("Endpoint=sb://menotifyappnotificationhub-ns.servicebus.windows.net/;SharedAccessKeyName=DefaultFullSharedAccessSignature;SharedAccessKey=\...", "yourappnotificationhub");
var toast = @"<toast><visual><binding template=""ToastText02""><text id=""1"">test</text><text id=""2"">Hello</text></binding> </visual></toast>";
await hub.SendWindowsNativeNotificationAsync(toast, "User_Name_You_Added_To_String_Array_In_WindowsPhoneApp");
}
现在,如果您发送推送,它将仅发送给您在 Windows Phone 应用程序中添加到订阅字符串数组中的姓名的人员。
我还粘贴了用于处理应用程序中收到的推送的代码:
private static void OnPushNotification(PushNotificationChannel sender, PushNotificationReceivedEventArgs e)
{
String notificationContent = "";
switch (e.NotificationType)
{
case PushNotificationType.Badge:
notificationContent = e.BadgeNotification.Content.GetXml();
break;
case PushNotificationType.Tile:
notificationContent = e.TileNotification.Content.GetXml();
break;
case PushNotificationType.Toast:
notificationContent = e.ToastNotification.Content.GetXml();
//..DO SOME ACTION, FOR EXAMPLE SHOW MESSAGEDIALOG WITH PUSH MESSAGE
break;
case PushNotificationType.Raw:
notificationContent = e.RawNotification.Content;
break;
}
e.Cancel = true;
}
希望对您有所帮助。
关于azure - Windows 手机 8.1 : Send notification to specified user,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28263405/