0

I'm implementing push notifications and they are received correctly if the application is in foreground ,didReceiveRemoteNotification is called with the data. So I think token and server problems are nil. When application is in background is when it gets ugly: I send a notification and never is shown as received at Notification Center, not badge displayed. In Settings/Notifications/MyApp everything is active. Maybe is because I'm using development certificates or because an Apple's sandbox issue? Any idea will be appreciated. Thank you

Carlos Pastor
  • 531
  • 1
  • 4
  • 18
  • I see same problem here http://stackoverflow.com/questions/7965417/ios-push-notification-alert-is-not-shown-when-the-app-is-running – Elto Jul 11 '13 at 20:51
  • Is not the same problem, linked one refers to PN not appearing when app is running in foreground and this one is when it is running in background, or not running at all. But thanks anyway. – Carlos Pastor Jul 12 '13 at 14:04

2 Answers2

2

Fixed. When creating the payload I was using a simple array, not an array with ['aps'] key. Server-side problem. I don't know why Apple is sending notifications not well formed when documentation says it won't. That detail make me thought server-side was ok and that's why I didn't paste the code, sorry for that.

Wrong:

  $payload = array('alert' => $this->code->getText(),
                   'sound' => 'default', 
                   'badge' => '1');
  $message['payload'] = json_encode($payload);

Right:

$body['aps'] = array(
                'alert' => $this->code->getText(),
                'sound' => 'default',
                'badge' => '1'
            );
            $message['payload'] = json_encode($body);

And sending code...

  if ($this->sendNotification($message['device_token'], 
                              $message['payload'])) {       
  } else {  // failed to deliver
      $this->reconnectToAPNS();
     }
Carlos Pastor
  • 531
  • 1
  • 4
  • 18
0

By your description I am assuming you are receiving APNs (Apple Push Notifications) some of the time. Double check your code in your AppDelegate and see if you have the following:

In your - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

you should have

[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];

In your didReceiveRemoteNotification try this code to see what happens when you receive an APN:

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
NSLog(@"remote notification: %@",[userInfo description]);
NSDictionary *apsInfo = [userInfo objectForKey:@"aps"];

NSString *alert = [apsInfo objectForKey:@"alert"];
NSLog(@"Received Push Alert: %@", alert);

NSString *sound = [apsInfo objectForKey:@"sound"];
NSLog(@"Received Push Sound: %@", sound);

NSString *badge = [apsInfo objectForKey:@"badge"];
NSLog(@"Received Push Badge: %@", badge);
}

and lastly make sure you are actually successfully registering to receive APNs:

- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken {
NSLog(@"My token is: %@", deviceToken);
}

include this code in case you get an error registering for APNs:

- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error {
NSLog(@"Failed to get token, error: %@", error);
}
sangony
  • 11,636
  • 4
  • 39
  • 55
  • Everything double-checked and working correctly when app is in foreground. Thanks for your time and effort. – Carlos Pastor Jul 11 '13 at 21:01
  • Do you have code on how to handle an incoming APN? Remember that if your app is in the background, only the sound plays and badge count is updated. Once your app is in the foreground you will have to handle any updates/synchronization server side. – sangony Jul 11 '13 at 21:12
  • Yes, I know and I am requesting server updates for that client when the push is received, but the problem is that is supposed to appear at the phone a notification (in notification center slider), an alert (with user assigned style at settings app/notifications), sound and badge and none are appearing when app is in background so user never realizes there is something new (and when he opens the app it'll request an update to the server). – Carlos Pastor Jul 11 '13 at 23:44
  • That sounds very much like the app does not have permission to show notifications. Take a look in your iPhone/iPad's notifications center to see if your app is allowed/included to receive APNs. Remember that if the user does NOT grant permission for notifications when your app first launches, only a manual change in the notification center can reverse that. – sangony Jul 12 '13 at 00:11
  • I understand you are refering to "Settings/Notifications/MyApp" and eveything here is active. Also it doesn't make sense because when it's in foreground notifications are arriving, so permissions should be ok. – Carlos Pastor Jul 12 '13 at 12:17