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

相关推荐
壹方秘境16 小时前
厌倦了那些看着像一个模版复刻出来的抓包工具,我开发了一款iOS端HTTPS抓包调试工具
ios·程序员·产品
开心就好202521 小时前
不同阶段的 iOS 应用混淆工具怎么组合使用,源码混淆、IPA混淆
后端·ios
世界不及妳微笑21 小时前
关于Xcode26.4 踩坑适配
ios·xcode
iceiceiceice21 小时前
iOS 26 适配 | 使用 `hidesSharedBackground` 保持导航栏按钮原有样式
ios·objective-c·编程语言
SY.ZHOU1 天前
移动端架构体系(一):组件化
android·ios·架构·系统架构
人月神话Lee1 天前
一个iOS开发者对Flutter中Widget、Element和RenderObject的理解
前端·flutter·ios
2501_916007471 天前
iOS 开发工具有哪些 按开发流程整理的工具清单
ide·vscode·ios·objective-c·个人开发·swift·敏捷流程
2501_915921431 天前
2026 iOS 上架新趋势 iOS 发布流程模块化
android·ios·小程序·https·uni-app·iphone·webview
库奇噜啦呼1 天前
【iOS】Effective Objective-C第四章
ios·objective-c·cocoa