[英]How to call didReceiveLocalNotification:(UILocalNotification *)notification in iPhone?
[英]UILocalNotification doesn't trigger didReceiveLocalNotification when tapping from Notification Center
我正在使用Objective-c在我的iPhone應用程序中創建UILocalNotification
。 我的目標是iOS 8並使用XCode 6。
我的問題與在應用未運行且通過點擊通知打開它時處理UILocalNotification
有關。 當用戶點擊該通知,並打開應用程序,我使用didReceiveLocalNotification
在AppDelegate.m
顯示一個特定視圖控制器和發送VC的一些數據(日期)。
從鎖定屏幕中輕按通知時,此方法工作正常。 在通知中心點擊通知時,從不調用didReceiveLocalNotification
。 我使用UIAlertView
在我的設備上對此進行了測試。 didFinishLaunchingWithOptions
被調用,但是當我在該方法中包含用於顯示特定View Controller的代碼時,它將永遠無法工作(盡管另一個UIAlertView
向我展示了它正在運行代碼的那一部分,只是從未完成showViewController
方法)。
這是我的didFinishLaunchingWithOptions
代碼:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if ([UIApplication sharedApplication].scheduledLocalNotifications.count >= 1) {
// Handle local notification received if app wasn't running in background
UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if ([[notification.userInfo objectForKey:@"notification"] isEqual:@"mood rating"]) {
// Create reportVC
NSLog(@"launched app and about to show reportvc");
ReportViewController *reportVC = (ReportViewController *)[self.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:@"reportVC"];
// Date stuff goes here - code removed
// show the reportVC
[self.window.rootViewController showViewController:reportVC sender:self];
}
} else {
[self createLocalNotification];
}
return YES;
}
這是我的didReceiveLocalNotification
代碼:
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
// Create report view
ReportViewController *reportVC = (ReportViewController *)[self.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:@"reportVC"];
// Same date stuff goes here as in didFinishLaunchingWithOptions
// Show report vc
[self.window.rootViewController showViewController:reportVC sender:self];
}
我取出的日期只是檢查它是否在晚上9點之后,然后創建今天或昨天的日期,然后將結果設置為reportVC的屬性。 讓我知道是否相關,我將其重新添加。
所以這是我嘗試解決的一些問題:
我試過使用presentViewController:animated:completion:
而不是showViewController:sender:
但是我想使用showViewController
這樣我就可以顯示導航欄了,但這還是不能解決問題。
我嘗試將這行添加到我的didFinishLaunchingWithOptions
方法中:[self application:application didReceiveLocalNotification:notification]; 確實可以解決此問題-從通知中心點按時,它打開了正確的視圖,但破壞了鎖屏通知。 加上這一行,當從鎖定屏幕上點擊通知時,我得到了兩次reportVC:第一個是除導航欄之外的黑屏,而最上面的是正確的。
我嘗試用以下代碼替換當前的showViewController
代碼行:UIViewController * topController = [UIApplication sharedApplication] .keyWindow.rootViewController; [topController showViewController:reportVC sender:self]; 但這也不能解決問題。
我嘗試從didFinishLaunchingWithOptions
方法中獲取我加倍的代碼,因為我意識到didFinishLaunchingWithOptions
完成后, didReceiveLocalNotification
似乎仍然可以運行。 看起來確實是這樣,但是它不能解決Notification Center通知的問題。
如果這正在搞砸,這是我目前正在測試的方式:
我將這兩行添加到didFinishLaunchingWithOptions
方法中:
[[UIApplication sharedApplication] cancelAllLocalNotifications];
[self createLocalNotification];
我將通知的預定時間更改為大約3分鍾。 通常使用以下日期組件將其設置為每晚晚上9點起飛:
dateComponents.hour = 21;
dateComponents.minute = 0;
我將通知的repeatInterval
更改為NSCalendarUnitMinute
而不是NSCalendarUnitDay
,這是發布版本的設置。
然后,我使用XCode在設備上運行該應用程序,並在其運行並計划通知時間后將其停止。 我沒有這兩行再次運行它:
[[UIApplication sharedApplication] cancelAllLocalNotifications];
[self createLocalNotification];
然后從XCode停止該應用程序,在我的設備上打開多任務,向上滑動該應用程序以將其關閉,然后等待通知到達。 點擊每個測試通知后,我會執行多任務並再次關閉該應用程序,以便每次都可以從完全關閉的應用程序進行測試。
您可能想嘗試一下(與dispatch_async()
異步呈現您的視圖控制器):
if ([[notification.userInfo objectForKey:@"notification"] isEqual:@"mood rating"]) {
dispatch_async(dispatch_get_main_queue(), ^{
// Create reportVC
NSLog(@"launched app and about to show reportvc");
ReportViewController *reportVC = (ReportViewController *)[self.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:@"reportVC"];
// Date stuff goes here - code removed
// show the reportVC
[self.window.rootViewController showViewController:reportVC sender:self];
});
}
或這樣(在顯示您的視圖控制器之前,請調用[self.window makeKeyAndVisible]
):
if ([[notification.userInfo objectForKey:@"notification"] isEqual:@"mood rating"]) {
// Create reportVC
NSLog(@"launched app and about to show reportvc");
ReportViewController *reportVC = (ReportViewController *)[self.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:@"reportVC"];
// Date stuff goes here - code removed
// show the reportVC
[self.window makeKeyAndVisible];
[self.window.rootViewController showViewController:reportVC sender:self];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.