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

相关推荐
RollingPin3 小时前
iOS八股文之 多线程
ios·多线程·串行并行·gcd·ios面试·同步异步·nsoperation
AirDroid_cn3 小时前
在 iOS 18 中,控制中心怎样添加应用快捷方式?
macos·ios·cocoa
RollingPin3 小时前
iOS八股文之 内存管理
ios·内存管理·内存泄漏·ios面试·arc·runloop·引用计数
2501_915106329 小时前
iOS 26 APP 性能测试实战攻略:多工具组合辅助方案
android·macos·ios·小程序·uni-app·cocoa·iphone
开开心心loky9 小时前
[iOS] KVC 学习
学习·ios·objective-c·cocoa
00后程序员张17 小时前
iOS混淆与IPA文件加固全流程实战 防止苹果应用被反编译的工程级方案
android·ios·小程序·https·uni-app·iphone·webview
胖虎117 小时前
iOS 推送证书 P8 介绍及生成流程
ios·个推·p8证书·极光推送·ios推送
白熊18818 小时前
【图像大模型】ms-swift 深度解析:一站式多模态大模型微调与部署框架的全流程使用指南
开发语言·ios·swift
2501_9151063218 小时前
iOS 应用加固与苹果软件混淆指南,如何防止 IPA 被反编译与二次打包?
android·ios·小程序·https·uni-app·iphone·webview
用户347475478332819 小时前
把SwiftUI View 转为图片
ios·swiftui