Background handling of Push Notification

By : Gupta
Source: Stackoverflow.com
Question!

When iPhone app is closed totally i.e. not even in background, then how APNS handling can be done. e.g. Store APNS Data in sqlite when app is completely closed.

By : Gupta


Answers

in AppDelegate:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

    if (launchOptions)
    { //launchOptions is not nil

        NSDictionary *userInfo = [launchOptions valueForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
        NSDictionary *apsInfo = [userInfo objectForKey:@"aps"];
        userInfoDic = userInfo;

        if (apsInfo)
        { //apsInfo is not nil
            [self performSelector:@selector(postNotificationToPresentPushMessagesVC)
                       withObject:nil
                       afterDelay:1];
        }
    }

    return YES;
}

-(void)postNotificationToPresentPushMessagesVC
{    
    [[NSNotificationCenter defaultCenter]postNotificationName:@"recievePush" object:userInfoDic];
}

in all VC:

- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(recievePush:) name:@"recievePush"  object:nil];

}

- (void) recievePush : (NSNotification *) notif
{
    NSDictionary *dict = notif.object;

// do something with message data
}


This video can help you solving your question :)
By: admin