HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – 从通知中心点击时,UILocalNotification不会触发didReceiveLocalNotification大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用 Objective-c在我的iPhone应用程序中创建UIlocalnotification.我的目标是iOS 8并使用XCode 6. @H_618_5@ @H_618_5@我的问题涉及在app未运行时处理UIlocalnotification并通过点击通知打开它.当用户点击通知并打开应用程序时,我在AppDelegate.m中使用didReceivelocalnotification显示特定的视图控制器并向VC发送一些数据(日期).

@H_618_5@从锁定屏幕点击通知时,此工作正常.在通知中心点击通知时,永远不会调用didReceivelocalnotification.我使用UIAlertView在我的设备上测试它.调用didFinishLaunchingWithOptions,但是当我包含代码以在该方法显示特定的View Controller时它永远不会起作用(尽管另一个UIAlertView向我展示它运行了那部分代码,但从未完成showViewController方法).

@H_618_5@这是我的didFinishLaunchingWithOptions代码

@H_618_5@
- (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;
}
@H_618_5@这是我的didReceivelocalnotification代码

@H_618_5@
- (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];
}
@H_618_5@我已经拿出的日期内容只是检查它是否在晚上9点之后,创建今天的日期或昨天,然后将结果设置为reportVC的属性.如果这是相关的,请告诉我,我会重新加入.

@H_618_5@所以这里有一些我试解决的问题:

@H_618_5@>我尝试过使用presentViewController:animated:completion:而不是showViewController:sender:但是我想使用showViewController,@R_2_9447@让导航栏出现,而且无论如何都没有解决问题.
>我已经尝试将此行添加到我的didFinishLaunchingWithOptions方法中:
[自我申请:申请didReceivelocalnotification通知];
这确实解决了这个问题 – 从通知中心点击它打开了正确的视图,但它破坏了锁屏通知.添加此行后,当从锁定屏幕点击通知时,我得到了两次reportVC:第一个是除导航栏之外的黑屏,而顶部的那个是正确的.
>我尝试用这个替换我当前的showViewController代码行:
UIViewController * topController = [UIApplication sharedApplication] .keyWindow.rootViewController;
[topController showViewController:reportVC sender:self];
但这也没有解决问题.
>我尝试从我的didFinishLaunchingWithOptions方法获取我加倍的代码,因为我发现didReceivelocalnotification似乎无论如何都会在didFinishLaunchingWithOptions完成后运行.情况似乎确实如此,但它并没有解决我的Notification Center通知问题.

@H_618_5@如果这是搞砸了,这就是我现在正在测试的方法

@H_618_5@我将这两行添加到didFinishLaunchingWithOptions方法中:

@H_618_5@
[[UIApplication sharedApplication] cancelAlllocalnotifications];
[self createlocalnotification];
@H_618_5@我将通知的预定时间更改为将来约3分钟.它通常设置为每晚9点使用这样的日期组件:

@H_618_5@
dateComponents.hour = 21;
dateComponents.minute = 0;
@H_618_5@我将通知的repeaTinterval更改为NSCalendarUnitminute而不是NSCalendarUnitDay,这是它为发布版本设置的内容.

@H_618_5@然后我使用XCode在我的设备上运行应用程序,并在运行后停止并安排通知.我没有这两行再次运行它:

@H_618_5@
[[UIApplication sharedApplication] cancelAlllocalnotifications];
[self createlocalnotification];
@H_618_5@然后从XCode停止应用程序,在我的设备上打开多任务处理,向上滑动应用程序以关闭它,然后等待通知到达.在点击每个测试通知后,我多次执行并再次关闭应用程序,以便每次都可以从完全关闭的应用程序进行测试.

解决方法

您可能想要尝试此操作(使用dispatch_async()异步显示视图控制器): @H_618_5@ @H_618_5@
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];
    });
}
@H_618_5@或者这个(在呈现视图控制器之前调用[self.window makeKeyAndVisible]):

@H_618_5@
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];
}

大佬总结

以上是大佬教程为你收集整理的ios – 从通知中心点击时,UILocalNotification不会触发didReceiveLocalNotification全部内容,希望文章能够帮你解决ios – 从通知中心点击时,UILocalNotification不会触发didReceiveLocalNotification所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。