Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/102.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UILocalNotification不';从通知中心点击时,t触发器未收到本地通知_Ios_Objective C_Iphone_Notifications_Uilocalnotification - Fatal编程技术网

Ios UILocalNotification不';从通知中心点击时,t触发器未收到本地通知

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

我正在使用Objective-c在我的iPhone应用程序中创建
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
    这样我就可以显示导航栏,但这并没有解决问题

  • 我已尝试将此行添加到我的
    didfishlaunchingwithoptions
    方法: [自应用程序:应用程序收到本地通知:通知]; 当从通知中心点击时,它打开了正确的视图,但它破坏了锁屏通知。添加了这一行后,当点击锁屏上的通知时,我得到了两次reportVC:第一次是除了导航栏之外的一个黑屏,最上面的一个是正确的

  • 我尝试用以下代码替换当前的
    showViewController
    代码行: UIViewController*topController=[UIApplication sharedApplication].keyWindow.rootViewController; [topController showViewController:reportVC发送者:self]; 但这也没有解决问题

  • 我试着从我的
    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,如果我的帖子回答了你原来的问题,那么你能把它标记为已接受吗?非常感谢。