2019-10-02 19:42:16 -05: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
2025-11-13 17:11:34 -08:00
@ MainActor final class UserNotificationManager {
2025-11-03 20:50:02 -08:00
static let shared = UserNotificationManager ()
static private let notificationCategory = "NEW_ARTICLE_NOTIFICATION_CATEGORY"
struct ActionIdentifier {
static let markAsRead = "MARK_AS_READ"
static let markAsStarred = "MARK_AS_STARRED"
static let openArticle = "OPEN_ARTICLE"
}
@ MainActor private var isActive = false
@ MainActor func start () {
guard ! isActive else {
assertionFailure ( "start called when already active" )
return
}
2019-10-02 19:42:16 -05:00
NotificationCenter . default . addObserver ( self , selector : #selector ( accountDidDownloadArticles ( _ :)), name : . AccountDidDownloadArticles , object : nil )
2019-10-04 11:20:57 -05:00
NotificationCenter . default . addObserver ( self , selector : #selector ( statusesDidChange ( _ :)), name : . StatusesDidChange , object : nil )
2020-12-23 19:57:33 +08:00
registerCategoriesAndActions ()
2025-11-03 20:50:02 -08:00
isActive = true
2019-10-02 19:42:16 -05:00
}
2025-11-03 20:50:02 -08:00
2019-10-02 19:42:16 -05:00
@objc func accountDidDownloadArticles ( _ note : Notification ) {
guard let articles = note . userInfo ?[ Account . UserInfoKey . newArticles ] as ? Set < Article > else {
return
}
2025-11-05 11:22:10 -08:00
2019-10-02 19:42:16 -05:00
for article in articles {
2025-11-03 10:16:03 -08:00
if ! article . status . read , let feed = article . feed , feed . isNotifyAboutNewArticles ?? false {
sendNotification ( feed : feed , article : article )
2019-10-02 19:42:16 -05:00
}
}
}
2019-10-04 11:20:57 -05:00
@objc func statusesDidChange ( _ note : Notification ) {
2022-02-27 09:55:59 -08:00
if let statuses = note . userInfo ?[ Account . UserInfoKey . statuses ] as ? Set < ArticleStatus >, ! statuses . isEmpty {
let identifiers = statuses . filter ({ $0 . read }). map { "articleID: \( $0 . articleID ) " }
UNUserNotificationCenter . current (). removeDeliveredNotifications ( withIdentifiers : identifiers )
2019-10-04 11:20:57 -05:00
return
}
2025-11-05 11:22:10 -08:00
2022-02-27 09:55:59 -08:00
if let articleIDs = note . userInfo ?[ Account . UserInfoKey . articleIDs ] as ? Set < String >,
let statusKey = note . userInfo ?[ Account . UserInfoKey . statusKey ] as ? ArticleStatus . Key ,
let flag = note . userInfo ?[ Account . UserInfoKey . statusFlag ] as ? Bool ,
statusKey == . read ,
flag == true {
let identifiers = articleIDs . map { "articleID: \( $0 ) " }
UNUserNotificationCenter . current (). removeDeliveredNotifications ( withIdentifiers : identifiers )
}
2019-10-04 11:20:57 -05:00
}
2019-10-02 19:42:16 -05:00
}
private extension UserNotificationManager {
2025-11-03 20:50:02 -08:00
2025-11-03 10:16:03 -08:00
func sendNotification ( feed : Feed , article : Article ) {
2019-10-02 19:42:16 -05:00
let content = UNMutableNotificationContent ()
2025-11-05 11:22:10 -08:00
2025-11-03 10:16:03 -08:00
content . title = feed . nameForDisplay
2020-05-15 19:53:44 +08:00
if ! ArticleStringFormatter . truncatedTitle ( article ). isEmpty {
content . subtitle = ArticleStringFormatter . truncatedTitle ( article )
2019-10-03 09:53:21 -05:00
}
2020-05-15 19:53:44 +08:00
content . body = ArticleStringFormatter . truncatedSummary ( article )
2025-11-03 10:16:03 -08:00
content . threadIdentifier = feed . feedID
2020-05-15 20:32:33 +08:00
content . sound = UNNotificationSound . default
content . userInfo = [ UserInfoKey . articlePath : article . pathUserInfo ]
2025-11-03 20:50:02 -08:00
content . categoryIdentifier = Self . notificationCategory
2025-11-03 10:16:03 -08:00
if let attachment = thumbnailAttachment ( for : article , feed : feed ) {
2020-12-23 19:57:33 +08:00
content . attachments . append ( attachment )
}
2025-11-05 11:22:10 -08:00
2020-05-15 20:32:33 +08:00
let request = UNNotificationRequest . init ( identifier : "articleID: \( article . articleID ) " , content : content , trigger : nil )
2019-10-02 19:42:16 -05:00
UNUserNotificationCenter . current (). add ( request )
}
2025-11-05 11:22:10 -08:00
2020-12-23 19:57:33 +08: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`
2025-11-03 10:16:03 -08:00
/// - feed: `Feed`
2020-12-23 19:57:33 +08:00
/// - Returns: A `UNNotifcationAttachment` if an icon is available. Otherwise nil.
/// - Warning: In certain scenarios, this will return the `faviconTemplateImage`.
2025-11-03 10:16:03 -08:00
func thumbnailAttachment ( for article : Article , feed : Feed ) -> UNNotificationAttachment ? {
if let imageURL = article . iconImageUrl ( feed : feed ) {
let thumbnail = try ? UNNotificationAttachment ( identifier : feed . feedID , url : imageURL , options : nil )
2020-12-23 19:57:33 +08:00
return thumbnail
}
return nil
}
2025-11-05 11:22:10 -08:00
2020-12-23 19:57:33 +08:00
func registerCategoriesAndActions () {
2025-11-03 20:50:02 -08:00
let readAction = UNNotificationAction ( identifier : ActionIdentifier . markAsRead , title : NSLocalizedString ( "Mark as Read" , comment : "Mark as Read" ), options : [])
let starredAction = UNNotificationAction ( identifier : ActionIdentifier . markAsStarred , title : NSLocalizedString ( "Mark as Starred" , comment : "Mark as Starred" ), options : [])
let openAction = UNNotificationAction ( identifier : ActionIdentifier . openArticle , title : NSLocalizedString ( "Open" , comment : "Open" ), options : [. foreground ])
let newArticleCategory = UNNotificationCategory ( identifier : Self . notificationCategory ,
actions : [ openAction , readAction , starredAction ],
intentIdentifiers : [],
hiddenPreviewsBodyPlaceholder : "" ,
options : [])
2020-12-23 19:57:33 +08:00
UNUserNotificationCenter . current (). setNotificationCategories ([ newArticleCategory ])
}
2019-10-02 19:42:16 -05:00
}