• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - UIApplicationLaunchOptionsLocalNotificationKey 始终为空 - iOS

[复制链接]
菜鸟教程小白 发表于 2022-12-12 19:03:48 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

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

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

这是我的代码:

-(BOOL)applicationUIApplication *)application didFinishLaunchingWithOptionsNSDictionary *)launchOptions {

    // Override point for customization after application launch.

    // Register the app for local notifcations.

    if ([application respondsToSelectorselector(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 及更高版本。

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



Best Answer-推荐答案


在警报 View 中显示值 launchOptions 以查看是否有任何值。

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

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

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

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

关于ios - UIApplicationLaunchOptionsLocalNotificationKey 始终为空 - iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36246104/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap