【发布时间】:2015-01-14 00:21:02
【问题描述】:
我正在使用 Objective-c 在我的 iPhone 应用程序中创建一个 UILocalNotification。我的目标是 iOS 8 并使用 XCode 6。
我的问题与处理UILocalNotification 相关,当应用程序未运行 并且它是通过点击通知打开的。当用户点击通知并打开应用程序时,我使用AppDelegate.m 中的didReceiveLocalNotification 来显示特定的视图控制器并向VC 发送一些数据(日期)。
从锁屏点击通知时效果很好。在通知中心点击通知时,didReceiveLocalNotification 永远不会被调用。我使用UIAlertView 在我的设备上进行了测试。 didFinishLaunchingWithOptions 被调用,但是当我在该方法中包含显示特定视图控制器的代码时,它永远不会起作用(尽管另一个 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方法中: [自我申请:申请didReceiveLocalNotification:通知]; 这确实解决了这个问题——当从通知中心点击时,它打开了正确的视图,但它破坏了锁屏通知。添加这一行后,当从锁屏点击通知时,reportVC 出现了两次:第一个是除了导航栏之外的黑屏,而最上面的那个是正确的。我尝试用以下代码替换我当前的
showViewController代码行: UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController; [topController showViewController:reportVC sender:self]; 但这也没有解决问题。我尝试从我的
didFinishLaunchingWithOptions方法中取出我加倍的代码,因为我意识到一旦didFinishLaunchingWithOptions完成,didReceiveLocalNotification似乎无论如何都会运行。情况似乎确实如此,但它并不能解决我的通知中心通知问题。
如果这把事情搞砸了,我现在是这样测试的:
我将这两行添加到didFinishLaunchingWithOptions 方法中:
[[UIApplication sharedApplication] cancelAllLocalNotifications];
[self createLocalNotification];
我将通知的预定时间更改为大约 3 分钟后。它通常设置为每晚 9 点,使用这样的日期组件:
dateComponents.hour = 21;
dateComponents.minute = 0;
我将通知的repeatInterval 更改为NSCalendarUnitMinute,而不是NSCalendarUnitDay,这是为发布版本设置的。
然后我使用 XCode 在我的设备上运行该应用程序,并在它运行并安排通知后停止它。我在没有这两行的情况下再次运行它:
[[UIApplication sharedApplication] cancelAllLocalNotifications];
[self createLocalNotification];
然后从 XCode 中停止应用程序,在我的设备上打开多任务处理,向上滑动应用程序以将其关闭,然后等待通知到达。在点击每个测试通知后,我会执行多项任务并再次关闭应用程序,这样我每次都可以从完全关闭的应用程序中进行测试。
【问题讨论】:
标签: ios objective-c iphone notifications uilocalnotification