2019-10-03 02:42:16 +02:00
|
|
|
//
|
|
|
|
// NotificationManager.swift
|
|
|
|
// NetNewsWire
|
|
|
|
//
|
|
|
|
// Created by Maurice Parker on 10/2/19.
|
|
|
|
// Copyright © 2019 Ranchero Software. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import Account
|
|
|
|
import Articles
|
|
|
|
import UserNotifications
|
|
|
|
|
|
|
|
final class UserNotificationManager: NSObject {
|
|
|
|
|
|
|
|
override init() {
|
|
|
|
super.init()
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(accountDidDownloadArticles(_:)), name: .AccountDidDownloadArticles, object: nil)
|
2019-10-04 18:20:57 +02:00
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(statusesDidChange(_:)), name: .StatusesDidChange, object: nil)
|
2020-12-23 12:57:33 +01:00
|
|
|
registerCategoriesAndActions()
|
2019-10-03 02:42:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@objc func accountDidDownloadArticles(_ note: Notification) {
|
|
|
|
guard let articles = note.userInfo?[Account.UserInfoKey.newArticles] as? Set<Article> else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for article in articles {
|
2019-11-15 03:11:41 +01:00
|
|
|
if !article.status.read, let webFeed = article.webFeed, webFeed.isNotifyAboutNewArticles ?? false {
|
|
|
|
sendNotification(webFeed: webFeed, article: article)
|
2019-10-03 02:42:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-04 18:20:57 +02:00
|
|
|
@objc func statusesDidChange(_ note: Notification) {
|
2021-02-08 00:46:24 +01:00
|
|
|
guard let statuses = note.userInfo?[Account.UserInfoKey.statuses] as? Set<ArticleStatus>, !statuses.isEmpty else {
|
2019-10-04 18:20:57 +02:00
|
|
|
return
|
|
|
|
}
|
2021-02-08 00:46:24 +01:00
|
|
|
let identifiers = statuses.filter({ $0.read }).map { "articleID:\($0.articleID)" }
|
2019-10-04 18:20:57 +02:00
|
|
|
UNUserNotificationCenter.current().removeDeliveredNotifications(withIdentifiers: identifiers)
|
|
|
|
}
|
|
|
|
|
2019-10-03 02:42:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private extension UserNotificationManager {
|
|
|
|
|
2020-12-23 12:57:33 +01:00
|
|
|
func sendNotification(webFeed: WebFeed, article: Article) {
|
2019-10-03 02:42:16 +02:00
|
|
|
let content = UNMutableNotificationContent()
|
2019-10-03 16:53:21 +02:00
|
|
|
|
2019-11-15 03:11:41 +01:00
|
|
|
content.title = webFeed.nameForDisplay
|
2020-05-15 13:53:44 +02:00
|
|
|
if !ArticleStringFormatter.truncatedTitle(article).isEmpty {
|
|
|
|
content.subtitle = ArticleStringFormatter.truncatedTitle(article)
|
2019-10-03 16:53:21 +02:00
|
|
|
}
|
2020-05-15 13:53:44 +02:00
|
|
|
content.body = ArticleStringFormatter.truncatedSummary(article)
|
|
|
|
content.threadIdentifier = webFeed.webFeedID
|
|
|
|
content.summaryArgument = "\(webFeed.nameForDisplay)"
|
|
|
|
content.summaryArgumentCount = 1
|
2020-05-15 14:32:33 +02:00
|
|
|
content.sound = UNNotificationSound.default
|
|
|
|
content.userInfo = [UserInfoKey.articlePath: article.pathUserInfo]
|
2020-12-23 14:18:51 +01:00
|
|
|
content.categoryIdentifier = "NEW_ARTICLE_NOTIFICATION_CATEGORY"
|
2020-12-23 12:57:33 +01:00
|
|
|
if let attachment = thumbnailAttachment(for: article, webFeed: webFeed) {
|
|
|
|
content.attachments.append(attachment)
|
|
|
|
}
|
2020-05-15 14:32:33 +02:00
|
|
|
|
|
|
|
let request = UNNotificationRequest.init(identifier: "articleID:\(article.articleID)", content: content, trigger: nil)
|
2019-10-03 02:42:16 +02:00
|
|
|
UNUserNotificationCenter.current().add(request)
|
|
|
|
}
|
|
|
|
|
2020-12-23 12:57:33 +01:00
|
|
|
/// 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? {
|
2020-12-24 00:31:44 +01:00
|
|
|
if let imageURL = article.iconImageUrl(webFeed: webFeed) {
|
|
|
|
let thumbnail = try? UNNotificationAttachment(identifier: webFeed.webFeedID, url: imageURL, options: nil)
|
2020-12-23 12:57:33 +01:00
|
|
|
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: "",
|
2021-01-24 17:21:34 +01:00
|
|
|
options: [])
|
2020-12-23 12:57:33 +01:00
|
|
|
|
|
|
|
UNUserNotificationCenter.current().setNotificationCategories([newArticleCategory])
|
|
|
|
}
|
|
|
|
|
2019-10-03 02:42:16 +02:00
|
|
|
}
|