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

相关推荐
wangruofeng6 小时前
AI 助力 Flutter 3.27 升级到 3.38 完整指南:两周踩坑与实战复盘
flutter·ios·ai编程
iOS日常1 天前
Xcode 垃圾清理
ios·xcode
开心就好20251 天前
不越狱能抓到 HTTPS 吗?在未越狱 iPhone 上抓取 HTTPS
后端·ios
傅里叶1 天前
iOS相机权限获取
flutter·ios
zhangkai2 天前
flutter存储知识点总结
flutter·ios
齐生12 天前
网络知识点 - TCP/IP 四层模型知识大扫盲
笔记·ios
IT技术分享社区2 天前
数码资讯:iPhone 18 Pro,十大升级细节浮出水面
ios·手机·iphone
嵌入式学习菌2 天前
https不校验证书实现及https接口实现
ios·iphone