0

I've developed an app and everything works fine except updating the badge when the app is not running.

I receive the push notification but nothing happens with the badge.

The application request the alert and badge type when registering with Apple.

Any ideas? This is driving me crazy.

This is the code I use to send the push:

<?php
$apnsHost = 'gateway.sandbox.push.apple.com';
$apnsCert = '/usr/local/php/cert.pem';
$apnsPort = 2195;

$streamContext = stream_context_create();
stream_context_set_option($streamContext, 'ssl', 'local_cert', $apnsCert);

$apns = stream_socket_client('ssl://' . $apnsHost . ':' . $apnsPort, $error, $errorString, 2, STREAM_CLIENT_CONNECT, $streamContext);

$payload['aps'] = array('alert' => 'Oh hai!', 'badge' => 1);
$output = json_encode($payload);
$token = '1234';
$token = pack('H*', str_replace(' ', '', $token));
$apnsMessage = chr(0) . chr(0) . chr(32) . $token . chr(0) . chr(strlen($output)) . $output;
fwrite($apns, $apnsMessage);

socket_close($apns);
fclose($apns);
grandnasty
  • 739
  • 6
  • 21

2 Answers2

0

You should have a look at the following

  1. Newsstand Module.

  2. applicationIconBadgeNumber property

  3. getApplicationIconBadgeNumber() method

  4. setApplicationIconBadgeNumber() method

Anand
  • 5,323
  • 5
  • 44
  • 58
0

Here is an example which uses push and app badge but as i already said you have to show me your mobile side code too if it does not solve your problem.Here is the code

    Titanium.Network.registerForPushNotifications({  
   types: [  
     Titanium.Network.NOTIFICATION_TYPE_BADGE,  
     Titanium.Network.NOTIFICATION_TYPE_ALERT  
   ],  
   success:function(e)  
   {  
     var deviceToken = e.deviceToken;  
     Ti.API.info("Push notification device token is: "+deviceToken);  
     alert('device token is' +e.deviceToken);  
     Ti.API.info("Push notification types: "+Titanium.Network.remoteNotificationTypes);  
     Ti.API.info("Push notification enabled: "+Titanium.Network.remoteNotificationsEnabled);  
   },  
   error:function(e)  
   {  
     Ti.API.info("Error during registration: "+e.error);  
   },  
   callback:function(e)  
   {  
     // called when a push notification is received.  
    //Titanium.Media.vibrate();  
    var data = JSON.parse(e.data);  
    var badge = data.badge;  
    if(badge > 0){  
     Titanium.UI.iPhone.appBadge = badge;  
    }  
    var message = data.message;  
    if(message != ''){  
     var my_alert = Ti.UI.createAlertDialog({title:'', message:message});  
     my_alert.show();  
    }  
   }  
  });   
 } 

Thanks

Wahhab_mirza
  • 1,484
  • 2
  • 10
  • 17
  • Does the app actually run any code when it's turned off? As far as I understood the OS should deal with badges when the app is not running? – grandnasty Jul 30 '13 at 09:05
  • NO it does not run code but it does this in callback function – Wahhab_mirza Jul 30 '13 at 09:10
  • So, if that is the case, then the source code won't matter? The question is how the badge can be updated when the app is not running? – grandnasty Jul 30 '13 at 14:11
  • Look at the example above i am updating it in the callback function.You should do the same way for updation – Wahhab_mirza Jul 30 '13 at 22:15
  • I understand that concept, but will that code actually run if the application is turned off and the user does not click the notification? – grandnasty Jul 31 '13 at 13:09