You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

iOS Push Notification Service Extension无法展示媒体内容求助

通知内容扩展无法展示媒体内容的解决求助

我已经通过以下代码成功实现App的推送通知功能,所有流程符合规范,代码可正常触发推送通知:

private func initPushNotification(_ application: UIApplication) {
    Messaging.messaging().delegate = self
    if #available(iOS 10.0, *) {
        let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
        UNUserNotificationCenter.current().requestAuthorization(
            options: authOptions,
            completionHandler: {status, error in
                PushNotificationHelper.shared.sendStatus(status)
            })
        // For iOS 10 display notification (sent via APNS)
        UNUserNotificationCenter.current().delegate = self
    } else {
        let settings: UIUserNotificationSettings =
        UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
        application.registerUserNotificationSettings(settings)
    }
    application.registerForRemoteNotifications()
    UIApplication.shared.applicationIconBadgeNumber = 0
}

 func userNotificationCenter(_ center: UNUserNotificationCenter,
                            willPresent notification: UNNotification,
  withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
  
    let userInfo = notification.request.content.userInfo
    Messaging.messaging().appDidReceiveMessage(userInfo)
    completionHandler([.alert, .sound, .badge])
}

// On Click Notification bar
func userNotificationCenter(_ center: UNUserNotificationCenter,
                            didReceive response: UNNotificationResponse,
                            withCompletionHandler completionHandler: @escaping () -> Void) {
    
    let userInfo = response.notification.request.content.userInfo
    Messaging.messaging().appDidReceiveMessage(userInfo)
    
    PushNotificationHelper.shared.handlePushNotification(response)
    
    completionHandler()
}

func application(_ application: UIApplication,
didReceiveRemoteNotification userInfo: [AnyHashable : Any],
   fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
  Messaging.messaging().appDidReceiveMessage(userInfo)
    
  completionHandler(.noData)
}

func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
    
}

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    
    Messaging.messaging().apnsToken = deviceToken
            
            #if DEBUG
                PushNotificationHelper.shared.subscribeToTopic(topic: PNServiceTopicKey.debug_all.keyValue)
                PushNotificationHelper.shared.unsubscribeToTopic(topic: PNServiceTopicKey.release_all.keyValue)
                PushNotificationHelper.shared.handleDebugPremiumUser()
            #else
                PushNotificationHelper.shared.subscribeToTopic(topic: PNServiceTopicKey.release_all.keyValue)
                PushNotificationHelper.shared.unsubscribeToTopic(topic: PNServiceTopicKey.debug_all.keyValue)
                PushNotificationHelper.shared.handleReleasePremiumUser()
            #endif

}

现在希望在通知中展示媒体内容,因此实现了Notification Content Extension,但目前通知仍仅显示标题和正文,媒体内容无法展示。相关代码如下:

func notificationCenter(){
    let notification = UNUserNotificationCenter.current()
    notification.requestAuthorization(options : [.alert , .badge , .sound]) { success, error in
        if success {
            print("Authorization Successful")
            notification.delegate = self
        }else{
            print("Authorization Failed")
        }
    }
    let openButton = UNNotificationAction(identifier: "openButton", title: "Open" , options: .foreground)
    let category = UNNotificationCategory(identifier: "myNotificationCategory", actions: [openButton], intentIdentifiers: [] , options: [])
    notification.setNotificationCategories([category])
}

我使用的推送payload如下:

{
 "to" : "/topics/avm_ios_debug_all",
 "content_available": true,
 "category": "content_added_notification",
 "alert" : {
    "title" : "Try our new photo editor",
    "body" : "Your message Here"
    },
    "mutable-content" : "1",
    "category" : "Make your images attractive and colorful",
 "data": {
 "title": "Try our new photo editor",
 "body" : "Make your images attractive and colorful",
 "app-url": "https://apps.apple.com/us/app/example",
 "deep-link": "deeplink://",
 "image" : "https://www.cyberclick.net/hs-fs/hubfs/Banner%20Ads.jpg?width=1200&name=Banner%20Ads.jpg"
 }
}

当前通知效果:
示例截图

现寻求解决方法,让通知能正常展示媒体内容。

内容的提问来源于stack exchange,提问作者d0tb0t

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 14:23:14