xcode 未调用SwiftUI应用程序协议上的通知服务扩展

00jrzges  于 2023-08-07  发布在  Swift
关注(0)|答案(1)|浏览(120)

我正在尝试在SwiftUI应用协议(iOS 16)上实现NotificationService扩展。常规推送通知可以工作,我在服务器上创建推送通知时提交了hasMutableContent标志(true)。
在iOS App的Xcode中,我选择了File -> new -> Target,它创建了NotificationServiceinfo.plist文件。我在新生成的类NotificationService: UNNotificationServiceExtension中设置了一些断点,但它们从未到达。断点将显示蓝色虚线轮廓。部署目标在Xcode中设置为16.4,我连接的设备在16.5.1上。在主应用程序文件中,我必须做一些额外的工作才能访问appDelegate,例如。

@UIApplicationDelegateAdaptor(AppDelegate.self) private var appDelegate: AppDelegate

字符串
NotificationService: UNNotificationServiceExtension也需要一些额外的工作吗?
1.在目标下,我的应用的目标-> Build Phases-> Embed Foundation Extension-> Copy only when installing未选中,通知服务也在这里列出。

第2点和第3点。不需要

1.我已经更改了生成的NotificationService文件的Target Membership,现在两个复选框处于活动状态,用于通知本身(默认情况下处于活动状态),我在这里检查了主应用程序。断点不再是蓝色圆点。
1.我将生成的Info.plist的内容复制到我的应用程序Info.plist中。

断点问题:见下面的Gruntcakes评论

不幸的是,仍然没有达到断点。我猜这个问题可能与这是一个SwiftUI应用程序有关,它使用了较新的SwiftUI应用程序协议,其中您获得了@main注解。

@main
struct MyApp: App {


但是我必须做什么才能让NotificationService extension工作呢?

sh7euo9m

sh7euo9m1#

写下这段代码,运行并在notificationService类上设置断点,然后一旦通知登陆到应用程序上,它就会登陆这个类,只需将您的通知服务类替换为以下代码并运行代码,希望它能解决您的问题。
作为参考,你也可以看到这个链接
https://medium.com/gits-apps-insight/processing-notification-data-using-notification-service-extension-6a2b5ea2da17

import UserNotifications

class NotificationService: UNNotificationServiceExtension {

    var contentHandler: ((UNNotificationContent) -> Void)?
    var bestAttemptContent: UNMutableNotificationContent?

    override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
        self.contentHandler = contentHandler
        debugPrint(request.content)
        bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
        
        if let bestAttemptContent = bestAttemptContent {
            // Modify the notification content here...
            bestAttemptContent.title = "\(bestAttemptContent.title )"
            if let attachment = request.attachment{
                bestAttemptContent.attachments = [attachment]
            }
            
            contentHandler(bestAttemptContent)
        }
        
    }
    
    override func serviceExtensionTimeWillExpire() {
        // Called just before the extension will be terminated by the system.
        // Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
        if let contentHandler = contentHandler, let bestAttemptContent =  bestAttemptContent {
            contentHandler(bestAttemptContent)
        }
    }

}

extension UNNotificationRequest {
    var attachment: UNNotificationAttachment? {
        
//        guard let fcmOptions = content.userInfo["fcm_options"] as? [String:Any] else {return nil}
        
        guard let attachmentURL = content.userInfo["image"] as? String, let imageData = try? Data(contentsOf: URL(string: attachmentURL)!) else {
            return nil
        }
        return try? UNNotificationAttachment(data: imageData, options: nil)
    }
}

extension UNNotificationAttachment {

    convenience init(data: Data, options: [NSObject: AnyObject]?) throws {
        let fileManager = FileManager.default
        let temporaryFolderName = ProcessInfo.processInfo.globallyUniqueString
        let temporaryFolderURL = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(temporaryFolderName, isDirectory: true)

        try fileManager.createDirectory(at: temporaryFolderURL, withIntermediateDirectories: true, attributes: nil)
        let imageFileIdentifier = UUID().uuidString + ".jpg"
        let fileURL = temporaryFolderURL.appendingPathComponent(imageFileIdentifier)
        try data.write(to: fileURL)
        try self.init(identifier: imageFileIdentifier, url: fileURL, options: options)
    }
}

字符串

相关问题