UIApplicationLaunchOptionsLocalNotificationKey始终为空 – iOS
我有一个使用本地通知的iOS应用程序。 当应用程序在后台运行或处于活动状态时,我使用application didReceiveLocalNotification
方法,这很好。 但是,当应用程序closures(不在后台运行)时,我使用application didFinishLaunchingWithOptions
方法来控制通知处理时发生的情况。
但是,我didFinishLaunchingWithOptions
的问题是didFinishLaunchingWithOptions
方法中的通知总是(null)
。
这是我的代码:
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. // Register the app for local notifcations. if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) { [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil]]; } // Setup the local notification check. UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey]; //UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"options" message:notification delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; //[alertView show]; // Check if a notifcation has been received. if (notification) { // Run the notifcation. [self.myViewController run_notifcation:[notification.userInfo valueForKey:@"notification_id"]]; } // Ensure the notifcation badge number is hidden. application.applicationIconBadgeNumber = 0; return YES; }
我在这里错过了什么? 我已经要求用户的权限,并有正确的代码来获取通知对象。 我的应用程序仅适用于iOS 9及更高版本。
谢谢你的时间,丹。
在警报视图中显示值launchOptions
以查看是否有任何值。
如果值不是零,那么你的UI代码可能在后台线程中执行。
确保所有UI代码在主线程中执行,如下所示:
dispatch_async(dispatch_get_main_queue(), ^{ //self doSomething (UI Code) });
在您的应用中使用asynchronous调用时,这一点非常重要。
在applicationWillTerminate
而不是didFinishLaunchingWithOptions
实现你的代码。 因为当应用程序终止,然后执行applicationWillTerminate
方法。
func applicationWillTerminate(application: UIApplication) { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. }
- 用接口控制器重复架构x86_64的符号
- 为什么我得到延迟加载NSBundle MobileCoreServices.framework?
- 收到iOS推送通知时打开视图控制器
- 在AppDelegate中使用Swift UINavigationBar setBackgroundImage
- 使用GIDSignIn处理使用其他Google应用程序login时,不能获取Google用户
- 表格添加SWRevealViewController后不加载数据
- Swift – 从appDelegate的pushViewController,rootViewController.navigationController是零
- 从AppDelegate.swift为一个视图控制器分配一个值
- 在今天的扩展中使用AppDelegate