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

相关推荐
毛骗导演5 小时前
万字解析 OpenClaw 源码架构-跨平台应用之 iOS 应用
前端·ios·架构
鹏多多.15 小时前
Flutter使用screenshot进行截屏和截长图以及分享保存的全流程指南
android·前端·flutter·ios·前端框架
少云清15 小时前
【UI自动化测试】1_IOS自动化测试 _环境搭建
ui·ios
yijianxiangde10020 小时前
iOS 无根越狱前期准备
ios
库奇噜啦呼20 小时前
【iOS】Effective Objective-C第一章
开发语言·ios·objective-c
2501_916008891 天前
iPhone 上怎么抓 App 的网络请求,在 iOS 设备上捕获网络请求
android·网络·ios·小程序·uni-app·iphone·webview
他们都不看好你,偏偏你最不争气1 天前
【iOS】block
开发语言·ios·objective-c·block·闭包
符哥20081 天前
充电桩 iOS App 的 BLE蓝牙模块(Swift)
macos·ios·cocoa
helloworddm1 天前
紧急预警!iOS最新高危漏洞爆发,23个漏洞打包扩散,已野外攻击
macos·ios·cocoa