When I try to send a notification from my server I receive this message: "Invalid token size"
From this thread, PushSharp doesn't send notifications, I found out that it might be because I'm trying to use a Sandbox cert to the the production server or vice versa but I don't think so as I don't have any production certificate setup.
I then exported the APNs Development Certificate (.p12) and use that one on the server (as required by PushSharp).
I have exported the .p12 file again to make sure that the certificate is the actual one but with no luck.
I'm using the "Sandbox" flag as well.
Here is the C# code that I'm using:
static class APN
{
static PushBroker push = new PushBroker();
static byte[] appleCert = File.ReadAllBytes(#"C:\Certs\PineAppPushDev.p12");
static public void StartAPN()
{
Console.WriteLine("Starting APN ...");
/* Event listeners */
push.OnChannelException += broker_OnChannelException;
push.OnNotificationFailed += broker_OnNotificationFailed;
push.RegisterAppleService(new ApplePushChannelSettings(false, appleCert, "*****"));
}
static private void broker_OnChannelException(object sender, PushSharp.Core.IPushChannel pushChannel, System.Exception error)
{
Console.WriteLine("broker_OnChannelException:");
Console.WriteLine("PushChannel: " + pushChannel.ToString());
Console.WriteLine("Error: " + error.ToString());
}
static private void broker_OnNotificationFailed(object sender, PushSharp.Core.INotification notification, System.Exception error)
{
Console.WriteLine("broker_OnNotificationFailed:");
Console.WriteLine("Notification: " + notification.ToString());
Console.WriteLine("Error: " + error.ToString());
}
static public void SendAPN(string message, string deviceID)
{
Console.WriteLine("SendAPN");
Console.WriteLine("DeviceID: " + deviceID.ToString());
try
{
push.QueueNotification(new AppleNotification()
.ForDeviceToken(deviceID)
.WithAlert(message));
}
catch (Exception ex)
{
Console.WriteLine("ERROR: APN.SendAPN(): " + ex.ToString());
}
}
}
Not sure what I'm missing, any help is highly appreciated!
Related
Hello I am new to this but I am developing a client to Mosquitto broker.
It works fine, but I want to know how could I add the Sender Id to the message.
i.e. Message From "Client1" : "LightON"
This is how I handle the subscription
private void Form1_Load_1(object sender, EventArgs e)
{
try
{
IPAddress HostIP;
HostIP = IPAddress.Parse(textBox1.Text);
clientSub = new MqttClient(HostIP);
clientSub.MqttMsgPublishReceived += new MqttClient.MqttMsgPublishEventHandler(EventPublished);
}
catch (InvalidCastException ex)
{
MessageBox.Show("ERROR ON LOAD" + ex.ToString());
}
}
The Publish Event is :
private void EventPublished(Object sender, uPLibrary.Networking.M2Mqtt.Messages.MqttMsgPublishEventArgs e)
{
try
{
SetText("Recevied Message..");
SetText("The Topic is:" + e.Topic);
SetText("*Message: " + System.Text.UTF8Encoding.UTF8.GetString(e.Message));
SetText("");
}
catch (InvalidCastException ex)
{
}
}
And I am using the M2mqtt library.
The only way to do this is to add it to the message payload yourself.
There is no concept of a publisher id in the MQTT headers. Client IDs are only to identify clients to the broker, not end to end.
I am new to C# sockets.
I am trying to connect to Web Socket, but I am neither getting connected message nor error message and no Exception.
What I need to do to connect with web socket? How can I trace whether it is trying to connect to socket?
My Code:
private WebSocket client;
const string host = "wss://stream.binance.com:9443";
private void button2_Click(object sender, EventArgs e)
{
client = new WebSocket(host);
client.OnOpen += (ss, ee) =>
MessageBox.Show("Concceted");
client.OnError += (SetStyle, ee) =>
MessageBox.Show("error");
client.Connect();
}
I guess you use WebSocketSharp. The following console test program tries to connect to the same url from your question:
using System;
using WebSocketSharp;
namespace Example
{
public class Program
{
public static void Main(string[] args)
{
using (var ws = new WebSocket("wss://stream.binance.com:9443"))
{
ws.OnMessage += (sender, e) =>
Console.WriteLine("Message received" + e.Data);
ws.OnError += (sender, e) =>
Console.WriteLine("Error: " + e.Message);
ws.Connect();
Console.ReadKey(true);
}
}
}
}
When I run it i get the following console output:
Fatal|WebSocket.doHandshake|Not a WebSocket handshake response.
According to the binance websocket stream documentation you need to change your url to e.g.
wss://stream.binance.com:9443/ws/bnbbtc#ticker
I recommend that you print the content of the error message (in this case e.Message) if possible because it can give you valuable hints to what might be the cause of the error.
I am writing a GUI for Modem communication tool, able to receive AT command and also return the result from modem after execution in modem. I use serial port datareceived event to handle the received data but it contains not only the response from the modem but also the AT command I give. Now I think it is because:
1) send at command from my GUI
2) Modem receive it, then trigger datareceived events
3) Modem runs the command, the reply also trigger datareceived events.
4) the received data then contains both my given command and also the reply
For example:
Input: AT+COPS=0
Output:AT+COPS=0OK (OK is modem response)
Input: AT
Output:ATOK (OK is modem response)
I could not find a solution in MSDN. Are there two different buffers in Modem?
How to solve this?
Here is my code:
void serialPort_DataReceived(object s, SerialDataReceivedEventArgs e)
{
if (e.EventType != SerialData.Chars)
{
return;
}
try
{
System.Threading.Thread.Sleep(1000);
string indata = atPort.ReadExisting();
string status = timeStamp.ToShortDateString() + " " + timeStamp.ToUniversalTime() + " " + "Read from Modem: " + indata;
this.Invoke(new MethodInvoker(delegate () { this.listBox_CommandOutput.Items.Add(status); }));
}
catch (Exception ex)
{
}
}
private void executeCommandLine()
{
if (this.textBox_CommandLine.Text != "")
{
try
{
atPort.DiscardInBuffer();
atPort.DiscardOutBuffer();
this.atPort.Write(this.textBox_CommandLine.Text.ToString()+"\\r");
this.listBox_CommandOutput.Items.Add(timeStamp.ToShortDateString() + " " + timeStamp.ToUniversalTime() + " " + "Write to Modem: " + this.textBox_CommandLine.Text.ToString());
}
catch (Exception exc)
{
this.listBox_CommandOutput.Items.Add(exc.ToString());
}
}
else MessageBox.Show("Command can't be void.", "COM talk", MessageBoxButtons.OK);
}
The problem is not in your code. Some modem by default use the echo mode by default: they echo every character sent to them to the sender (so you can use them like a terminal).
You can disable the echo mode using the AT command:
ATE0
I would like to catch unhandled exceptions in a windows service application in the class that inherits from ServiceBase class.
I have already tried incorporating the code:
AppDomain.CurrentDomain.UnhandledException += (s, e) =>
{
var exception = (Exception)e.ExceptionObject;
Log.Error("Unhandled exception", exception);
};
But that doesn't work.
Try this:
// Starts the application.
[SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.ControlAppDomain)]
public static void Main(string[] args)
{
// Add the event handler for handling non-UI thread exceptions to the event.
AppDomain.CurrentDomain.UnhandledException +=
new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
// Runs the application.
Application.Run(new ErrorHandlerForm());
}
private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
try
{
Exception ex = (Exception)e.ExceptionObject;
string errorMsg = "An application error occurred. Please contact the adminstrator " +
"with the following information:\n\n";
// Since we can't prevent the app from terminating, log this to the event log.
if (!EventLog.SourceExists("ThreadException"))
{
EventLog.CreateEventSource("ThreadException", "Application");
}
// Create an EventLog instance and assign its source.
EventLog myLog = new EventLog();
myLog.Source = "ThreadException";
myLog.WriteEntry(errorMsg + ex.Message + "\n\nStack Trace:\n" + ex.StackTrace);
}
catch (Exception exc)
{
try
{
MessageBox.Show("Fatal Non-UI Error",
"Fatal Non-UI Error. Could not write the error to the event log. Reason: "
+ exc.Message, MessageBoxButtons.OK, MessageBoxIcon.Stop);
}
finally
{
Application.Exit();
}
}
}
You also can take a look at this example: https://msdn.microsoft.com/en-us/library/system.windows.forms.application.threadexception.aspx
I'm trying to debug an app without setting fiddler as its proxy.
To do this, I've setup fiddler.core based app that resides on an other computer in the network and added an entry to hosts file.
Here's the app's code:
private static Proxy googleEndpoint;
static void Main(string[] args)
{
List<Fiddler.Session> oAllSessions =new List<Session>();
Fiddler.FiddlerApplication.BeforeRequest += (i) => Console.WriteLine("Before request: "+i.fullUrl);
FiddlerApplication.AfterSessionComplete += (i) =>
{
Console.WriteLine("After request: "+i.fullUrl);
lock (oAllSessions)
{
oAllSessions.Add(i);
}
};
//https://www.google.com.ua/
googleEndpoint = FiddlerApplication.CreateProxyEndpoint(443, true, "www.google.com.ua");
if (null != googleEndpoint)
{
Console.WriteLine("google.com.ua endpoint mounted");
}
else
{
Console.WriteLine("failed to mount google.com.ua endpoint");
}
Console.ReadKey();
SaveSessionsToDesktop(oAllSessions);
}
private static void SaveSessionsToDesktop(List<Fiddler.Session> oAllSessions)
{
bool bSuccess = false;
string sFilename = DateTime.Now.ToString("hh-mm-ss") + ".saz";
try
{
try
{
Monitor.Enter(oAllSessions);
TranscoderTuple oExporter = FiddlerApplication.oTranscoders.GetExporter("SAZ");
if (null != oExporter)
{
Dictionary<string, object> dictOptions = new Dictionary<string, object>();
dictOptions.Add("Filename", sFilename);
// dictOptions.Add("Password", "pencil");
bSuccess = FiddlerApplication.DoExport("SAZ", oAllSessions.ToArray(), dictOptions, null);
}
else
{
Console.WriteLine("Save failed because the SAZ Format Exporter was not available.");
}
}
finally
{
Monitor.Exit(oAllSessions);
}
WriteCommandResponse(bSuccess ? ("Wrote: " + sFilename) : ("Failed to save: " + sFilename));
}
catch (Exception eX)
{
Console.WriteLine("Save failed: " + eX.Message);
}
}
public static void WriteCommandResponse(string s)
{
ConsoleColor oldColor = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine(s);
Console.ForegroundColor = oldColor;
}
but when I'm trying to access https://www.google.com.ua from the target machine, the requests now time out.
Fiddler app shows that it has recieved the request in the BeforeRequest event, but it never sends a reply (AfterSessionComplete never gets called).
Why does this happen and how can I fix it?
How do I do the same for port 80?
I trust you're keeping in mind the fact that, unless you reconfigure the client to trust the FiddlerServer's root certificate, it will not issue a HTTPS request to FiddlerCore. It will instead immediately close the connection upon getting the interception certificate.
What do you see if you attach a BeforeResponse event handler? Does it fire?