我希望我的應用在通過點擊通知啓動應用時執行特定的操作。我想在應用程序已經運行到後臺時執行這些特定的事情,但是當通過點擊通知啓動應用程序從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
下面是該過程我用來調試我的應用程序,但...這個程序是錯誤的!
- 設置我的調試器「等待MyApp.app推出」中的「編輯計劃」菜單
- 我開始了我與XCode的應用程序第一時間選項(從頭開始推出)的XCode顯示「等待我的MyApp啓動」=>我點擊我的應用程序圖標啓動應用程序
- 該應用程序啓動=>我點擊主頁按鈕=>通知顯示
- 我點擊停止按鈕在XCode關閉應用程序後,我用XCode重新啓動它=> XCode再次顯示「正在等待我的MyApp啓動」消息=>點擊通知中的 狀態欄啓動應用程序
- => 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;
}
謝謝大家的幫助!
您可以將您的通知有效載荷到你的問題? – Joe
謝謝你的回答喬。我添加了如何在我的問題中創建通知。希望這會幫助你幫助我:) –
嗨@Regis_AG,即使在使用「等待應用程序啓動」時,我也可以獲得本地通知 - 請參閱下面的答案編輯。 –