0

当我点击通知展开时,它会出现在标题和消息的上方,在图像的底部。我正在寻找的是改变这个顺序,图像在顶部,身体在底部。

  • 我澄清说,我在 ViewController 之外做所有事情,从 AppDelegade 火力库用于获取这些数据,从而在另一个类中本地创建通知。
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
                     fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
      registerDataNotification(didReceiveRemoteNotification: userInfo)
      completionHandler(UIBackgroundFetchResult.newData)
    }
fileprivate func triggerNotification(image: String?, id: String?, title: String?, body: String?, visualizationMode: VisualizationMode){
        let content = UNMutableNotificationContent()
        content.title = title!
        content.body = body!
        content.badge = 2
        content.sound = UNNotificationSound.default()
        if image == nil || image?.count==0 {
            notifyUser(id, content)
        }else{
            do {
                let fileUrl = URL(string: image!)
                let data = try Data(contentsOf: fileUrl!)
                var uIImage = UIImage(data: data)
                uIImage = visualizationMode.rawValue.contains("Text") ? uIImage!.resized(toWidth: uIImage!.size.width) : uIImage
                if let attachment = UNNotificationAttachment.create(identifier: id!, image: uIImage!, options: nil) {
                    content.attachments = [attachment]
                }
                notifyUser(id, content)
            }
            catch{
                print(error.localizedDescription)
                notifyUser(id, content)
            }
        }
    }
fileprivate func notifyUser(_ id: String?, _ content: UNMutableNotificationContent) {
        let masivNotification = MasivNotification(didReceiveRemoteNotification: jsonCleanUserInfo)
        eventService.registerEvent(masivNotification: masivNotification, masivEventType: MasivEventType.Received, token: token,
                                           country: country, masivPlatform: masivPlatform.Ios, externalAppId: externalAppId)
        let request = UNNotificationRequest.init(identifier: (id)!, content: content, trigger: nil)
        UNUserNotificationCenter.current().add(request)
    }
extension UNNotificationAttachment {

    static func create(identifier: String, image: UIImage, options: [NSObject : AnyObject]?) -> UNNotificationAttachment? {
        let fileManager = FileManager.default
        let tmpSubFolderName = ProcessInfo.processInfo.globallyUniqueString
        let tmpSubFolderURL = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(tmpSubFolderName, isDirectory: true)
        do {
            try fileManager.createDirectory(at: tmpSubFolderURL, withIntermediateDirectories: true, attributes: nil)
            let imageFileIdentifier = identifier+".png"
            let fileURL = tmpSubFolderURL.appendingPathComponent(imageFileIdentifier)
            let imageData = UIImagePNGRepresentation(image)
            try imageData?.write(to: fileURL)
            let imageAttachment = try UNNotificationAttachment.init(identifier: imageFileIdentifier, url: fileURL, options: options)
            return imageAttachment
        } catch {
            print("error " + error.localizedDescription)
        }
        return nil
    }
}
4

0 回答 0