adds icons and actions to notifications

This commit is contained in:
Stuart Breckenridge 2020-12-23 19:57:33 +08:00
parent 5a5d8ee01c
commit 709bafbe62
No known key found for this signature in database
GPG Key ID: 1F11FD62007DC331
1 changed files with 43 additions and 5 deletions

View File

@ -17,6 +17,7 @@ final class UserNotificationManager: NSObject {
super.init() super.init()
NotificationCenter.default.addObserver(self, selector: #selector(accountDidDownloadArticles(_:)), name: .AccountDidDownloadArticles, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(accountDidDownloadArticles(_:)), name: .AccountDidDownloadArticles, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(statusesDidChange(_:)), name: .StatusesDidChange, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(statusesDidChange(_:)), name: .StatusesDidChange, object: nil)
registerCategoriesAndActions()
} }
@objc func accountDidDownloadArticles(_ note: Notification) { @objc func accountDidDownloadArticles(_ note: Notification) {
@ -43,26 +44,63 @@ final class UserNotificationManager: NSObject {
private extension UserNotificationManager { private extension UserNotificationManager {
private func sendNotification(webFeed: WebFeed, article: Article) { func sendNotification(webFeed: WebFeed, article: Article) {
let content = UNMutableNotificationContent() let content = UNMutableNotificationContent()
content.title = webFeed.nameForDisplay content.title = webFeed.nameForDisplay
if !ArticleStringFormatter.truncatedTitle(article).isEmpty { if !ArticleStringFormatter.truncatedTitle(article).isEmpty {
content.subtitle = ArticleStringFormatter.truncatedTitle(article) content.subtitle = ArticleStringFormatter.truncatedTitle(article)
} }
content.body = ArticleStringFormatter.truncatedSummary(article) content.body = ArticleStringFormatter.truncatedSummary(article)
content.threadIdentifier = webFeed.webFeedID content.threadIdentifier = webFeed.webFeedID
content.summaryArgument = "\(webFeed.nameForDisplay)" content.summaryArgument = "\(webFeed.nameForDisplay)"
content.summaryArgumentCount = 1 content.summaryArgumentCount = 1
content.sound = UNNotificationSound.default content.sound = UNNotificationSound.default
content.userInfo = [UserInfoKey.articlePath: article.pathUserInfo] content.userInfo = [UserInfoKey.articlePath: article.pathUserInfo]
if let attachment = thumbnailAttachment(for: article, webFeed: webFeed) {
content.attachments.append(attachment)
}
let request = UNNotificationRequest.init(identifier: "articleID:\(article.articleID)", content: content, trigger: nil) let request = UNNotificationRequest.init(identifier: "articleID:\(article.articleID)", content: content, trigger: nil)
UNUserNotificationCenter.current().add(request) UNUserNotificationCenter.current().add(request)
} }
/// Determine if there is an available icon for the article. This will then move it to the caches directory and make it avialble for the notification.
/// - Parameters:
/// - article: `Article`
/// - webFeed: `WebFeed`
/// - Returns: A `UNNotifcationAttachment` if an icon is available. Otherwise nil.
/// - Warning: In certain scenarios, this will return the `faviconTemplateImage`.
func thumbnailAttachment(for article: Article, webFeed: WebFeed) -> UNNotificationAttachment? {
if let image = article.iconImage() {
let fm = FileManager.default
var path = fm.urls(for: .cachesDirectory, in: .userDomainMask)[0]
#if os(macOS)
path.appendPathComponent(webFeed.webFeedID + "_smallIcon.tiff")
#else
path.appendPathComponent(webFeed.webFeedID + "_smallIcon.png")
#endif
fm.createFile(atPath: path.path, contents: image.image.dataRepresentation()!, attributes: nil)
let thumbnail = try? UNNotificationAttachment(identifier: webFeed.webFeedID, url: path, options: nil)
return thumbnail
}
return nil
}
func registerCategoriesAndActions() {
let readAction = UNNotificationAction(identifier: "MARK_AS_READ", title: NSLocalizedString("Mark as Read", comment: "Mark as Read"), options: [])
let starredAction = UNNotificationAction(identifier: "MARK_AS_STARRED", title: NSLocalizedString("Mark as Starred", comment: "Mark as Starred"), options: [])
let openAction = UNNotificationAction(identifier: "OPEN_ARTICLE", title: NSLocalizedString("Open", comment: "Open"), options: [.foreground])
let newArticleCategory =
UNNotificationCategory(identifier: "NEW_ARTICLE_NOTIFICATION_CATEGORY",
actions: [openAction, readAction, starredAction],
intentIdentifiers: [],
hiddenPreviewsBodyPlaceholder: "",
options: .customDismissAction)
UNUserNotificationCenter.current().setNotificationCategories([newArticleCategory])
}
} }