Reputation: 579
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
Upvotes: 0
Views: 2158
Reputation: 579
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();
}
Upvotes: 2
Reputation: 11696
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);
}
Upvotes: 0