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

相关推荐
iOS_MingXing7 小时前
iOS导航栏左上角显示一块白色圆角/圆形
macos·ios·cocoa
ii_best9 小时前
按键精灵和触动精灵哪个好?iOS 自动化选型的四个判断维度
运维·ios·自动化·按键精灵
乐鑫科技 Espressif10 小时前
通过 Wi-Fi Aware 直接将 ESP 设备连接到 iPhone——全新的跨平台 P2P 标准
ios·esp32·iphone·p2p·乐鑫科技
杜哥无敌1 天前
反向代理那些事儿: MaxKB 和1Panel ,用https还是http
ios·iphone
遥不可及zzz1 天前
iOS 27 适配指南:Xcode 27 升级、UIScene 生命周期迁移及游戏项目兼容性处理
ios·ios 27·xcode27
Geek-Chow1 天前
02 · Redux 是什么,又不是什么
ios
楚识科技1 天前
移动端 OCR SDK 集成拆解:Android/iOS 离线识别与结构化回填
android·ios·ocr
CocoaKier2 天前
神技分享:App加锁,App隐藏?苹果官方自带,只需要一个长按
ios·apple
传奇开心果编程2 天前
【六大移动UI框架比较学与练】第5课:交互与导航——事件处理、手势与页面路由实战对照
android·flutter·react native·ios·swiftui·移动开发·android jetpack
Geek-Chow2 天前
01 · 为什么要在 iOS 上用 Redux
ios