[英]Why notification service extension is not firing on iOS 14?
我已經集成了 UNNotificationServiceExtension ,它允許我在推送通知標題呈現給用戶之前更改它。
我遵循了蘋果開發者文檔中提到的指南,並在 SO 上瀏覽了相關問題,但似乎沒有任何效果。
收到推送通知時,有時不會觸發我的通知服務擴展,主要是在我刪除並安裝新的應用程序副本時。 在這種情況下也不會觸發斷點。 看起來系統忘記觸發服務擴展。 我選擇了正確的擴展方案,而不是主應用程序目標。 我的通知標題沒有按照我的邏輯更新。
注意:這發生在 iOS 14 上。在 iOS 12 上,它工作正常。 這是 iOS錯誤嗎?
這已經在這些線程中進行了討論。
https://developer.apple.com/forums/thread/67202 https://developer.apple.com/forums/thread/125987
任何幫助將非常感激。 謝謝
我做了一個示例項目來演示這個問題。 您可以從Github Repo下載它
您必須修改 UNNotificationRequest 內容部分。 下面是我用於通知擴展的代碼片段。
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
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
if let bestAttemptContent = bestAttemptContent {
var urlString:String? = nil
if let urlImageString = request.content.userInfo["urlImageString"] as? String {
urlString = urlImageString
}
// You can set what ever title you want to set
bestAttemptContent.title = "Your custom tile goes here"
if urlString != nil, let fileUrl = URL(string: urlString!) {
print("fileUrl: \(fileUrl)")
guard let imageData = NSData(contentsOf: fileUrl) else {
contentHandler(bestAttemptContent)
return
}
guard let attachment = UNNotificationAttachment.saveImageToDisk(fileIdentifier: "image.jpg", data: imageData, options: nil) else {
print("error in UNNotificationAttachment.saveImageToDisk()")
contentHandler(bestAttemptContent)
return
}
bestAttemptContent.attachments = [ attachment ]
}
contentHandler(bestAttemptContent)
}
}
override func serviceExtensionTimeWillExpire() {
if let contentHandler = contentHandler, let bestAttemptContent = bestAttemptContent {
contentHandler(bestAttemptContent)
}
}
}
@available(iOSApplicationExtension 10.0, *)
extension UNNotificationAttachment {
static func saveImageToDisk(fileIdentifier: String, data: NSData, options: [NSObject : AnyObject]?) -> UNNotificationAttachment? {
let fileManager = FileManager.default
let folderName = ProcessInfo.processInfo.globallyUniqueString
let folderURL = NSURL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(folderName, isDirectory: true)
do {
try fileManager.createDirectory(at: folderURL!, withIntermediateDirectories: true, attributes: nil)
let fileURL = folderURL?.appendingPathComponent(fileIdentifier)
try data.write(to: fileURL!, options: [])
let attachment = try UNNotificationAttachment(identifier: fileIdentifier, url: fileURL!, options: options)
return attachment
} catch let error {
print("error \(error)")
}
return nil
}
}
我使用過的 JSON 有效載荷
{
"aps": {
"alert": {
"title": "title",
"body": "Your message Here"
},
"mutable-content": 1
},
"urlImageString": "https://cdn.pixabay.com/photo/2015/04/23/22/00/tree-736885__340.jpg"
}
如果要調試通知服務擴展,則必須從下拉列表中選擇 select 通知服務擴展方案,然后您的斷點將為您工作
我已為此問題提交了技術支持事件。 我在通知服務擴展上執行了廣泛的 R & D。 根據蘋果文檔在 vanila 項目中實現它。 我們的配置和一切似乎都是正確的。 所以我就這個問題請求了蘋果技術支持並得到了這個回復。
由於 iOS 中的錯誤,我們已確定您看到此問題。 請使用反饋助手 ( https://feedbackassistant.apple.com ) 提交有關此問題的完整錯誤報告。 有關反饋助手的更多信息,請訪問https://developer.apple.com/bug-reporting/ 。
請在錯誤報告中再次包含您提供給我的所有信息(您可以將日志直接附加到錯誤報告中)。 請確保遵循https://developer.apple.com/bug-reporting/profiles-and-logs/上的說明,並按照 APNS(Apple 推送通知服務)的說明在您的設備上安裝日志記錄配置文件,並且確保在重現問題之前已安裝它。
重要提示:在反饋助手中,當提交有關 macOS、iOS、watchOS 或 tvOS 上的 API/SDK 的錯誤報告時,請在 select 適當的 API/SDK 區域(在“不在此列表中的其他內容”之后)提交錯誤報告。
所以,我要向蘋果提交一份錯誤報告。 一旦得到有關錯誤報告的答復,我將更新此答案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.