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***79002 小时前
Swift进阶
开发语言·ios·swift
denggun123452 小时前
ios卡顿优化
ios·xcode
從南走到北2 小时前
JAVA国际版打车APP打车顺风车滴滴车跑腿APP源码Android+IOS+H5
android·java·ios
QuantumLeap丶3 小时前
《Flutter全栈开发实战指南:从零到高级》- 13 -状态管理GetX
android·flutter·ios·前端框架
從南走到北6 小时前
JAVA国际版二手车交易二手车市场系统源码支持Android+IOS+H5+APP
android·java·ios
ajassi20008 小时前
开源 Objective-C IOS 应用开发(九)复杂控件-tableview
ios·开源·objective-c
2501_915106328 小时前
iOS性能调优的系统化实践,从架构分层到多工具协同的全流程优化指南(开发者深度版)
android·ios·小程序·架构·uni-app·iphone·webview
uiop_uiop_uiop8 小时前
iOS arm64e hook MGCopyAnswer got Crash or Only Partial results got hooked
macos·ios·cocoa
~~李木子~~11 小时前
贪心算法实验1
算法·ios·贪心算法
大炮走火12 小时前
iOS在制作framework时,oc与swift混编的流程及坑点!
开发语言·ios·swift