2012-11-15 32 views
12

我希望我的應用在通過點擊通知啓動應用時執行特定的操作。我想在應用程序已經運行到後臺時執行這些特定的事情,但是當通過點擊通知啓動應用程序從SCRATCH(不運行到後臺)時,我也會執行這些特定的事情。XCode:爲什麼didFinishLaunchingWithOptions中的launchOptions始終爲零?

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification 

=>沒問題:

當應用程序從後臺通過在通知點擊開始,我通過得到通知!

當應用程序是從頭開始通過在通知點擊開始,我想通過得到通知:

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

    UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey]; 

} 

但launchOptions始終是零!當應用程序從頭開始通過點擊應用程序圖標(正常),而且通過點擊通知(非正常)從頭開始時,該應用程序爲零。

任何人都知道如何解決這個問題?

謝謝!

編輯1

這裏是如何我通知創建(喬的問題):(!回答我的問題:))

NSDictionary *userInfo = [NSDictionary dictionaryWithObjects: 
[NSArray arrayWithObjects:notifText,latitudeString,longitudeString,nil] 
forKeys:[NSArray arrayWithObjects:@"notifText",@"latitude",@"longitude", nil]]; 

    UILocalNotification *localNotif = [[UILocalNotification alloc] init]; 
    localNotif.fireDate = itemDate; 
    localNotif.timeZone = [NSTimeZone defaultTimeZone]; 
    localNotif.alertBody =msg; 
    localNotif.alertAction = @"Ok"; 
    localNotif.soundName = UILocalNotificationDefaultSoundName; 
    localNotif.applicationIconBadgeNumber = 1; 
    localNotif.userInfo = userInfo; 

    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif]; 
    [localNotif release]; 

EDIT 2

下面是該過程我用來調試我的應用程序,但...這個程序是錯誤的!

  1. 設置我的調試器「等待MyApp.app推出」中的「編輯計劃」菜單
  2. 我開始了我與XCode的應用程序第一時間選項(從頭開始推出)的XCode顯示「等待我的MyApp啓動」=>我點擊我的應用程序圖標啓動應用程序
  3. 該應用程序啓動=>我點擊主頁按鈕=>通知顯示
  4. 我點擊停止按鈕在XCode關閉應用程序後,我用XCode重新啓動它=> XCode再次顯示「正在等待我的MyApp啓動」消息=>點擊通知中的 狀態欄啓動應用程序
  5. => launchOptions爲零!

launchOptions等於零是由於這樣的事實,重振與Xcode中的應用程序(在這種情況下,與「等待我的MyApp的啓動」選項)刪除通知,即使是在狀態欄仍顯示...

爲了能夠在通過點擊通知從頭開始重新啓動應用程序後檢查launchOptions的內容是什麼,似乎唯一的方法是在提到的UIAlert中顯示此內容由Tammo Freese回答。因此,請使用以下方式在此特定情況下進行調試:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"options" message:[launchOptions[UIApplicationLaunchOptionsLocalNotificationKey] description] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
    [alertView show]; 
    return YES; 
} 

謝謝大家的幫助!

+0

您可以將您的通知有效載荷到你的問題? – Joe

+0

謝謝你的回答喬。我添加了如何在我的問題中創建通知。希望這會幫助你幫助我:) –

+0

嗨@Regis_AG,即使在使用「等待應用程序啓動」時,我也可以獲得本地通知 - 請參閱下面的答案編輯。 –

回答

19

我無法找到代碼中的錯誤你共享。通常這應該可以在模擬器和設備上使用。下面是一個適用於我的例子:首先生成一個新的Single View iPhone應用程序(ARC和Storyboards)。然後更改AppDelegate兩種方法如下:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"options" message:[launchOptions[UIApplicationLaunchOptionsLocalNotificationKey] description] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
    [alertView show]; 
    return YES; 
} 

- (void)applicationDidEnterBackground:(UIApplication *)application 
{ 
    [[UIApplication sharedApplication] cancelAllLocalNotifications]; 

    UILocalNotification *localNotif = [[UILocalNotification alloc] init]; 
    localNotif.fireDate = [NSDate dateWithTimeInterval:10.0 sinceDate:[NSDate date]]; 
    localNotif.timeZone = [NSTimeZone defaultTimeZone]; 
    localNotif.alertBody = @"Just some text"; 
    localNotif.alertAction = @"OK"; 
    localNotif.soundName = UILocalNotificationDefaultSoundName; 
    localNotif.applicationIconBadgeNumber = 1; 
    localNotif.userInfo = @{@"test": @YES}; 

    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif]; 
} 

然後啓動這個程序,按home鍵,然後停止應用程序。如果你上談到在10秒內按下home鍵後,本地通知挖掘,你會看到這樣的事情,這說明本地通知已傳遞到-application:didFinishLaunchingWithOptions:

screenshot of the iPhone Simulator showing an alert view that displays a local notification

我的建議是:一得到我上面發佈的例子,以確保你的設置沒有任何問題,然後檢查你在代碼中做了什麼不同。

編輯

這適用於編輯的問題2:本地通知似乎也爲我工作,等待時的應用程序啓動(在模擬器,而不是設備上)。試試這個:

  1. 安裝上面描述的示例應用程序,並啓動它,並啓用「等待MyApp.app啓動」禁用。
  2. 點擊主頁按鈕,然後通過Xcode中或通過任務欄停止應用程序。
  3. 啓用「等待MyApp.app啓動」。
  4. 如果你現在挖掘的通知中心的通知,它顯示在警報視圖。
+0

感謝您的回答Tammo!感謝您的想法,顯示UIAlert以在啓動時查看通知的內容,我發現爲什麼我做錯了! :)問題是我在「編輯方案」菜單中設置了我的調試器,其中包含「等待MyApp啓動」。我這樣做是爲了能夠在點擊通知後從頭開始調試應用程序時調試我的應用程序,但是...此過程會在啓動時刪除所有通知!我將編輯我的問題以更清楚地解釋。非常感謝你的幫助!! –

0

請原諒我,我還是新的,當涉及到蘋果的推送通知服務,但我沒有通過他們的文檔一些閱讀,我的猜測是,有可能是一些在創建本地通知引起該問題。

我會仔細檢查你是如何與蘋果的實例創建本地通知,如清單2-1所示here只是爲了排除這種可能性。由於用於創建本地通知的一些代碼在此線程中未顯示給我們,因此難以評估本地通知的實例是否確實正確。它可能最終會像啓動日期或通知中的其他內容設置不正確那樣簡單。

+0

感謝您的回答。這不是通知實例的問題,但問題是由於我調試的方式。看到我的答案。還是要謝謝你的幫助。 –

+0

不客氣。很高興聽到你能解決這個問題。乾杯。 –

1

不知道這是否是你尋找什麼,但是否缺少「返回YES;」? 因爲我用這個來執行SEGUE從一個通知一個新的觀點,它工作正常

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    UINavigationController *navigation = (UINavigationController *) self.window.rootViewController; 

    [navigation.visibleViewController performSegueWithIdentifier:@"Ident" sender:nil]; 

    return YES; 
} 
+0

感謝您的回答。其實,YES是在我的代碼中(我只是想把它複製到我的問題中)。問題最終是由於我調試的方式。看到我的答案。還是要謝謝你的幫助。 –

1

我的結論和建議,如果它可以HEP任何人,請你有新的版本來測試您的應用程序每次下面

,必須測試與最新的應用程序生成的通知的通知點擊動作。如果你繼續與年長構建生成然後它會表現得出乎意料的舊通知點擊動作檢測(指以某種方式其能夠啓動應用程序,但它不會回報你在didFinishLaunchingWithOptions任何有效信息:)

相關問題