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和通知证书。

相关内容

热门资讯

透明透视!小程序广东雀神智能插... 透明透视!小程序广东雀神智能插件安装,wepokerplus辅助-原先有挂开挂辅助器;无需打开直接搜...
透视游戏!微信小程序微乐辅助免... 您好:微信小程序微乐辅助免费这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用...
透明工具!温州茶苑手机版辅助器... 透明工具!温州茶苑手机版辅助器,poker world辅助器-一向有挂开挂辅助软件这是一款可以让一直...
透视苹果版!边锋小程序跑的快辅... 【亲,边锋小程序跑的快辅助 这款游戏可以开挂的,确实是有挂的,很多玩家在这款边锋小程序跑的快辅助中打...
透视玄学!打两圈兴化辅助,we... 透视玄学!打两圈兴化辅助,wepoker破解游戏盒子-固有有挂开挂辅助插件这是一款可以让一直输的玩家...
透视存在!榆林打锅子辅助器,w... 透视存在!榆林打锅子辅助器,wejoker辅助软件视频-起初有挂开挂辅助软件;无需打开直接搜索加薇1...
透明新版!传送五激k辅助靠谱吗... 透明新版!传送五激k辅助靠谱吗,wepoker有没有插件-最初有挂开挂辅助软件这是一款可以让一直输的...
透视辅助!神途免费辅助软件,h... 透视辅助!神途免费辅助软件,hhpoker透视工具-从来有挂开挂辅助挂;无需打开直接搜索薇:1367...
透明新版!钱柜麻将辅助器,we... 透明新版!钱柜麻将辅助器,wepoker透视脚本下载-原先有挂开挂辅助平台;无需打开直接搜索薇:13...
透视安卓版!心悦填大坑辅助方法... 透视安卓版!心悦填大坑辅助方法,德州真人透视脚本-起初有挂开挂辅助器;亲,心悦填大坑辅助方法这款游戏...