user3781953
user3781953

Reputation: 81

AppDelegate is not listening to APNS Push Notification

When my app enters background, AppDelegate is not listening to the new push notification from APNS. But strange thing is that it doesn't happen all the time. Sometimes it works, sometimes it does not. I can't figure out why.

Here is my sample code for listening APNS Notification

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
    {
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIRemoteNotificationTypeBadge
                                                                                             |UIRemoteNotificationTypeSound
                                                                                             |UIRemoteNotificationTypeAlert) categories:nil];
        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
    }
    else
    {
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeSound | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert)];
    }

    if (launchOptions != nil)
    {
        NSDictionary *dictionary = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
        if (dictionary != nil)
        {
        }
    }
    return YES;
}

- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo
{
    NSLog(@"Received notification: %@", userInfo);
    [self application:application didReceiveRemoteNotification:userInfo fetchCompletionHandler:nil];
}

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
    NSLog(@"Received notification background: %@", userInfo);
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 5 * NSEC_PER_SEC),
                   dispatch_get_main_queue(), ^{
                       // Check result of your operation and call completion block with the result
                       completionHandler(UIBackgroundFetchResultNewData);
                   });
}

- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
    [application registerForRemoteNotifications];
}

Upvotes: 0

Views: 545

Answers (2)

Urmi
Urmi

Reputation: 344

Try to change your line..

    if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
    [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeAlert|UIUserNotificationTypeSound categories:nil]];
} else {
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeSound];
}

in iOS 8 you have to use "UIUserNotificationTypeBadge"...

Hope this will work for you..

Upvotes: 0

user4034301
user4034301

Reputation:

In background/closed mode,no code will execute and none of the delegate method will be invoked.This time push notification is handled by OS itself.The app badge is set by push notification payload value coming from server side. If you are not getting app badge in background,it is a badge number issue from server side.Check whether the push notification payload contains application badge field and set to values greater than 0.

This link have same issue and resolved it.

Upvotes: 1

Related Questions