Azure WindowsPushMessage Xml Payload Format - c#

On my Azure Mobile Service, I am sending a Push Notification to a Windows Phone 8.1 with the following code:
WindowsPushMessage message = new WindowsPushMessage();
message.XmlPayload = #"<?xml version=""1.0"" encoding=""utf-8""?>" +
#"<toast><visual><binding template=""ToastText01"">" +
#"<text id=""1"">" + pushString + #"</text>" +
#"</binding></visual></toast>";
try
{
var result = await Services.Push.SendAsync(message);
Services.Log.Info(pushString);
}
catch (System.Exception ex)
{
Services.Log.Error(ex.Message, null, "Push.SendAsync Error");
}
I want to change the format of the Notification, Though, and all I can seem to find are these formats:
WindowsPushMessage message = new WindowsPushMessage();
message.XmlPayload = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<wp:Notification xmlns:wp=\"WPNotification\">" +
"<wp:Toast>" +
"<wp:Text1>Yo Dawg</wp:Text1>" +
"</wp:Toast> " +
"</wp:Notification>";
WindowsPushMessage message = new WindowsPushMessage();
message.XmlPayload ="<wp:Notification xmlns:wp=\"WPNotification\">" +
"<wp:Toast>" +
"<wp:Text1>" + TextBoxTitle.Text.ToString() + "</wp:Text1>" +
"<wp:Text2>" + TextBoxSubTitle.Text.ToString() + "</wp:Text2>" +
"<wp:Param>/Page2.xaml?NavigatedFrom=Toast Notification</wp:Param>" +
"</wp:Toast> " +
"</wp:Notification>";
But these formats do not work correctly with WindowsPushMessage payload. I receive this error for either one of the them:
The payload is not in accepted XML format. The first node should be Badge/Tile/Toast. If want to send raw notification, please set header to wns/raw.
Can anyone tell me what the correct format would be?

Something like the following:
message.XmlPayload = #"<?xml version=""1.0"" encoding=""utf-8""?>" +
#"<toast><visual><binding template=""ToastNewPayload"">" +
#"<text id=""1"">" + TextBoxTitle.Text.ToString()+ #"</text>" +
#"<text id=""2"">" + TextBoxSubTitle.Text.ToString() + #"</text>" +
#"<text id=""3"">/Page2.xaml?NavigatedFrom=Toast Notification</text>" +
#"</binding></visual></toast>";
See the following doc: https://azure.microsoft.com/en-us/documentation/articles/notification-hubs-templates/

Related

Error in RestSharp Request? , XML , The ':' character, hexadecimal value 0x3A, cannot be included in a name

The : character , hexadecimal value 0x3A, cannot be included in a name.
I get the above error from the API while parsing an XML body in a RestSharp POST Request.
What could I do?
string xmlBody = "<soap:Envelope" +
" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" +
" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"" +
" xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\">" +
"<soap:Body> " +
"<MainField " +
" xmlns =\"http://www.w3.org\">" +
"<Username>string</Username> " +
"<Password>string</Password> " +
"<FieldPlace> " +
"<Value1>string</Value1> " +
"<Value2>string</Value2> " +
"</FieldPlace> " +
"</MainField> " +
"</soap:Body> " +
"</soap:Envelope>";
requestPost.AddParameter("text/xml", xmlBody, "text/xml" , ParameterType.RequestBody);
This is the XML
<soap:Envelope
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
<soap:Body>
<MainField>
xmlns ="http://www.w3.org">
<Username>string</Username>
<Password>string</Password>
<FieldPlace>
<Value1>string</Value1>
<Value2>string</Value2>
</FieldPlace>
</MainField>
</soap:Body>
</soap:Envelope>
Please try with this corrected XML and let me know if that works for you.
Couple of points to note:
Missing spaces on the xmlns at the soap:Envelope element.
The MainField element was closed before adding the namespace.
string xmlBody = "<soap:Envelope" +
" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" +
" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"" +
" xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\">" +
"<soap:Body> " +
"<MainField " +
"xmlns=\"http://www.w3.org/2003/05/soap-envelope\" > " +
"<Username>string</Username> " +
"<Password>string</Password> " +
"<FieldPlace> " +
"<Value1>string</Value1> " +
"<Value2>string</Value2> " +
"</FieldPlace> " +
"</MainField> " +
"</soap:Body> " +
"</soap:Envelope>";
I'd also recommend using an alternative method to generate the XML string, perhaps the System.Xml.XmlWriter.

Yahoo Weather API by Lat and Lon

I'm writing a weather app in Xamarin.Form. I am using the Yahoo API. I have no problem getting the weather by the city name parameter. However, when I change the code to use longitude and latitude, the weather does not appear.
To download the weather I use the example from the page: https://developer.yahoo.com/weather/documentation.html#oauth-csharp
I processed it in the following way:
lSign = string.Format(
"format={0}&" +
"lat={1}&" +
"lon={2}&" +
"oauth_consumer_key={3}&" +
"oauth_nonce={4}&" +
"oauth_signature_method={5}&" +
"oauth_timestamp={6}&" +
"oauth_version={7}&" +
"u={8}",
cFormat,
szerokosc,
dlugosc,
cConsumerKey,
lNonce,
cOAuthSignMethod,
lTimes,
cOAuthVersion,
jednostka.ToString().ToLower()
(...)
url = cURL + "?lat=" + szerokosc + "&lon=" + dlugosc + "&u=" + jednostka.ToString().ToLower() + "&format=" + cFormat;
According to the documentation, lSign is used for authentication. It should not be changed, remove these "lat={1}&" + "lon={2}&" from that strings.
It says Please don't simply change value of any parameter without
re-sorting.
The location information should be involved in the request url and the authorization information is added in the header.
// Add Authorization
lClt.Headers.Add ( "Authorization", _get_auth () );
// The request URL
lURL = cURL + "?" + "lat=" + szerokosc + "&lon=" + dlugosc + "&format=" + cFormat;
Unfortunately, the simple removal of " lat = {1} & " + " lon = {2} & " from variable lSign does not solve the problem.
For example, to get weather data by the city name I use:
lSign = string.Format(
"format={0}&" +
"location={1}&" +
"oauth_consumer_key={2}&" +
"oauth_nonce={3}&" +
"oauth_signature_method={4}&" +
"oauth_timestamp={5}&" +
"oauth_version={6}&" +
"u={7}",
cFormat,
miasto,
cConsumerKey,
lNonce,
cOAuthSignMethod,
lTimes,
cOAuthVersion,
jednostka.ToString().ToLower()
and
url = cURL + "?location=" + Uri.EscapeDataString(miasto) + "&u=" + jednostka.ToString().ToLower() + "&format=" + cFormat;
and
string headerString = _get_auth();
WebClient webClient = new WebClient();
webClient.Headers[HttpRequestHeader.ContentType] = "application/" + cFormat;
webClient.Headers[HttpRequestHeader.Authorization] = headerString;
webClient.Headers.Add("X-Yahoo-App-Id", cAppID);
byte[] reponse = webClient.DownloadData(url);
string lOut = Encoding.ASCII.GetString(reponse);

Google Cloud Speech response always empty error

I am sending audio to GCS, via "https://speech.googleapis.com/v1/speech:recognize?key=<my key>", the following way:
byte[] audioBytes = g_NPCAudioListener.GetAudioClipMonoData16(88200);
string jsonData = "{" +
"\"config\": {" +
"\"encoding\": \"LINEAR16\"," +
"\"sampleRateHertz\": 48000," +
"\"languageCode\": \"en-US\"" +
"}," +
"\"audio\": {" +
"\"content\" : \"" + Convert.ToBase64String(audioBytes) + "\"" +
"}" +
"}";
byte[] postData = System.Text.Encoding.UTF8.GetBytes(jsonData);
g_NPCController.Debug("Sending to google: " + jsonData);
using (WWW req = new WWW(g_Google_Speech_URL, postData, g_JSONHeaders)) {
yield return req;
if (req.error == null) {
g_NPCController.Debug(req.text.Replace("\n", "").Replace(" ", ""));
} else {
string msg = Convert.ToString(req.bytes);
g_NPCController.Debug("Google Speech Error: " + req.error + "\n - error: " + req.text);
}
}
Everything is up to specification, however, I keep getting nothing but an error flag with an empty body.
While working on the main JSON impl, I was getting "Invalid parameter" and such... but now that I am streaming 88200 chunks of 16-bit uncompressed audio bytes, I keep getting an error with no text attached to it - not even a code. Did anyone come across a similar situation?
If relevant, the audio I get it from an AudioClip in Unity, then convert the 32-bit float[] to a byte[originalAudio.Length * sizeof(float)] and then to base64 as required.
Thanks.

C# - How to detect browser type

I'm running Selenium with C# for my automation testing on multiple browsers (IE, FF, Chrome) and there is one part of my test that passes for Chrome but not Firefox.
Is there a way to detect the browser type that is currently being used during the automated test?
You can install UAParser from Nugget :
https://www.nuget.org/packages/UAParser/
It will read the client header and Parse it.
Exemple:
//string uaString = "Mozilla/5.0 (iPhone; CPU iPhone OS 5_1_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9B206 Safari/7534.48.3";
// Request the header
string uaString= HttpContext.Current.Request.UserAgent.ToString();
// get a parser with the embedded regex patterns
var uaParser = Parser.GetDefault();
// get a parser using externally supplied yaml definitions
// var uaParser = Parser.FromYamlFile(pathToYamlFile);
// var uaParser = Parser.FromYaml(yamlString);
ClientInfo c = uaParser.Parse(uaString);
Console.WriteLine(c.UserAgent.Family); // => "Mobile Safari"
Console.WriteLine(c.UserAgent.Major); // => "5"
Console.WriteLine(c.UserAgent.Minor); // => "1"
Console.WriteLine(c.OS.Family); // => "iOS"
Console.WriteLine(c.OS.Major); // => "5"
Console.WriteLine(c.OS.Minor); // => "1"
Console.WriteLine(c.Device.Family); // => "iPhone"
Use the following code
System.Web.HttpBrowserCapabilities browser = Request.Browser;
string s = "Browser Capabilities\n"
+ "Type = " + browser.Type + "\n"
+ "Name = " + browser.Browser + "\n"
+ "Version = " + browser.Version + "\n"
+ "Major Version = " + browser.MajorVersion + "\n"
+ "Minor Version = " + browser.MinorVersion + "\n"
+ "Platform = " + browser.Platform + "\n"
+ "Is Beta = " + browser.Beta + "\n"
+ "Is Crawler = " + browser.Crawler + "\n"
+ "Is AOL = " + browser.AOL + "\n"
+ "Is Win16 = " + browser.Win16 + "\n"
+ "Is Win32 = " + browser.Win32 + "\n"
+ "Supports Frames = " + browser.Frames + "\n"
+ "Supports Tables = " + browser.Tables + "\n"
+ "Supports Cookies = " + browser.Cookies + "\n"
+ "Supports VBScript = " + browser.VBScript + "\n"
+ "Supports JavaScript = " +
browser.EcmaScriptVersion.ToString() + "\n"
+ "Supports Java Applets = " + browser.JavaApplets + "\n"
+ "Supports ActiveX Controls = " + browser.ActiveXControls
+ "\n"
+ "Supports JavaScript Version = " +
browser["JavaScriptVersion"] + "\n";

how to give XML as input to XML webservice and receive XML as output

I want to send XML file as input to XML Webservice and receive output as XML.
Following code is my first attempted:
XmlDocument node = new XmlDocument();
node.LoadXml("<IDV>" +
"<start_date>" + "25/12/2014" + "</start_date>"<br>
"<vehicle_class_cd>" + "45" + "</vehicle_class_cd>"
"<RTOLocation>" + "10416" + "</RTOLocation>"
"<vehiclemodelcode>" + "22647" + "</vehiclemodelcode>"
"<manufacturer_code>" + "12474" + "</manufacturer_code>"
"<purchaseregndate>" + "27" + "</purchaseregndate>"
"<manufacturingyear>" + "2014" + "</manufacturingyear>"
"</IDV>");
demo.Service src = new demo.Service();
var str = src.getIDV(node.ToString());

Categories

Resources