解析 - 当收到远程通知IOS时,如何在app打开(前台)时停止模态警报?

时间:2014-10-31 07:07:47

标签: ios notifications parse-platform push

我在开发应用程序IOS时遇到问题,我使用Parse Push Notification Framework进行推送远程通知。问题是当应用程序运行并且通知发送的同时,应用程序自动显示模态警报框。所以,我不希望模态警报显示。我花了很多时间在上面,我在互联网上做研究,阅读文件但没有找到结果,我觉得没有人知道这件事。请帮帮我!

AppDelegate.m

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

{

    // Override point for customization after application launch.

        UIImage *navBackgroundImage = [UIImage imageNamed:@"nav_bg_new"];

    [[UINavigationBar appearance] setBackgroundImage:navBackgroundImage forBarMetrics:UIBarMetricsDefault];



    [[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];

    // ****************************************************************************

    // Uncomment and fill in with your Parse credentials:

    // [Parse setApplicationId:@"your_application_id" clientKey:@"your_client_key"];

    // ****************************************************************************

    [Parse setApplicationId:@"my_app_id" clientKey:@"my_client_key"];

    // Override point for customization after application launch.

    [application registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeSound];





    return YES;

}


- (void)applicationWillResignActive:(UIApplication *)application

{

    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.

    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.

}

- (void)applicationDidEnterBackground:(UIApplication *)application

{

    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 

    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

}

- (void)applicationWillEnterForeground:(UIApplication *)application

{

    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.

}



- (void)applicationDidBecomeActive:(UIApplication *)application

{

    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

}

- (void)applicationWillTerminate:(UIApplication *)application

{

    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.

}



- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {

    PFInstallation *currentInstallation = [PFInstallation currentInstallation];

    [currentInstallation setDeviceTokenFromData:deviceToken];

    currentInstallation.channels = @[@"global"];

    [currentInstallation saveInBackground];

}

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {

    if (error.code == 3010) {

        NSLog(@"Push notifications are not supported in the iOS Simulator.");

    } else {

        // show some alert or otherwise handle the failure to register.      NSLog(@"application:didFailToRegisterForRemoteNotificationsWithError: %@", error);

    }

}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {    
    [PFPush handlePush:userInfo];

}

提前非常感谢。

1 个答案:

答案 0 :(得分:0)

如果您只是想确保在应用程序打开时没有看到通知,请在application:willFinishLaunchingWithOptions:中将BOOL isOpening设置为TRUE并在application:didFinishingLaunchingWithOptions:期间将其设置为FALSE,然后更改行为ofReReiveRemoteNotification:以及相应地调用PFPush handlePush:

你可以试试这个:

@property (nonatomic, assign) BOOL isLaunching;

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    isLaunching = TRUE;
}

- (void)applicationDidEnterForeground:(UIApplication *)application
{
    isLaunching = FALSE;
}

- (void)application:(UIApplication *)application    didReceiveRemoteNotification:(NSDictionary *)userInfo {
    if (!isLaunching) {
        //Only fire the push handler if the application isn't active
        [PFPush handlePush:userInfo];
    }
}