为什么通知服务扩展未在iOS 14上启动? [英] Why notification service extension is not firing on iOS 14?
问题描述
我已经集成了
如果要调试Notification Service扩展,则必须从下拉列表中选择Notification Service Extension方案,然后断点将为您工作
I have integrated UNNotificationServiceExtension which allows me to change the push notification title before it's presented to the user.
I have followed the guidelines mentioned in apple developer documentation and also browsed related questions at SO, But nothing seems to be working.
As directed, i have followed these guidelines
- Include "mutable-content": 1 in your push notification payload
- Extension's deployment target should be set correctly (should match with main app target)
- The payload must include an alert dictionary with title, subtitle, or body information
- The 'aps' dictionary in payload of the push notification must include a key 'category' with a string value.
The problem
My notification service extension is not triggered sometimes when push notification is received, mostly when i delete and install a fresh copy of app. Breakpoints also don't get triggered in this case. It looks like system forgets to fire the service extension. I have selected the correct scheme of extension and not the main app target. My notification title is not updated as per my logic.
Note: This is occurring on iOS 14. on iOS 12, it is working fine. Is this an iOS bug?
This is already being discussed in these threads.
https://developer.apple.com/forums/thread/67202 https://developer.apple.com/forums/thread/125987
Any help would be much appreciated. Thanks
Sample Code
I made a sample project to demonstrate this issue. You can download it from Github Repo
Related WWDC Video
Best Practices and What’s New in User Notifications
You have to modify the UNNotificationRequest content part. Below is the code snippet which I have used for my notification extension.
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
}
}
The JSON payload which I have used
{
"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"
}
with your code repo the output which I am getting is as
If you want to debug the Notification Service extension then you have to select the Notification Service Extension scheme from the drop down and then your breakpoints will work for you
这篇关于为什么通知服务扩展未在iOS 14上启动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!