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

相关推荐
kkk_皮蛋11 小时前
在移动端使用 WebRTC (Android/iOS)
android·ios·webrtc
Swift社区12 小时前
如何在 SwiftUI 中对 CoreImage 滤镜做实时预览
ios·swiftui·cocoa
ipad协议开发15 小时前
企业微信iPad协议的开发进程
ios·企业微信·ipad
七月巫山晴15 小时前
【iOS】OC中的一些宏
前端·ios·objective-c
初级代码游戏1 天前
iOS开发 SwiftUI Text的基本用法
ios·swiftui·swift
TheNextByte11 天前
如何安全有效地清除iPad数据以便出售?
安全·ios·ipad
十二测试录1 天前
Android和iOS测试区别
android·经验分享·ios·职场发展·ab测试
张飞签名上架2 天前
深耕全球市场:App上架iOS与Google Play全流程指南
macos·ios·cocoa·ios上架·上架·谷歌上架
Digitally2 天前
iPhone 无法向安卓设备发送图片:轻松解决
android·ios·iphone
阿里云云原生2 天前
RUM 助力 iOS 应用稳定性:从异常捕获到堆栈还原的全流程分析
人工智能·阿里云·ios·云原生·rum