Reputation: 198
I got problem with Windows Azure Mobile Service. I created PushServiceMobileService
, in my mobile application is notyifyAllUsers
service call, everything is ok, but how to make controller, which will send notification only to specified user (for example "you have 2 new friends")? I understand that channel.Uri
which is generated on mobile application startup is destination for my requests, but all on sending http request on this href I have Http 400 response. Could you tell me how to build that request? Thanks a lot.
Ps. sorry for my English ;)
Upvotes: 0
Views: 443
Reputation: 3157
I would like to show you my example which i created to use in my application. I also use Azure Mobile Services and Notification Hub.
Let me divide it in two sections:
Test application for sending push notifications code.
In App.xaml.cs (Windows Phone project) class I have created this method:
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;
}
}
In subscribtion array you can put login or name of person who is for example currently logged into your application. After you fill subscription array you must attach it to the RegisterNativeAsync method as a parameter next to your channel Uri.
Now in your test push notification console application you can check it using this method:
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");
}
Now if you send push it will be only send to person whose name you added to subscription string array in Windows Phone application.
I also paste code for handling received push in app:
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;
}
I hope it will help.
Upvotes: 4