推送通知的后台处理

时间:2015-04-27 07:02:29

标签: ios objective-c sqlite swift push-notification

当iPhone应用程序完全关闭时,即使在后台也不是,那么APNS如何处理。 例如当应用程序完全关闭时,将APNS数据存储在sqlite中。

1 个答案:

答案 0 :(得分:1)

在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];
}

在所有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
}