UIApplicationLaunchOptionsLocalNotificationKey 始终为 null - iOS [英] UIApplicationLaunchOptionsLocalNotificationKey always null - iOS

查看:28
本文介绍了UIApplicationLaunchOptionsLocalNotificationKey 始终为 null - iOS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用本地通知的 iOS 应用程序.当应用程序在后台运行或处于活动状态时,我使用 application didReceiveLocalNotification 方法并且效果很好.但是当应用程序关闭(不在后台运行)时,我使用 application didFinishLaunchingWithOptions 方法来控制处理通知时发生的情况.

I have an iOS application which uses local notifications. When the app is running in the background or is active, I use the application didReceiveLocalNotification methods and that works great. 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.

但是,我遇到的问题是 didFinishLaunchingWithOptions 方法中的通知总是 (null).

However, the problem I have is that the notification is always (null) in the didFinishLaunchingWithOptions method.

这是我的代码:

-(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 及更高版本.

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.

感谢您抽出宝贵时间,丹.

Thanks for your time, Dan.

推荐答案

在警报视图中显示值 launchOptions 以查看是否有任何值.

Display the value launchOptions in an alert view to see if there is any value.

如果该值不为 nil,那么您的 UI 代码可能会在后台线程中执行.

If the value is not nil, then probably your UI code gets executed in the background thread.

确保所有 UI 代码都在主线程中执行,如下所示:

Ensure all UI code is executed in the main thread as shown below:

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

当您在应用中使用异步调用时,这一点很重要.

This is important when you use asynchronous calls in your app.

这篇关于UIApplicationLaunchOptionsLocalNotificationKey 始终为 null - iOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆