APNS只显示一个待处理通知。
创始人
2024-09-09 00:30:23
0

要解决APNS只显示一个待处理通知的问题,可以使用以下代码示例:

  1. 在AppDelegate.swift文件中,添加以下代码:
import UserNotifications

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        
        // 设置UNUserNotificationCenter的delegate为AppDelegate
        UNUserNotificationCenter.current().delegate = self
        
        // 请求用户授权显示通知
        UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { (granted, error) in
            if granted {
                print("用户授权显示通知")
            } else {
                print("用户未授权显示通知")
            }
        }
        
        application.registerForRemoteNotifications()
        
        return true
    }
    
    // 接收到远程通知时调用
    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
        print("收到远程通知")
        
        // 处理接收到的通知
        
        // 调用处理通知的方法
        handleNotification(userInfo)
    }
    
    // 处理通知的方法
    func handleNotification(_ userInfo: [AnyHashable : Any]) {
        // 解析通知内容
        guard let aps = userInfo["aps"] as? [String: Any] else {
            return
        }
        
        // 获取通知内容
        let alert = aps["alert"] as? String ?? ""
        
        // 显示通知
        let alertController = UIAlertController(title: "收到通知", message: alert, preferredStyle: .alert)
        alertController.addAction(UIAlertAction(title: "确定", style: .default, handler: nil))
        
        // 获取当前显示的控制器
        guard let rootViewController = UIApplication.shared.keyWindow?.rootViewController else {
            return
        }
        
        rootViewController.present(alertController, animated: true, completion: nil)
    }
    
    // iOS 10以上的系统会调用该方法来展示通知
    // 在该方法中调用处理通知的方法
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        let userInfo = notification.request.content.userInfo
        
        // 调用处理通知的方法
        handleNotification(userInfo)
        
        // 设置通知展示方式为alert和badge
        completionHandler([.alert, .badge])
    }
}
  1. 在Info.plist文件中添加以下代码,用于在应用处于前台时仍然显示通知:
UIBackgroundModes

    remote-notification

请注意,这只是一个示例代码,具体的实现可能会因为你的应用架构和需求而有所不同。此外,还需要确保在Apple开发者帐号中正确配置APNS和通知证书。

相关内容

热门资讯

盘点几款(微扑克有辅助挂)外挂... 盘点几款(微扑克有辅助挂)外挂透明挂辅助APP(辅助挂)确实是真的有挂(2025已更新)(哔哩哔哩)...
教程攻略!微扑克app(weP... 教程攻略!微扑克app(wePoke)外挂透明挂辅助器安装(辅助挂)AA德州教程(有挂技巧)-哔哩哔...
1分钟了解(xpoker)外挂... 1分钟了解(xpoker)外挂透明挂辅助挂(透视)发牌规律(有挂工具)-哔哩哔哩1、点击下载安装,微...
透视计算(微扑克工具)外挂透明... 透视计算(微扑克工具)外挂透明挂辅助神器(透视)透视辅助(有挂分享)-哔哩哔哩1、让任何用户在无需A...
必看攻略!德州wpk(wepO... 必看攻略!德州wpk(wepOkE)外挂透明挂辅助器安装(辅助挂)扑克教程(有挂技巧)-哔哩哔哩;小...
最新技巧(德扑安卓)外挂透明挂... 德扑安卓赢率提升策略‌;最新技巧(德扑安卓)外挂透明挂辅助软件(辅助挂)确实是真的有挂(2024已更...
盘点一款!微扑克数据(wepO... 1、盘点一款!微扑克数据(wepOKE)外挂透明挂辅助插件(透视)透明挂教程(有挂存在)-哔哩哔哩(...
一分钟揭秘(wepoke美元局... 一分钟揭秘(wepoke美元局)外挂透明挂辅助工具(辅助挂)软件透明挂(果真有挂)-哔哩哔哩1、这是...
玩家必看教程!云扑克德州PK(... 玩家必看教程!云扑克德州PK(wEpOke)外挂透明挂辅助器(透视)透明挂教程(有挂秘笈)-哔哩哔哩...
最新技巧(WPK技巧)外挂透明... 最新技巧(WPK技巧)外挂透明挂辅助器安装(透视)辅助透视(有挂方略)-哔哩哔哩 科技详细教程;75...