2014-10-31 52 views
1

我在開發應用程序IOS時遇到問題,我使用Parse Push Notification Framework推送遠程通知。問題是當應用程序正在運行且通知已發送的同時,應用程序將自動顯示模式警報框。所以,我不希望模態警報顯示。我花了很多時間在網上,我在網上做了研究,看了文檔,但沒有找到任何結果,我覺得沒有人知道這一點。請幫幫我!解析 - 如何在應用程序打開時(前臺)停止模態警報,當收到遠程通知IOS?

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

} 

非常感謝提前。

回答

0

如果你只是想確保你沒有看到,當應用程序被打開通知,在您的application:willFinishLaunchingWithOptions:設置BOOL isOpening爲TRUE,並在application:didFinishingLaunchingWithOptions:其設置爲false,然後更改didReceiveRemoteNotification的行爲:和調用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]; 
    } 
} 
+0

謝謝您的回答。但是,我想要的是禁用,當應用程序打開前景時停止警報對話框。 – Darith 2014-11-03 02:48:12

+0

你能更具體一點嗎?我不完全明白。您想要防止應用程序打開時顯示警報,但它尚未打開? – 2014-11-03 14:34:57

+0

感謝@Ryan Kreager的回覆。是的,我想阻止應用程序打開時顯示的警報。上面的代碼(你的答案)只是檢查應用程序是否活動,我已經知道如何檢查它。但我的問題是當我處理推送[PFPush handlePush:userInfo];應用程序處於活動狀態,並且我發送模態警報顯示的通知。我想消除模態警報。我想要自定義通知應用程序正在運行。 – Darith 2014-11-04 02:31:39

相關問題