I have added remote notifications to my app, but it only works when the app is in the foreground. When the app is running in the background and I make the push nothing happens.
My code in the appDelegate is as follows:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
NSLog(@"Registering for push notifications...");
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
return YES;
}
-(void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
NSString*str =[NSString stringWithFormat:@"Device Token=%@",deviceToken];
NSLog(@"%@",str);
}
-(void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError*)err
{
NSString*str =[NSString stringWithFormat: @"Error: %@", err];
NSLog(@"%@",str);
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSDictionary *messageBundleReceived = [userInfo objectForKey:@"CLS-Alert"];
NSString *messageReceived = [messageBundleReceived objectForKey:@"alert"];
NSString *badgeNumberReceived = [messageBundleReceived objectForKey:@"badge"];
int badgeNumber = badgeNumberReceived.intValue;
UIAlertView *remoteAlert = [[UIAlertView alloc] initWithTitle:@"Attention!!"
message:messageReceived
delegate:self cancelButtonTitle:@"Ok"
otherButtonTitles:nil, nil];
[remoteAlert show];
[UIApplication sharedApplication].applicationIconBadgeNumber = badgeNumber;
}
I am using PushMeBaby to perform the push, and have taken care of all the setup such as certificates, and provisioning the device as evidenced by the fact it does receive the push when in the foreground. Every option is turned on in Notification Center as well.
Any ideas as to why the alertView is not popping up when the app is in the background?