收到推播時有兩種狀況:
- 前景 (Foreground)
- 背景 (Background)
我現在想只在背景收到推播時,使用者點進來再做對應處理,不然使用者在前景已經處理過的任務,後到的推播再來做一次,就不是很合理囉~
/** Theme: Detect Foreground or Background IDE: Xcode 7 Language: Objective C Date: 104/09/28 Author: HappyMan Blog: https://cg2010studio.wordpress.com/ */ - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { // 先判斷前景或背景收到,因為推播太慢(可能要30s)收到 if ([AppDelegate runningInForeground]) { return; } } +(BOOL)runningInBackground { UIApplicationState state = [UIApplication sharedApplication].applicationState; BOOL result = (state == UIApplicationStateBackground); return result; } +(BOOL)runningInForeground { UIApplicationState state = [UIApplication sharedApplication].applicationState; BOOL result = (state == UIApplicationStateActive); return result; }
[UIApplication sharedApplication].applicationState
- UIApplicationStateActive
- UIApplicationStateInactive
- UIApplicationStateBackground
Inactive我就不太曉得什麼狀況用得上。
參考:iOS how to judge application is running foreground or background?。
隨意留個言吧:)~