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 分享扩展(三):轻松定制 iOS 分享界面,提升用户体验
ios·swift·apple
zjam93332 小时前
iOS 26 又有新的属性监听方法啦?
ios
鹤渺3 小时前
React Native 搭建iOS与Android开发环境
android·react native·ios
Digitally10 小时前
如何将数据从安卓设备传输到 iPhone | 综合指南
android·ios·iphone
Pitayafruit11 小时前
写给尊贵的 Tare Pro 用户的喂饭级 IOS APP 开发指南
ios·ai编程·trae
openinstall1 天前
A/B测试如何借力openinstall实现用户价值深挖?
android·ios·html
二流小码农1 天前
鸿蒙开发:资讯项目实战之项目初始化搭建
android·ios·harmonyos
Hierifer1 天前
跨端实现 DSBridge 源码解析
前端·ios
CocoaKier1 天前
你的开发者账号已经被盯上了,如何区分钓鱼邮件
ios·google·apple