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

相关推荐
如此风景1 小时前
IOS SwiftUI 全组件详解
ios
雾散声声慢1 小时前
解决 iOS 上 Swiper 滑动图片闪烁问题:原因分析与最有效的修复方式
前端·css·ios
QuantumLeap丶4 小时前
《Flutter全栈开发实战指南:从零到高级》- 24 -集成推送通知
android·flutter·ios
YungFan4 小时前
iOS开发之MetricKit监控App性能
ios·swiftui·swift
二流小码农5 小时前
鸿蒙开发:上架困难?谈谈我的上架之路
android·ios·harmonyos
图图大恼5 小时前
在iOS上体验Open-AutoGLM:从安装到流畅操作的完整指南
人工智能·ios·agent
笑尘pyrotechnic6 小时前
[iOS原理] Block的本质
ios·objective-c·cocoa
TheNextByte17 小时前
如何从 iPhone 发送大型音频文件
ios·iphone
TheNextByte17 小时前
如何清理 iPhone 应用崩溃日志:简单有效的指南
ios·cocoa·iphone
岁月向前1 天前
Jenkins实现iOS自动化打包
ios