0
votes

Hello friends i am new in push notification please help me out. I am trying to get badge count for my app but "NSDictionary userInfo" contains only alert and sound . this is the code for register device for push notification

  -(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{
 if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)])
{
    UIUserNotificationSettings* notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge |UIUserNotificationTypeAlert |  UIUserNotificationTypeSound categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];
    [[UIApplication sharedApplication] registerForRemoteNotifications];
}
else
{
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound)];
}

Now
When i get message from server.

 -(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
    {
    NSLog(@"%@",userInfo);
    if (application.applicationState == UIApplicationStateActive)
    {

        /***********code to show alert********/
        if (![[[NSString alloc]initWithString:[[userInfo objectForKey:@"aps"] objectForKey: @"alert"]] isEqualToString:@""] && [[NSString alloc]initWithString:[[userInfo objectForKey:@"aps"] objectForKey: @"alert"]]!=nil)
        {
            NSString *MSG =[[NSString alloc]initWithString:[[userInfo objectForKey:@"aps"] objectForKey: @"alert"]];
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Message" message:MSG delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil, nil];
            [alert show];
        }

        else
        {
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Message" message:@"Notification Received." delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil, nil];
            [alert show];
        }
    }
        application.applicationIconBadgeNumber =[[[userInfo objectForKey:@"aps"] objectForKey: @"badge"]integerValue];

    if (userInfo)
    {
        NSLog(@"%@",userInfo);

        if ([userInfo objectForKey:@"aps"])
        {
            if([[userInfo objectForKey:@"aps"] objectForKey:@"badgecount"])
            {
                [UIApplication sharedApplication].applicationIconBadgeNumber = [[[userInfo objectForKey:@"aps"] objectForKey: @"badgecount"] intValue];
            }
        }
    }

    }

Now output is

{ aps =     
    {
        alert = "MAgic 2 SAlon App!";
        sound = default;
    };
}

my php code is `

// Put your device token here (without spaces):
$deviceToken = 'xxxxxxxxx';

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

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

$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'
);

// 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);`
1

1 Answers

0
votes

There is no problem with your iOS code so far, but in your php script you are missing the badge count.


In all iOS apps, unread notification counts are maintained from backend/server. When the backend/server sends a push notification to a particular device then server sends the badge count along with the payload.

So I guess the back end is not sending you the badge count.

The backend/server team should follow this format, so that you can get badge count programatically:-

{
    "aps" :  
    {
        "alert" : "msg",
        "badge" : 1 ,
        "sound" : "demo.aiff"
    }
}

Here are tutorial link, how to add badge count in php Script:-

http://b2cloud.com.au/tutorial/ios-push-notifications-in-php/

http://learn-php-by-example.blogspot.in/2013/01/working-with-apple-push-notification.html