0

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?

Michał Ciuba
  • 7,876
  • 2
  • 33
  • 59
Scooter
  • 4,068
  • 4
  • 32
  • 47
  • what happens when the app is in the foreground and you get a push notification? (_because i don't see any `alertView` code being used_). If you're talking about the Push Notification alert type, then go to the settings and change the app alert type to what you want – staticVoidMan Jan 11 '14 at 16:50
  • In the didReceiveRemoteNotification method above I am displaying an AlertView. I want this to happen when the app is in the background as well. – Scooter Jan 11 '14 at 17:11
  • the AlertView won't appear until the user brings your app to foreground (by clicking on a notification baner) – Michał Ciuba Jan 11 '14 at 17:19
  • That notification banner is the thing I am not receiving. – Scooter Jan 11 '14 at 17:24
  • Did you specify the background modes for your app? – ldindu Jan 11 '14 at 18:04
  • In the Capabilities section of XCode I have Background Modes on, with Background Fetch and Remote Notifications both checked. – Scooter Jan 11 '14 at 20:25
  • One more followup: I worked my way through Ray Wenderlich's tutorial from scratch and the app that resulted worked like a champ. So then I cut and paste that code directly into my app, and of course the behavior still is incorrect. There must be a setting somewhere in the .plist file or something that is different and causing my trouble, but I am really at a loss on this one. – Scooter Jan 13 '14 at 00:18

1 Answers1

0

you may already found a solution, but might be useful for others. People who are targeting iOS7 and up can use

didReceiveRemoteNotification:fetchCompletionHandler

appdelegate method, check the below link it is explained clearly

didReceiveRemoteNotification not working in the background

Community
  • 1
  • 1
harsha yarabarla
  • 506
  • 4
  • 11