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

ios - Swift - 在 applicationWillTerminate 中调用本地通知方法

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

我有一个用于本地通知的公共(public)函数,我想在 applicationWillTerminate 中调用该函数。当我尝试这个时,没有任何反应。我确定本地通知功能没问题,因为当我在 viewDidLoad 中调用它时,它可以正常工作。


这是本地通知功能;

func scheduleLocalNotification(second: Double) {

    if #available(iOS 10.0, *) {
        let center = UNUserNotificationCenter.current()

        let content = UNMutableNotificationContent()
        content.badge = 1
        content.title = "Title!"
        content.body = "Message!"
        content.categoryIdentifier = "id"
        content.userInfo = ["key": "value"]
        content.sound = UNNotificationSound.default()

        let trigger = UNTimeIntervalNotificationTrigger(timeInterval: second, repeats: false)

        let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
        center.add(request)
    } else {
        // Fallback on earlier versions
    }
}

AppDelegate;

func applicationWillTerminate(_ application: UIApplication) {

        scheduleLocalNotification(second: 30.0)
        print("Terminating!")

        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }

我在 SO 中搜索了类似的问题,发现了 2 个问题,但没有一个能解决我的问题。

Send local notification after termination app swift 2

Local notification on application termination


编辑:请求授权;

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        let launchedBefore = UserDefaults.standard.bool(forKey: "launchedBefore")
        if (!launchedBefore)  {
            if #available(iOS 10.0, *) {
                UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { (granted, error) in
                    if granted {
                        print("Authorization granted!")
                    } else {
                        print("Authorization not granted!")
                    }
                }
            } else {
                let settings = UIUserNotificationSettings(types: [.alert, .badge , .sound], categories: nil)
                UIApplication.shared.registerUserNotificationSettings(settings)
            }

            UserDefaults.standard.set(true, forKey: "launchedBefore")
        }

        // Override point for customization after application launch.
        return true
    }



Best Answer-推荐答案


您链接到的第二个问题的答案似乎相关 - 如果系统决定在后台终止您的应用程序,则不能保证调用 applicationWillTerminate

关于ios - Swift - 在 applicationWillTerminate 中调用本地通知方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42235023/

回复

使用道具 举报

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

本版积分规则

关注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