如果应用在推送通知上处于活动状态

时间:2012-07-10 13:57:39

标签: iphone xcode apple-push-notifications

发送推送通知后,我希望应用程序在我的收件箱页面上打开,以便用户可以在收件箱中看到新消息。我的AppDelegate中有以下代码

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{

    [self.tabController setSelectedIndex:2]; 
}

当我发送推送消息并且电话处于活动状态时,它会直接在那里发出警告。我知道它应该用我的代码做什么但是有办法改变它就像这样

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{

    if(app.isActive == FALSE)
          [self.tabController setSelectedIndex:2]; 
}

对此的任何见解都将有很大帮助。谢谢!

3 个答案:

答案 0 :(得分:5)

来自Stanislav Yaglo,Objective C: How to check if application is currently active (i.e. user is using it)?

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {

    if (application.applicationState == UIApplicationStateActive) {
        // For example, update the tab bar
    }
    else {
        self.tabController.selectedIndex = 3;
    }
}

希望它有所帮助!

答案 1 :(得分:1)

进入应用程序后,您可以将bool设置为YES,只需将bool设置为:

- (void)applicationWillEnterForeground:(UIApplication *)application

并将bool设置为

中的NO
- (void)applicationDidEnterBackground:(UIApplication *)application

答案 2 :(得分:1)

您可以通过检查applicationState来判断应用程序是否在前台:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    if ( application.applicationState == UIApplicationStateActive )
        // app is in the foreground
    else
        // app was just brought from background to foreground
    ...
}