简体   繁体   English

UIApplicationLaunchOptionsLocalNotificationKey 始终为 null - iOS

[英]UIApplicationLaunchOptionsLocalNotificationKey always null - iOS

I have an iOS application which uses local notifications.我有一个使用本地通知的 iOS 应用程序。 When the app is running in the background or is active, I use the application didReceiveLocalNotification methods and that works great.当应用程序在后台运行或处于活动状态时,我使用application didReceiveLocalNotification方法并且效果很好。 But when the app is closed (not running in the background), I use the application didFinishLaunchingWithOptions method to control what happens when the notification is handled.但是当应用程序关闭(不在后台运行)时,我使用application didFinishLaunchingWithOptions方法来控制处理通知时发生的情况。

However, the problem I have is that the notification is always (null) in the didFinishLaunchingWithOptions method.但是,我didFinishLaunchingWithOptions的问题是didFinishLaunchingWithOptions方法中的通知始终为(null)

Here is my code:这是我的代码:

-(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;
}

Am I missing something here?我在这里错过了什么吗? I have asked the user for permission first and have the correct code to get the notification object.我首先向用户请求许可,并有正确的代码来获取通知对象。 My app is built for iOS 9 and higher only.我的应用程序仅适用于 iOS 9 及更高版本。

Thanks for your time, Dan.谢谢你的时间,丹。

Display the value launchOptions in an alert view to see if there is any value.在警报视图中显示值launchOptions以查看是否有任何值。

If the value is not nil, then probably your UI code gets executed in the background thread.如果该值不为 nil,那么您的 UI 代码可能会在后台线程中执行。

Ensure all UI code is executed in the main thread as shown below:确保所有 UI 代码都在主线程中执行,如下所示:

dispatch_async(dispatch_get_main_queue(), ^{ 
//self doSomething (UI Code)
});

This is important when you use asynchronous calls in your app.当您在应用中使用异步调用时,这一点很重要。

Implement your code in applicationWillTerminate rather than didFinishLaunchingWithOptions .applicationWillTerminate而不是didFinishLaunchingWithOptions实现你的代码。 Because when the app terminated then execute the applicationWillTerminate method.因为当应用程序终止时执行applicationWillTerminate方法。

 func applicationWillTerminate(application: UIApplication) {
     // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM