Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Push notification badge increase and php script

Tags:

iphone

badge

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

like image 844
NewObjective Avatar asked Sep 21 '26 13:09

NewObjective


2 Answers

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.

like image 147
souvickcse Avatar answered Sep 24 '26 01:09

souvickcse


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.

like image 37
Rengers Avatar answered Sep 24 '26 03:09

Rengers



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!