am working on push notifications
app but badge count
is not increasing when notification comes.
i have saw so many examples in stack overflow
but no one is useful.
Can anyone suggest me how to resolve this problem... thanks in advance!
My Server Side PHP Code:
<?php
// Put your device token here (without spaces):
$deviceToken = 'c0d35d5f5ab179f0a93cb7c6b89b96b305ad3517b24e454abd4517e2323f4a7a';
// Put your private key's passphrase here:
$passphrase = '12345push';
// Put your alert message here:
$message = 'My First push notification!';
//$badge = 3;
////////////////////////////////////////////////////////////////////////////////
$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(
'alert' => $message,
'sound' => 'default',
'badge'=>($badge != (null) ? $badge + 1 : 1)
);
// 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 appdelegate.m
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSString* alertValue = [[userInfo valueForKey:@"aps"] valueForKey:@"badge"];
NSLog(@"my message-- %@",alertValue);
int badgeValue= [alertValue intValue];
[UIApplication sharedApplication].applicationIconBadgeNumber += badgeValue;
}
Usually in all apps, the unread notification counts are maintained in the server. When the server sends a push notification to a particular device token server sends the badge count along with the payload.
Your server logic needs to keep track of the proper badge count and send it appropriately.
{
"aps" :
{
"alert" : "Your notification message",
"badge" : badgecount ,
"sound" : "bingbong.aiff"
}
}
EDIT
You have set badge count in didReceiveRemoteNotification
method. before this method called appbadge
is set from pushnotification, so from server you have to set correct badge..
Solution:
so create some webservice send deviceToken and currentBadge in that webservice to store at server, and when next time you send push check the last badge value for the token, and send it.
You have to manage your server side when you send a notification to other user then from php side they set a counter to increment the badge & send the notification to other user after that when you open your app the badge is set to null like this :
- (void) applicationWillResignActive:(UIApplication *) application
{
application.applicationIconBadgeNumber = 0;
}
& to receive the notification you set this code :
- (void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSLog(@"userInfo %@",userInfo);
for (id key in userInfo)
{
NSLog(@"key: %@, value: %@", key, [userInfo objectForKey:key]);
}
[application setApplicationIconBadgeNumber:[[[userInfo objectForKey:@"aps"] objectForKey:@"badge"] intValue]];
NSLog(@"Badge %d",[[[userInfo objectForKey:@"aps"] objectForKey:@"badge"] intValue]);
NSString *message = [[userInfo objectForKey:@"aps"] objectForKey:@"alert"];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Test" message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
You have to do it from the server side. In my case I have done it through php and mysql. Here is my database
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With