Skip to content

Commit

Permalink
添加 subtitle 支持。close: #48
Browse files Browse the repository at this point in the history
  • Loading branch information
Finb committed Dec 11, 2024
1 parent d97abb5 commit f155f65
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Common/RealmConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ let kRealmDefaultConfiguration = {
let fileUrl = groupUrl?.appendingPathComponent("bark.realm")
let config = Realm.Configuration(
fileURL: fileUrl,
schemaVersion: 14,
schemaVersion: 15,
migrationBlock: { migration, oldSchemaVersion in
switch oldSchemaVersion {
case 0...13:
Expand Down
2 changes: 1 addition & 1 deletion Controller/MessageListViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class MessageListViewModel: ViewModel, ViewModelType {
results = results.filter("group in %@", filterGroups)
}
if let text = searchText, text.count > 0 {
results = results.filter("title CONTAINS[c] %@ OR body CONTAINS[c] %@", text, text)
results = results.filter("title CONTAINS[c] %@ OR subtitle CONTAINS[c] %@ OR body CONTAINS[c] %@", text, text, text)
}
return results
}
Expand Down
1 change: 1 addition & 0 deletions Model/Message.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import UIKit
class Message: Object {
@objc dynamic var id = NSUUID().uuidString
@objc dynamic var title: String?
@objc dynamic var subtitle: String?
@objc dynamic var body: String?
@objc dynamic var url: String?
@objc dynamic var group: String?
Expand Down
2 changes: 2 additions & 0 deletions NotificationServiceExtension/Processor/ArchiveProcessor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@ class ArchiveProcessor: NotificationContentProcessor {
if isArchive {
let alert = (userInfo["aps"] as? [String: Any])?["alert"] as? [String: Any]
let title = alert?["title"] as? String
let subtitle = alert?["subtitle"] as? String
let body = alert?["body"] as? String
let url = userInfo["url"] as? String
let group = userInfo["group"] as? String

try? realm?.write {
let message = Message()
message.title = title
message.subtitle = subtitle
message.body = body
message.url = url
message.group = group
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ class CiphertextProcessor: NotificationContentProcessor {
bestAttemptContent.title = title
alert["title"] = title
}
if let subtitle = map["subtitle"] as? String {
bestAttemptContent.title = subtitle
alert["subtitle"] = subtitle
}
if let body = map["body"] as? String {
bestAttemptContent.body = body
alert["body"] = body
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ You can send GET or POST requests, and you'll receive a push notification immedi
URL structure: The first part is the key, followed by three matches
/:key/:body
/:key/:title/:body
/:key/:category/:title/:body
/:key/: title/:subtitle/:body
title: The push title, slightly larger than the body text
subtitle: The push subtitle
body: The push content, use the newline character '\n' for line breaks
category: Reserved for additional features, currently not open for use, just ignore it
For POST requests, the parameter names are the same as above
```

Expand Down
4 changes: 2 additions & 2 deletions README.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ Bark 支持 iOS 通知的多项高级特性,包括推送分组、定制推送
URL 组成: 第一个部分是 key , 之后有三个匹配
/:key/:body
/:key/:title/:body
/:key/:category/:title/:body
/:key/:title/:subtitle/:body
title 推送标题 比 body 字号粗一点
subtitle 推送副标题
body 推送内容 换行请使用换行符 '\n'
category 另外的功能占用的字段,还没开放 忽略就行
post 请求 参数名也是上面这些
```

Expand Down
15 changes: 14 additions & 1 deletion View/MessageTableViewCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,27 @@ class MessageTableViewCell: BaseTableViewCell<MessageTableViewCellViewModel> {
override func bindViewModel(model: MessageTableViewCellViewModel) {
super.bindViewModel(model: model)

Observable.combineLatest(model.title, model.body, model.url).subscribe {[weak self] title, body, url in
Observable.combineLatest(model.title, model.subtitle, model.body, model.url).subscribe { [weak self] title, subtitle, body, url in
guard let self else { return }

let text = NSMutableAttributedString(
string: body,
attributes: [.font: UIFont.preferredFont(ofSize: 14), .foregroundColor: BKColor.grey.darken4]
)

if subtitle.count > 0 {
// 插入一行空行当 spacer
text.insert(NSAttributedString(
string: "\n",
attributes: [.font: UIFont.systemFont(ofSize: 6, weight: .medium)]
), at: 0)

text.insert(NSAttributedString(
string: subtitle + "\n",
attributes: [.font: UIFont.preferredFont(ofSize: 16, weight: .medium), .foregroundColor: BKColor.grey.darken4]
), at: 0)
}

if title.count > 0 {
// 插入一行空行当 spacer
text.insert(NSAttributedString(
Expand Down
2 changes: 2 additions & 0 deletions View/MessageTableViewCellViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class MessageTableViewCellViewModel: ViewModel {
var identity: String

let title: BehaviorRelay<String>
let subtitle: BehaviorRelay<String>
let body: BehaviorRelay<String>
let url: BehaviorRelay<String>

Expand All @@ -34,6 +35,7 @@ class MessageTableViewCellViewModel: ViewModel {
self.message = message
self.identity = message.id
self.title = BehaviorRelay<String>(value: message.title ?? "")
self.subtitle = BehaviorRelay<String>(value: message.subtitle ?? "")
self.body = BehaviorRelay<String>(value: message.body ?? "")
self.url = BehaviorRelay<String>(value: message.url ?? "")

Expand Down
4 changes: 3 additions & 1 deletion docs/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
可以发 GET 或者 POST 请求 ,请求成功会立即收到推送

## URL格式
URL由推送key、参数 title、参数 body 组成。有下面两种组合方式
URL由推送key、参数 title、参数 subtitle、参数 body 组成。有下面三种组合方式

```
/:key/:body
/:key/:title/:body
/:key/:title/:subtitle/:body
```

## 请求方式
Expand Down Expand Up @@ -59,6 +60,7 @@ curl -X "POST" "https://api.day.app/push" \
| 参数 | 说明 |
| ----- | ----------- |
| title | 推送标题 |
| subtitle | 推送副标题 |
| body | 推送内容 |
| level | 推送中断级别。 <br>active:默认值,系统会立即亮屏显示通知<br>timeSensitive:时效性通知,可在专注状态下显示通知。<br>passive:仅将通知添加到通知列表,不会亮屏提醒。 |
| badge | 推送角标,可以是任意数字 |
Expand Down

0 comments on commit f155f65

Please sign in to comment.