IOS update icon badge according to payload issue

I implement a push notification in my application. Push notification is successfully implemented.But there is one issue for updating badge number.

(I am talking about when app in not running)

If i send +2 then badge is set to 2.
if i send +5 then badge is set to 5.

I know that there is badge is 0 initially.So nothing on the app icon.
Now i send a push notification with badge = +2 and badge is set to 2.
Now i again send push notification with badge = +3.
"There must be badge set to 2 + 3 = 5. "
But badge is set to 3 , not 5.

it means badge number is just replaced not add to current badge number.
So what should be the problem with increasing badge when notification arrives.

My php script is :
 

                $passphrase = ‘test123’;

                $message = “from myApp+ Admin”;

                $ctx = stream_context_create();

                stream_context_set_option($ctx, ‘ssl’, ‘local_cert’, ‘myApp.pem’);

                stream_context_set_option($ctx, ‘ssl’, ‘passphrase’, $passphrase);

                $fp = stream_socket_client(

                    ‘ssl://gateway.sandbox.push.apple.com:2195’, $err,

                    $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);

                

                if (!$fp)

                    exit(“Failed to connect: $err $errstr” . PHP_EOL);

                //echo ‘Connected to APNS’ . PHP_EOL;

                

                $body[‘aps’] = array(

                ‘alert’ => $message,

                ‘badge’ => +1,

                ‘sound’ => ‘default’

                

                );

                    $payload = json_encode($body);

                    print_r($payload);

                    $arr1 = array (“your device token1”);

                //foreach($arr1 as $value)

                //{

                    

                    //echo $deviceToken = $value->device_token;

                   

                    $deviceToken = “2f9491d64760c7q5213cf06ff3g6aa8e29r7b5104a00123faa151b92d34023o6”;

                    $msg = chr(0) . pack(‘n’, 32) . pack(‘H*’, $deviceToken) . pack(‘n’, strlen($payload)) . $payload;

                    $result = fwrite($fp, $msg, strlen($msg));

                    /*if (!$result)

                        echo ‘Message not delivered’ . PHP_EOL;

                    else

                        echo ‘Message successfully delivered’ . PHP_EOL;//fwrite($fp, $msg, strlen($msg));*/

                //}

                fclose($fp);

                echo ‘notification sent successfully.’;


Please help me.
Also i try to sending “+2” and “+3”(as a string) , but string is not working.
I think only number value is valid for badge number.

What should i do for increase badge on my app icon , not just replace.
I test it in iPhone 4s with ios 7.0.1 and also ipad with ios 5.1.1

Thank you in advance.

Bhavin

It could be that you are sending the badge number as a number, not a string. The + symbol essentially gets ignored because you are saying it is a positive number (it’s just being used to sign the integer).

See if this works:

$body['aps'] = array( 'alert' =\> $message, 'badge' =\> "+1", 'sound' =\> 'default' );

Edit: sorry I just re-read the last part of your message, you say you’ve already tried setting it as a string. I’ve just looked up incrementing badge numbers using PHP and all the posts on stack overflow seem to say it’s not possible. Everyone says that in order for this to work you would need to store the current badge number on a server, and then calculate the new badge number in your PHP code, and send that as a fixed value.

It could be that you are sending the badge number as a number, not a string. The + symbol essentially gets ignored because you are saying it is a positive number (it’s just being used to sign the integer).

See if this works:

$body['aps'] = array( 'alert' =\> $message, 'badge' =\> "+1", 'sound' =\> 'default' );

Edit: sorry I just re-read the last part of your message, you say you’ve already tried setting it as a string. I’ve just looked up incrementing badge numbers using PHP and all the posts on stack overflow seem to say it’s not possible. Everyone says that in order for this to work you would need to store the current badge number on a server, and then calculate the new badge number in your PHP code, and send that as a fixed value.