iOS 应用在前台时显示通知

背景:

在iOS应用中,当应用在前台运行时,是不会默认弹出通知的。这是iOS的设计决定,以避免用户在使用应用的过程中被打扰。然而,如果你希望在应用在前台的时候也能收到通知,你可以在你的应用代码中进行一些修改。

添加代理

  1. 在AppDelegate的didFinishLaunchingWithOptions:方法中设置通知中心的代理

  2. 在你的AppDelegate中实现userNotificationCenter(_:willPresent:withCompletionHandler:)方法。

    public func setNotifiAuth() {
    let center = UNUserNotificationCenter.current()
    center.delegate = self
    center.requestAuthorization(options: [.alert, .badge, .sound]) { granted, error in
    if granted == true {
    dePrint("允许通知")
    } else {
    dePrint("不允许通知")
    }
    }
    }

    extension LocalNotification: UNUserNotificationCenterDelegate {
    // 前台显示弹窗
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification) async -> UNNotificationPresentationOptions {
    return .alert
    }
    }

相关推荐
aiopencode7 小时前
iOS开发中Xcode安装不完整问题解决方案与配置指南
后端·ios
Joseph188 小时前
深度拆解 DanceUI:从声明式视图到原生渲染的全链路技术解析
ios·swiftui
人月神话Lee8 小时前
【图像处理】颜色科学与灰度化——人眼看到的和数字记录的不一样
ios·ai编程·图像识别
bcbnb9 小时前
iOS开发中手动实现代码混淆的完整步骤与示例
后端·ios
2501_9159090610 小时前
全面解析前端开发中常用的浏览器调试工具及其使用场景
android·ios·小程序·https·uni-app·iphone·webview
择势10 小时前
NSProxy 核心原理、消息机制、多继承、AOP、Timer 解耦、快速转发全解
ios
songgeb11 小时前
iOS IAP 本地货币展示:从一个需求到搞清楚 priceLocale
ios·swift
MonkeyKing715516 小时前
iOS Block 底层深度解析:结构、变量捕获、copy逻辑与循环引用本质
ios·objective-c
MonkeyKing16 小时前
iOS 二进制重排与PageZero优化:从原理到实战
ios
MonkeyKing16 小时前
iOS 野指针、僵尸对象与Zombie机制原理详解
ios