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

相关推荐
如此风景12 小时前
iOS SwiftUI开发所有修饰符使用详解
ios
mumuWorld12 小时前
KSCrash 实现机制深度分析
ios·源码阅读
AskHarries12 小时前
Google 登录问题排查指南
flutter·ios·app
崽崽长肉肉13 小时前
Swift中的知识点总结
ios·swift
2501_9160074714 小时前
苹果手机iOS应用管理全指南与隐藏功能详解
android·ios·智能手机·小程序·uni-app·iphone·webview
2501_9151063217 小时前
全面理解 iOS 帧率,构建从渲染到系统行为的多工具协同流畅度分析体系
android·ios·小程序·https·uni-app·iphone·webview
TouchWorld18 小时前
iOS逆向-哔哩哔哩增加3倍速(1)-最大播放速度
ios·逆向
RollingPin19 小时前
React Native与Flutter的对比
android·flutter·react native·ios·js·移动端·跨平台开发
2501_9160088919 小时前
iOS 能耗检测的工程化方法,构建多工具协同的电量分析与性能能效体系
android·ios·小程序·https·uni-app·iphone·webview
long_run19 小时前
Objective-C 类与对象详细入门
ios