Ios UILocalNotification不';从通知中心点击时,t触发器未收到本地通知
我正在使用Objective-c在我的iPhone应用程序中创建Ios UILocalNotification不';从通知中心点击时,t触发器未收到本地通知,ios,objective-c,iphone,notifications,uilocalnotification,Ios,Objective C,Iphone,Notifications,Uilocalnotification,我正在使用Objective-c在我的iPhone应用程序中创建UILocalNotification。我的目标是iOS 8和XCode 6 我的问题与处理UILocalNotification有关,当应用程序未运行且通过点击通知打开时。当用户点击通知并打开应用程序时,我使用AppDelegate.m中的didReceiveLocalNotification来显示特定的视图控制器并向VC发送一些数据(日期) 当点击锁屏上的通知时,此功能可以正常工作。点击通知中心中的通知时,不会调用didRece
UILocalNotification
。我的目标是iOS 8和XCode 6
我的问题与处理UILocalNotification
有关,当应用程序未运行且通过点击通知打开时。当用户点击通知并打开应用程序时,我使用AppDelegate.m
中的didReceiveLocalNotification
来显示特定的视图控制器并向VC发送一些数据(日期)
当点击锁屏上的通知时,此功能可以正常工作。点击通知中心中的通知时,不会调用didReceiveLocalNotification。我使用UIAlertView
在我的设备上测试了这一点didFinishLaunchingWithOptions
被调用,但当我在该方法中包含显示特定视图控制器的代码时,它永远不会工作(尽管另一个UIAlertView
向我显示它正在运行该部分代码,只是从来没有完成showViewController
方法)
以下是我的didfishlaunchingwithoptions
code:
- (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;
}
- (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];
}
这是我的direceivelocalnotification
code:
- (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;
}
- (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
- 我已尝试将此行添加到我的
方法: [自应用程序:应用程序收到本地通知:通知]; 当从通知中心点击时,它打开了正确的视图,但它破坏了锁屏通知。添加了这一行后,当点击锁屏上的通知时,我得到了两次reportVC:第一次是除了导航栏之外的一个黑屏,最上面的一个是正确的didfishlaunchingwithoptions
- 我尝试用以下代码替换当前的
代码行: UIViewController*topController=[UIApplication sharedApplication].keyWindow.rootViewController; [topController showViewController:reportVC发送者:self]; 但这也没有解决问题showViewController
- 我试着从我的
方法中取出我加倍使用的代码,因为我意识到didfishlaunchingwithoptions
完成后,didfishlaunchingwithoptions
似乎无论如何都会运行。看起来确实如此,但它并不能解决我在通知中心通知方面的问题didcreceivelocalnotification
didfishlaunchwithoptions
方法中:
[[UIApplication sharedApplication] cancelAllLocalNotifications];
[self createLocalNotification];
我将通知的预定时间更改为未来3分钟左右。它通常设置为每晚9点,使用如下日期组件:
dateComponents.hour = 21;
dateComponents.minute = 0;
我将通知的repeatInterval
更改为NSCalendarUnitMinute
,而不是为发布版本设置的NSCalendarUnitDay
然后,我使用XCode在我的设备上运行应用程序,并在它运行并计划通知后停止它。我在没有这两行的情况下再次运行它:
[[UIApplication sharedApplication] cancelAllLocalNotifications];
[self createLocalNotification];
然后从XCode停止应用程序,在我的设备上打开多任务处理,向上滑动应用程序以关闭它,然后等待通知到达。点击每个测试通知后,我将执行多任务并再次关闭应用程序,以便每次都可以从完全关闭的应用程序中进行测试。您可能希望尝试此操作(使用dispatch\u async()
异步显示视图控制器):
或者(在显示视图控制器之前,请调用[self.window makeKeyAndVisible]
):
谢谢最后,我在我的
didfishlaunchingwithoptions
方法中使用了您的dispatch\u async
建议。它适用于我在通知中心点击的通知,但这意味着每个锁屏通知都会显示reportVC两次,因为这些通知也会调用didReceiveLocalNotification
。我刚刚删除了我的direceivelocalnotification
实现,并结合您的建议,它似乎正在发挥作用。编辑:当出现通知并且应用程序在后台运行时,现在不会发生任何事情。回到绘图板上。@bellebethcooper,如果我的帖子回答了你原来的问题,那么你能把它标记为已接受吗?非常感谢。