NewObjective
NewObjective

Reputation: 69

Push notification badge increase and php script

I send push notification to my device with php script from my server. The script code is

<?php    
// Put your device token here (without spaces):
$deviceToken = 'a14b6212fa69a2b1c2dde4547a50c711fd40b9787cc029800584890d72a9f5db';

// Put your private key's passphrase here:
$passphrase = 'ujyfljnhjgby123';

// Put your alert message here:
$message = 'Delivery 33 message!';

////////////////////////////////////////////////////////////////////////////////

$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);

// Open a connection to the APNS server
$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;

// Create the payload body
$body['aps'] = array(
'badge' => +1,
'alert' => $message,
'sound' => 'default'
);

// Encode the payload as JSON
$payload = json_encode($body);

// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) .       $payload;

// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));

if (!$result)
echo 'Message not delivered' . PHP_EOL;
else
echo 'Message successfully delivered' . PHP_EOL;

// Close the connection to the server
fclose($fp);

In my AppDelegate.m there is such code

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
[UIApplication sharedApplication].applicationIconBadgeNumber = [[[userInfo objectForKey:@"aps"] objectForKey: @"badgecount"] intValue];

 }

The question is - why number in my badge is not updating when I receive more than one notification? Looks like its just replace

'badge' => +1,

each time. What am I doing wrong? Can you please help? Thank you

Upvotes: 3

Views: 5816

Answers (4)

souvickcse
souvickcse

Reputation: 7804

You have to do it from the server side. In my case I have done it through php and mysql. Here is my database enter image description here

I have added a field badgecount and i increase the badge count every time i send the push to the device with this code

    $query = "SELECT badgecount FROM pushnotifications WHERE device_token = '{$device_token}'";
    $query = $this->db->query($query);
    $row = $query->row_array();
    $updatequery = "update pushnotifications set badgecount=badgecount+1 WHERE device_token ='{$device_token}'";
    $updatequery = $this->db->query($updatequery);
    $device = $device_token;
    $payload['aps'] = array('alert' => $pushmessage, 'badge' =>$row["badgecount"]+1, 'sound' => 'default');
    $payload = json_encode($payload); 

And I also make another api for making the badgcount 0 which is called in the

- (void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo

So when the notification is seen it is again zero in the server.

Upvotes: 3

Brad Moore
Brad Moore

Reputation: 356

I am not 100% sure on this, but +1 will just say 1, I think you need '+1'.

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

EDIT: Actually, this may just be for UrbanAirship now that I think about it...

Upvotes: 0

Rengers
Rengers

Reputation: 15218

You can not pass relative values for badge in the payload. +1 will simply become 1. If you want to increment or decrement you will need to keep track of the current badge number on your server, and pass the new absolute value in the payload.

Upvotes: 1

AppleDelegate
AppleDelegate

Reputation: 4249

First register for notification with the following method in the appDelegate.m

  - (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devicetoken { 

Check the reponse here

}

Upvotes: -1

Related Questions