about raw notification running from Server
-
Friday, September 21, 2012 9:48 PM
Hello everybody,
I open channel for notification in client, send it to server, server receive URI and try to send from server by this procedure... by debugging this void pass without any error
In client (OnPushNotificationReceived) never happened but if SendNotification work correct must happened, no? There is the way to check witch notifications now stay in https://db3.notify.windows.com?
Server Side:
public static void SendNotification(string deviceUri, string msg2)
{
// create a payload for a toast notification
string msg =
"<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<wp:Notification xmlns:wp=\"WPNotification\">" +
"<wp:Toast>" +
"<wp:Text1><string></ltwp:Text1>" +
"<wp:Text2><string></wp:Text2>" +
"</ltwp:Toast>" +
"</wp:Notification>";byte[] msgBytes = new UTF8Encoding().GetBytes(msg);
// create a web request that identifies the payload as a toast notification
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(deviceUri);
request.Method = WebRequestMethods.Http.Post;
request.ContentType = "text/xml";
request.ContentLength = msg.Length;
request.Headers["X-MessageID"] = Guid.NewGuid().ToString();
request.Headers["X-WindowsPhone-Target"] = "toast";
request.Headers["X-NotificationClass"] = "2";// post the payload
Stream requestStream = request.GetRequestStream();
requestStream.Write(msgBytes, 0, msgBytes.Length);
requestStream.Close();
}client side:
private static async void OnPushNotificationReceived(PushNotificationChannel sender, PushNotificationReceivedEventArgs e)
{
if (e.NotificationType == PushNotificationType.Raw)
{
e.Cancel = true;
await _dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
// rootPage.NotifyUser("Raw notification received with content: " + e.RawNotification.Content, NotifyType.StatusMessage);
});
}
}
public static async void OpenRAWChannel()
{
try
{
if (channel == null)
{
channel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();
String uri = channel.Uri;
await _dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
channel.PushNotificationReceived += OnPushNotificationReceived;
});
}bool b = await ConnectToServer();
}
catch (Exception ex)
{
//rootPage.NotifyUser("Could not create a channel. Error number:" + ex.Message, NotifyType.ErrorMessage);
}
}- Moved by Josh Twist (MSFT) Friday, September 21, 2012 10:42 PM Not applicable to Mobile Services (From:Windows Azure Mobile Services)
All Replies
-
Friday, September 21, 2012 10:59 PMbut you send a tile notitication now? so the method would never fire. you need to send raw notitifcations then from your server
-
Saturday, September 22, 2012 4:08 AM
steps is:
client part:
1) OpenRAWChannel();
2) registate OnPushNotificationReceived
3) and send uri to Server WebServices
server part:
4)receive uri
5) and start SendNotification by received uri
6).... waiting brakepoint on OnPushNotificationReceived but nothing happened :(
i have sertificate into WebClouds into Certificates of my task. I need to registrate this certificate in notification Server? may be this is a problem?
- Edited by Andrey Filatov Saturday, September 22, 2012 6:54 AM
-
Saturday, September 22, 2012 6:54 AM
But SendNotification makes an toast notification, not a raw one so that event would not triggered.
If you have the permissions right in your manifest you should see a toast message instead. Check if you allow toast meesages on the first page of the manifest.
-
Saturday, September 22, 2012 7:02 AM
i allow toast messages in manifest .. the same doesn't work... how i can check what part doesn't work (client or server)? errors not rise not in client not in server :(
brakepoint what i used must be fire on every type of notification.... :(
private static async void OnPushNotificationReceived(PushNotificationChannel sender, PushNotificationReceivedEventArgs e)
*** (brakepoint) {
if (e.NotificationType == PushNotificationType.Raw)may be you have example of correct void who can send raw notifications from server part or my void send toast in correct way?
- Edited by Andrey Filatov Saturday, September 22, 2012 10:05 AM
-
Saturday, September 22, 2012 11:13 AMdid you registered your app for the access_token?
-
Saturday, September 22, 2012 11:58 AMno, only made project and publish web part
-
Saturday, September 22, 2012 12:08 PM
read this: http://msdn.microsoft.com/en-us/library/windows/apps/hh465407.aspx
at the end they tell you how to authenticate with wns if you dont have a store account
- Marked As Answer by Andrey Filatov Sunday, September 30, 2012 5:04 PM
-
Saturday, September 22, 2012 1:35 PM
this is a correct code for send from WebServices to notification server? have problem with DataContract, can not find it in server side this libraries, for client DataContract found without problem but i need server code :(
using WEBServices.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Xml;
using System.Web;
using System.Runtime.Serialization;
using System.Threading.Tasks;
using System.IO;
public class RAWSender
{
[DataContract]
public class OAuthToken
{
[DataMember(Name = "access_token")]
public string AccessToken { get; set; }
[DataMember(Name = "token_type")]
public string TokenType { get; set; }
}
// Authorization
// Post to WNS
public string PostToWns(string secret, string sid, string uri, string xml, string type = "wns/toast")
{
try
{
// You should cache this access token
var accessToken = GetAccessToken(secret, sid);
byte[] contentInBytes = Encoding.UTF8.GetBytes(xml);
HttpWebRequest request = HttpWebRequest.Create(uri) as HttpWebRequest;
request.Method = "POST";
request.Headers.Add("X-WNS-Type", type);
request.Headers.Add("Authorization", String.Format("Bearer {0}", accessToken.AccessToken));
using (Stream requestStream = request.GetRequestStream())
requestStream.Write(contentInBytes, 0, contentInBytes.Length);
using (HttpWebResponse webResponse = (HttpWebResponse)request.GetResponse())
return webResponse.StatusCode.ToString();
}
catch (WebException webException)
{
string exceptionDetails = webException.Response.Headers["WWW-Authenticate"];
if (exceptionDetails.Contains("Token expired"))
{
GetAccessToken(secret, sid);
// Implement a maximum retry policy
return PostToWns(uri, xml, secret, sid, type);
}
else
{
// Log the response
return "EXCEPTION: " + webException.Message;
}
}
catch (Exception ex)
{
return "EXCEPTION: " + ex.Message;
}
}
- Edited by Andrey Filatov Saturday, September 22, 2012 1:47 PM


