当前位置: 首页 > wzjs >正文

北京的公司排名seo快速排名软件推荐

北京的公司排名,seo快速排名软件推荐,专门做本子的网站,可信网站是否有规定必须做在iOS通知中显示富媒体内容可以显著提升用户体验。通过UNNotificationAttachment,我们可以为本地和远程通知添加图片、音频、视频等内容。 基本实现方法 1. 创建带附件的通知 func scheduleNotificationWithImage() {// 1. 创建通知内容let content UNMutableNo…

在iOS通知中显示富媒体内容可以显著提升用户体验。通过UNNotificationAttachment,我们可以为本地和远程通知添加图片、音频、视频等内容。

基本实现方法

1. 创建带附件的通知

func scheduleNotificationWithImage() {// 1. 创建通知内容let content = UNMutableNotificationContent()content.title = "图片通知"content.body = "这是一条带图片的推送通知"content.sound = UNNotificationSound.default// 2. 准备图片附件guard let imageURL = Bundle.main.url(forResource: "notification_image", withExtension: "jpg"),let attachment = try? UNNotificationAttachment(identifier: "imageAttachment",url: imageURL,options: nil) else {print("无法创建图片附件")return}content.attachments = [attachment]// 3. 设置触发器(5秒后)let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)// 4. 创建请求let request = UNNotificationRequest(identifier: "imageNotification",content: content,trigger: trigger)// 5. 添加到通知中心UNUserNotificationCenter.current().add(request) { error inif let error = error {print("添加图片通知失败: \(error.localizedDescription)")} else {print("图片通知已安排")}}
}

2. 支持的文件类型

iOS通知附件支持以下类型:

文件类型扩展名限制
图片.jpg, .png, .gif最大10MB
音频.mp3, .aiff, .wav最大5MB
视频.mp4, .mov最大50MB
其他见官方文档需符合UTI规范

高级用法

1. 动态下载网络图片

func scheduleNotificationWithRemoteImage(imageURLString: String) {// 1. 下载图片guard let url = URL(string: imageURLString) else { return }URLSession.shared.downloadTask(with: url) { tempURL, response, error inguard let tempURL = tempURL, error == nil else {print("图片下载失败: \(error?.localizedDescription ?? "未知错误")")return}// 2. 保存到临时目录let fileManager = FileManager.defaultlet filename = url.lastPathComponentlet tempDirectory = URL(fileURLWithPath: NSTemporaryDirectory())let localURL = tempDirectory.appendingPathComponent(filename)do {// 移除已存在的文件if fileManager.fileExists(atPath: localURL.path) {try fileManager.removeItem(at: localURL)}// 移动文件try fileManager.moveItem(at: tempURL, to: localURL)// 3. 创建附件let attachment = try UNNotificationAttachment(identifier: "remoteImage",url: localURL,options: [UNNotificationAttachmentOptionsTypeHintKey: kUTTypeJPEG])// 4. 创建通知let content = UNMutableNotificationContent()content.title = "网络图片通知"content.body = "下载的图片已附加到通知"content.attachments = [attachment]let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 1, repeats: false)let request = UNNotificationRequest(identifier: "remoteImageNotification",content: content,trigger: trigger)// 5. 添加通知UNUserNotificationCenter.current().add(request) { error inif let error = error {print("添加远程图片通知失败: \(error.localizedDescription)")}}} catch {print("创建附件失败: \(error.localizedDescription)")}}.resume()
}

2. 视频附件处理

func scheduleNotificationWithVideo() {// 1. 获取视频URL(可以是本地或下载后的远程视频)guard let videoURL = Bundle.main.url(forResource: "demo", withExtension: "mp4") else {print("找不到视频文件")return}// 2. 创建视频附件do {let attachment = try UNNotificationAttachment(identifier: "videoAttachment",url: videoURL,options: [UNNotificationAttachmentOptionsTypeHintKey: kUTTypeMPEG4])// 3. 创建通知内容let content = UNMutableNotificationContent()content.title = "视频通知"content.body = "点击查看视频内容"content.attachments = [attachment]// 4. 设置触发器let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)// 5. 创建并发送通知let request = UNNotificationRequest(identifier: "videoNotification",content: content,trigger: trigger)UNUserNotificationCenter.current().add(request) { error inif let error = error {print("添加视频通知失败: \(error.localizedDescription)")}}} catch {print("创建视频附件失败: \(error.localizedDescription)")}
}

附件选项配置

1. 常用选项

let options: [AnyHashable: Any] = [// 指定文件类型UNNotificationAttachmentOptionsTypeHintKey: kUTTypePNG,// 是否隐藏缩略图(仅音频)UNNotificationAttachmentOptionsThumbnailHiddenKey: false,// 缩略图裁剪区域(CGRect字典)UNNotificationAttachmentOptionsThumbnailClippingRectKey: ["X": 0.25,"Y": 0.25,"Width": 0.5,"Height": 0.5],// 缩略图时间点(视频,秒)UNNotificationAttachmentOptionsThumbnailTimeKey: 5
]let attachment = try UNNotificationAttachment(identifier: "customImage",url: imageURL,options: options
)

2. 自适应图片大小

func resizeImageForNotification(originalURL: URL, targetSize: CGSize) -> URL? {guard let image = UIImage(contentsOfFile: originalURL.path) else { return nil }let renderer = UIGraphicsImageRenderer(size: targetSize)let resizedImage = renderer.image { _ inimage.draw(in: CGRect(origin: .zero, size: targetSize))}let tempDir = URL(fileURLWithPath: NSTemporaryDirectory())let resizedURL = tempDir.appendingPathComponent("resized_\(originalURL.lastPathComponent)")do {try resizedImage.pngData()?.write(to: resizedURL)return resizedURL} catch {print("保存调整大小后的图片失败: \(error)")return nil}
}

最佳实践与注意事项

1. 文件大小优化

func optimizeImageForNotification(originalURL: URL) -> URL? {guard let imageData = try? Data(contentsOf: originalURL),let image = UIImage(data: imageData) else {return nil}// 1. 检查文件大小let fileSize = imageData.count / 1024 // KBprint("原始文件大小: \(fileSize)KB")// 2. 如果超过限制则压缩if fileSize > 1024 { // 假设限制为1MBlet compressionQuality: CGFloat = 1024.0 / CGFloat(fileSize)if let compressedData = image.jpegData(compressionQuality: compressionQuality) {let tempURL = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("optimized_\(originalURL.lastPathComponent)")try? compressedData.write(to: tempURL)return tempURL}}return originalURL
}

2. 清理临时文件

func cleanTempAttachments() {let fileManager = FileManager.defaultlet tempDir = URL(fileURLWithPath: NSTemporaryDirectory())do {let tempFiles = try fileManager.contentsOfDirectory(at: tempDir, includingPropertiesForKeys: nil)for file in tempFiles {// 删除所有临时附件文件if file.lastPathComponent.hasPrefix("optimized_") || file.lastPathComponent.hasPrefix("resized_") {try fileManager.removeItem(at: file)}}} catch {print("清理临时文件失败: \(error)")}
}

3. 错误处理与回退

func safeAddAttachment(to content: UNMutableNotificationContent, imageURL: URL) {do {let optimizedURL = optimizeImageForNotification(originalURL: imageURL) ?? imageURLlet attachment = try UNNotificationAttachment(identifier: "fallbackImage",url: optimizedURL,options: nil)content.attachments = [attachment]} catch {print("添加附件失败,使用纯文本通知: \(error)")// 回退方案:使用文本通知content.body += " [包含图片,加载失败]"}
}

实际应用示例

1. 社交媒体通知

func sendPostNotification(userName: String, postText: String, imageURL: URL?) {let content = UNMutableNotificationContent()content.title = "\(userName) 发布了新内容"content.body = postTextcontent.sound = .defaultcontent.categoryIdentifier = "POST_NOTIFICATION"// 如果有图片则添加if let imageURL = imageURL {safeAddAttachment(to: content, imageURL: imageURL)}// 添加用户交互按钮let likeAction = UNNotificationAction(identifier: "like",title: "👍 喜欢",options: [])let commentAction = UNTextInputNotificationAction(identifier: "comment",title: "💬 评论",options: [.foreground],textInputButtonTitle: "发送",textInputPlaceholder: "说点什么...")let category = UNNotificationCategory(identifier: "POST_NOTIFICATION",actions: [likeAction, commentAction],intentIdentifiers: [],options: [])UNUserNotificationCenter.current().setNotificationCategories([category])// 立即触发通知let request = UNNotificationRequest(identifier: UUID().uuidString,content: content,trigger: nil // 立即触发)UNUserNotificationCenter.current().add(request)
}

2. 电商促销通知

func sendPromotionNotification(productName: String, discount: String, imageURL: URL) {let content = UNMutableNotificationContent()content.title = "限时特惠"content.body = "\(productName) 直降 \(discount)"content.sound = .defaultcontent.categoryIdentifier = "PROMOTION_NOTIFICATION"// 添加产品图片safeAddAttachment(to: content, imageURL: imageURL)// 添加交互按钮let buyNowAction = UNNotificationAction(identifier: "buyNow",title: "立即购买",options: [.foreground])let addToCartAction = UNNotificationAction(identifier: "addToCart",title: "加入购物车",options: [])let category = UNNotificationCategory(identifier: "PROMOTION_NOTIFICATION",actions: [buyNowAction, addToCartAction],intentIdentifiers: [],options: [])UNUserNotificationCenter.current().setNotificationCategories([category])// 设置明天上午10点的触发器var dateComponents = DateComponents()dateComponents.hour = 10let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents,repeats: false)let request = UNNotificationRequest(identifier: "promo_\(productName)",content: content,trigger: trigger)UNUserNotificationCenter.current().add(request)
}

通过以上方法,可以为iOS通知添加丰富的媒体内容,显著提升用户体验和通知的点击率。记得在实际使用时考虑性能优化和错误处理,确保在各种情况下都能提供良好的用户体验。

http://www.dtcms.com/wzjs/51319.html

相关文章:

  • 福州网站设计招聘优化搜狗排名
  • 做网站是买服务器还是买cdn最火的网络推广平台
  • 深圳seo推广公司福州关键词排名优化
  • cn后缀做网站网络平台的推广方法
  • 龙岩做网站开发找哪家安徽网站seo
  • 开发网站找什么公司友情链接交换要注意哪些问题
  • win10记事本怎么做网站长沙网络推广公司
  • 360网站seo怎么做app定制开发
  • 有哪些专门做校企合作的网站做关键词排名好的公司
  • 聊城 网站制作怎样查询百度收录和排名情况
  • 动漫网站建设总结什么是关键词排名优化
  • 上海网站开发与设计微信营销软件排行榜
  • 解决wordpress慢seo快速排名软件
  • 网站建设 源码路由优化大师
  • wordpress外观无法编辑日照seo优化
  • 手机网站活动策划方案seo招聘信息
  • 网站建设公司怎么做好商品热搜词排行榜
  • 吉林市建设官方网站网站优化推广服务
  • 影响网站收录的因数百度高级搜索网址
  • 邢台最新消息河南优化网站
  • 重庆建设医院官方网站百度推广竞价开户
  • 会声会影免费模板网站百度小说风云榜今天
  • 广州自助网站搭建制作公司搜索引擎优化学习
  • 阜新市住房和城乡建设委员会网站qq引流推广软件免费
  • 企业网站程序带wap软件外包公司排行榜
  • 网站建设诚信服务百度引擎搜索引擎
  • 网站 数据报表如何做青岛网站seo优化
  • 网站建设招聘要求淘宝友情链接怎么设置
  • 网站中的文字滑动怎么做南宁网站推广大全
  • 新疆建设兵团公安厅官方网站雅虎搜索引擎