Skip to content

Commit

Permalink
Added priority support
Browse files Browse the repository at this point in the history
  • Loading branch information
binwiederhier committed May 25, 2022
1 parent dd04ecd commit 5b64f61
Show file tree
Hide file tree
Showing 15 changed files with 115 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
filePath = "ntfyNSE/NotificationService.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "38"
endingLineNumber = "38"
startingLineNumber = "58"
endingLineNumber = "58"
landmarkName = "didReceive(_:withContentHandler:)"
landmarkType = "7">
</BreakpointContent>
Expand Down Expand Up @@ -78,8 +78,8 @@
filePath = "ntfyNSE/NotificationService.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "37"
endingLineNumber = "37"
startingLineNumber = "57"
endingLineNumber = "57"
landmarkName = "didReceive(_:withContentHandler:)"
landmarkType = "7">
</BreakpointContent>
Expand Down
12 changes: 12 additions & 0 deletions ntfy/Assets.xcassets/priority-1.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"images" : [
{
"filename" : "priority-1.svg",
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
1 change: 1 addition & 0 deletions ntfy/Assets.xcassets/priority-1.imageset/priority-1.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions ntfy/Assets.xcassets/priority-2.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"images" : [
{
"filename" : "priority-2.svg",
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
1 change: 1 addition & 0 deletions ntfy/Assets.xcassets/priority-2.imageset/priority-2.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions ntfy/Assets.xcassets/priority-3.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"images" : [
{
"filename" : "priority-3.svg",
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
1 change: 1 addition & 0 deletions ntfy/Assets.xcassets/priority-3.imageset/priority-3.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions ntfy/Assets.xcassets/priority-4.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"images" : [
{
"filename" : "priority-4.svg",
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
1 change: 1 addition & 0 deletions ntfy/Assets.xcassets/priority-4.imageset/priority-4.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions ntfy/Assets.xcassets/priority-5.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"images" : [
{
"filename" : "priority-5.svg",
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
1 change: 1 addition & 0 deletions ntfy/Assets.xcassets/priority-5.imageset/priority-5.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions ntfy/Persistence/Notification.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ struct Message: Decodable {
var time: Int64
var message: String?
var title: String?
var priority: Int16?
}
30 changes: 13 additions & 17 deletions ntfy/Persistence/Store.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,20 +89,14 @@ class Store: ObservableObject {
Log.d(Store.tag, "Subscription for topic \(topic) unknown")
return
}

do {
let notification = Notification(context: context)
notification.id = id
notification.time = timeInt
notification.message = message
notification.title = userInfo["title"] as? String ?? ""
subscription.addToNotifications(notification)
subscription.lastNotificationId = id
try context.save()
} catch let error {
Log.w(Store.tag, "Cannot store notification (fromUserInfo)", error)
rollbackAndRefresh()
}
let m = Message(
id: id,
time: timeInt,
message: message,
title: userInfo["title"] as? String ?? "",
priority: Int16(userInfo["priority"] as? String ?? "3") ?? 3
)
save(notificationFromMessage: m, withSubscription: subscription)
}

func save(notificationFromMessage message: Message, withSubscription subscription: Subscription) {
Expand All @@ -112,6 +106,7 @@ class Store: ObservableObject {
notification.time = message.time
notification.message = message.message ?? ""
notification.title = message.title ?? ""
notification.priority = (message.priority != nil && message.priority != 0) ? message.priority! : 3
subscription.addToNotifications(notification)
subscription.lastNotificationId = message.id
try context.save()
Expand Down Expand Up @@ -177,9 +172,9 @@ class Store: ObservableObject {
extension Store {
static let sampleData = [
"stats": [
Message(id: "1", time: 1653048956, message: "In the last 24 hours, hyou had 5,000 users across 13 countries visit your website", title: "Record visitor numbers"),
Message(id: "2", time: 1653058956, message: "201 users/h\n80 IPs", title: "This is a title"),
Message(id: "3", time: 1643058956, message: "This message does not have a title, but is instead super long. Like really really long. It can't be any longer I think. I mean, there is s 4,000 byte limit of the message, so I guess I have to make this 4,000 bytes long. Or do I? 😁 I don't know. It's quite tedious to come up with something so long, so I'll stop now. Bye!", title: nil)
Message(id: "1", time: 1653048956, message: "In the last 24 hours, hyou had 5,000 users across 13 countries visit your website", title: "Record visitor numbers", priority: 4),
Message(id: "2", time: 1653058956, message: "201 users/h\n80 IPs", title: "This is a title", priority: 1),
Message(id: "3", time: 1643058956, message: "This message does not have a title, but is instead super long. Like really really long. It can't be any longer I think. I mean, there is s 4,000 byte limit of the message, so I guess I have to make this 4,000 bytes long. Or do I? 😁 I don't know. It's quite tedious to come up with something so long, so I'll stop now. Bye!", title: nil, priority: 5)
],
"backups": [],
"announcements": [],
Expand Down Expand Up @@ -218,6 +213,7 @@ extension Store {
notification.time = message.time
notification.message = message.message
notification.title = message.title
notification.priority = message.priority ?? 3
return notification
}
}
14 changes: 11 additions & 3 deletions ntfy/Views/NotificationListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,17 @@ struct NotificationRowView: View {

var body: some View {
VStack(alignment: .leading, spacing: 0) {
Text(notification.shortDateTime())
.font(.subheadline)
.foregroundColor(.gray)
HStack(alignment: .center, spacing: 2) {
Text(notification.shortDateTime())
.font(.subheadline)
.foregroundColor(.gray)
if [1,2,4,5].contains(notification.priority) {
Image("priority-\(notification.priority)")
.resizable()
.scaledToFit()
.frame(width: 16, height: 16)
}
}
if let title = notification.title, title != "" {
Text(title)
.font(.headline)
Expand Down
22 changes: 21 additions & 1 deletion ntfyNSE/NotificationService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import CoreData
/// Note that the app extension does not run as part of the main app, so log messages are not printed in the main Xcode window. To debug,
/// select Debug -> Attach to Process by PID or Name, and select the extension. Don't forget to set a breakpoint, or you're not gonna have a good time.
class NotificationService: UNNotificationServiceExtension {
let tag = "NotificationService"
private let tag = "NotificationService"

var contentHandler: ((UNNotificationContent) -> Void)?
var bestAttemptContent: UNMutableNotificationContent?
Expand All @@ -33,6 +33,26 @@ class NotificationService: UNNotificationServiceExtension {
// Play a sound, and group by topic
bestAttemptContent.sound = .default
bestAttemptContent.threadIdentifier = userInfo["topic"] as? String ?? ""

// Map priorities to interruption level (light up screen, ...) and relevance (order)
let priority = userInfo["priority"] as? String ?? "3"
switch priority {
case "1":
bestAttemptContent.interruptionLevel = .passive
bestAttemptContent.relevanceScore = 0
case "2":
bestAttemptContent.interruptionLevel = .passive
bestAttemptContent.relevanceScore = 0.25
case "4":
bestAttemptContent.interruptionLevel = .timeSensitive
bestAttemptContent.relevanceScore = 0.75
case "5":
bestAttemptContent.interruptionLevel = .critical
bestAttemptContent.relevanceScore = 1
default:
bestAttemptContent.interruptionLevel = .active
bestAttemptContent.relevanceScore = 0.5
}

// Save notification to store, and display it
Store.shared.save(notificationFromUserInfo: userInfo)
Expand Down

0 comments on commit 5b64f61

Please sign in to comment.