APNS Input string was not in a correct format. Disconnected

1.6k Views Asked by At

I am trying to set up apple push notifications, i am writing my own code for the server and as far as i know i have set up the app correctly. How ever i keep getting the following error come back from the log:

Payload queue received. Connecting to apple server. Creating SSL connection. Conected. Payload generated for "Token goes here i deleted it : {"aps":{"alert":"test","badge":1,"sound":"default"}} Notification successfully sent to APNS server for Device Toekn : "Token here I've deleted it" An error occurred while reading Apple response for token "Token here I've deleted it" - Input string was not in a correct format. Disconnected

This is my code.

var push = new PushNotification(true, @"C:\wwwroot\UltraNet\PushService\bin\Debug\206dist.p12", "ultrait");

var payload = new NotificationPayload("devicetoken here ive deleted it", "test", 1, "default");
var p = new List<NotificationPayload> { payload };

var result = push.SendToApple(p);
Console.ReadLine();

I have made sure that the certificates etc are set up correctly.

I am testing it as a adhoc app at the moment because it takes so long for a new version to be able to go live.

I really don't know where I'm going wrong if any one could help it would be brilliant thank you.

I also don't know what i need to do with the PEM files that i have created.

Edit***

I have the correct token this is another error that i receive

Payload generated for df99286a1cb993cecba86b2e21f3fc4c04d214fcf7e0cf35a668fc822bdaa053 : {"aps":{"alert":"test","badge":1,"sound":"default"}} Notification successfully sent to APNS server for Device Toekn : df99286a1cb993cecba86b2e21f3fc4c04d214fcf7e0cf35a668fc822bdaa053 Disconnected. An error occurred while reading Apple response for token df99286a1cb993cecba86b2e21f3fc4c04d214fcf7e0cf35a668fc822bdaa053 - Safe handle has been closed

2

There are 2 best solutions below

1
On BEST ANSWER

It was all to do with my certificates. Because i hadn't turnt my combined PEM certificate back to a p12 file.

1
On

Based on the code of ReadResponse (see below), the Input string was not in a correct format error message refers to the response received from Apple, and not to the notification you sent. The code failed to properly read the error response from Apple.

Had it succeeded in reading the response, you would have known what the exact failure was and which message failed. Since you don't have the error response, it's a safe bet to assume the problem is your device token. That's the most common failure. If you can isolate the device token for which the error occurs, you should simply delete that token from your DB. Invalid Device Token error often occurs when you try to use sandbox tokens when pushing to production environment or vica versa.

private void ReadResponse(IAsyncResult ar)
{
    if (!_conected)
        return;
    string payLoadId = "";
    int payLoadIndex = 0;
  try
  {
    var info = ar.AsyncState as MyAsyncInfo;
    info.MyStream.ReadTimeout = 100;
    if (_apnsStream.CanRead)
    {
      var command = Convert.ToInt16(info.ByteArray[0]);
      var status = Convert.ToInt16(info.ByteArray[1]);
      var ID = new byte[4];
      Array.Copy(info.ByteArray, 2, ID, 0, 4);

      payLoadId = Encoding.Default.GetString(ID);
      payLoadIndex = ((int.Parse(payLoadId)) - 1000);
      Logger.Error("Apple rejected palyload for device token : " + _notifications[payLoadIndex].DeviceToken);
      Logger.Error("Apple Error code : " + _errorList[status]);
      Logger.Error("Connection terminated by Apple.");
      _rejected.Add(_notifications[payLoadIndex].DeviceToken);
      _conected = false;
    }
  }
  catch (Exception ex)
  {
      Logger.Error("An error occurred while reading Apple response for token {0} - {1}", _notifications[payLoadIndex].DeviceToken, ex.Message);
  }
}