0

See the edit below, I found out that the problem is with the c# code but can't get where !

I know that previously the maximum payload size was 256b but now with ios8 we can send up to 2kb of payload but it is not working for me.

Stated here: APN (Apple Push Notification) payload size limit

Examples:

If i send this payload works fine (I receive the notification):

{"aps":{"alert":"New negotiation opened from (idOperation: 5) pablo.gomez","sound":"default"},"data":{"notificationType":"2","idOperation":"5","link":"http://localhost:7000/Login.aspx?idInstance=9&forward=VE9fVHJhZGluZy5hc3B4P2lkT3BlcmF0aW9uPTU="}}

But if I send one longer it won't work (I don't receive it on my phone), for example this:

"{"aps":{"alert":"New negotiation test test test test test test test opened from (idOperation: 5) pablo.gomez","sound":"default"},"data":{"notificationType":"2","idOperation":"5","link":"http://localhost:7000/Login.aspx?idInstance=9&forward=VE9fVHJhZGluZy5hc3B4P2lkT3BlcmF0aW9uPTU="}}"

I tried this on development and on production also. Both fail with the second one. I have tested this with an iPhone 5 iOS 8.1.3

My app has iOS deployment target iOS 8.0 but the version that I have uploaded to the store has deployment target iOS 7.0, so maybe this is the problem when I tested with production environment?

I read something about the category of the notification here: iOS8 and iOS7 Push Notification Payload

Maybe this could fix it?

And this is how I send the payload to apple with c#

        ...
        try
            {
                memoryStream = new MemoryStream();
                writer = new BinaryWriter(memoryStream);

                writer.Write((byte)0);  //The command
                writer.Write((byte)0);  //The first byte of the deviceId length (big-endian first byte)
                writer.Write((byte)32); //The deviceId length (big-endian second byte)
                //bw.Write(new byte[] { 0, 0, 32 }); Other way of doing this.
                writer.Write(this.HexStringToByteArray(token.ToUpper()));

                writer.Write((byte)0);
                writer.Write((byte)payload.Length);
                writer.Write(System.Text.Encoding.UTF8.GetBytes(payload));
                writer.Flush();

                sslStream.Write(memoryStream.ToArray());
                sslStream.Flush();
                responseString += " SENT TO: " + token + "<br />";
            }
        ...


private byte[] HexStringToByteArray(string hexString)
    {
        if (hexString == null)
            return null;

        if (hexString.Length % 2 == 1)
            hexString = '0' + hexString; // Up to you whether to pad the first or last byte

        byte[] data = new byte[hexString.Length / 2];

        for (int i = 0; i < data.Length; i++)
            data[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);

        return data;
    }

EDIT: I tried pushing the notif with a script in PHP an works fine so the problem is why my c# code.. But can't find the problem here..

Community
  • 1
  • 1
Pablo Matias Gomez
  • 6,614
  • 7
  • 38
  • 72

1 Answers1

1

Ok, so finally figured it out what the problem was. As you can see in the code, I was sending the command 0 which is an old version of the notificacion service.

The new notification version is 2 as described here: https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/CommunicatingWIthAPS.html#//apple_ref/doc/uid/TP40008194-CH101-SW4

So I made a refactor to support this new version and now all notifications are received fine.

The working code is this:

        ...
        try
            {
                memoryStream = new MemoryStream();
                writer = new BinaryWriter(memoryStream);

                writer.Write((byte)2);  // The command (version 2)

                // 4 bytes for the frameLength (this includes all the items ids listed below)
                int frameLength = 1 + 2 + 32 + 1 + 2 + payload.Length; // (tokenCommand + tokenLength + token) + (payloadCommand + payloadLength + payload) 
                this.writeIntBytesAsBigEndian(writer, frameLength, 4);

                // DEVICE ID
                writer.Write((byte)1);  // Command for Item ID: deviceId
                byte[] tokenBytes = this.HexStringToByteArray(token);
                this.writeIntBytesAsBigEndian(writer, tokenBytes.Length, 2);
                writer.Write(tokenBytes);

                // PAYLOAD
                writer.Write((byte)2); // Command for Item ID: payload
                this.writeIntBytesAsBigEndian(writer, payload.Length, 2);
                writer.Write(Encoding.ASCII.GetBytes(payload), 0, payload.Length);

                writer.Flush();

                sslStream.Write(memoryStream.ToArray());
                sslStream.Flush();
                responseString += " SENT TO: " + token + "<br />";
            }
            ...


    private void writeIntBytesAsBigEndian(BinaryWriter writer, int value, int bytesCount)
    {
        byte[] bytes = null;
        if (bytesCount == 2)
            bytes = BitConverter.GetBytes((Int16)value);
        else if (bytesCount == 4)
            bytes = BitConverter.GetBytes((Int32)value);
        else if (bytesCount == 8)
            bytes = BitConverter.GetBytes((Int64)value);

        if (bytes != null)
        {
            Array.Reverse(bytes);
            writer.Write(bytes, 0, bytesCount);
        }
    }
Pablo Matias Gomez
  • 6,614
  • 7
  • 38
  • 72