Skip to content

Commit

Permalink
添加重要通知功能
Browse files Browse the repository at this point in the history
close: 152
  • Loading branch information
Finb committed Nov 12, 2024
1 parent ad442cd commit 089b33c
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 6 deletions.
2 changes: 2 additions & 0 deletions Bark/Bark.entitlements
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
<string>development</string>
<key>com.apple.developer.usernotifications.communication</key>
<true/>
<key>com.apple.developer.usernotifications.critical-alerts</key>
<true/>
<key>com.apple.developer.usernotifications.time-sensitive</key>
<true/>
<key>com.apple.security.application-groups</key>
Expand Down
2 changes: 1 addition & 1 deletion Common/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class Client: NSObject {

func registerForRemoteNotifications() {
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.alert, .sound, .badge], completionHandler: { (_ granted: Bool, _: Error?) in
center.requestAuthorization(options: [.alert, .sound, .badge, .criticalAlert], completionHandler: { (_ granted: Bool, _: Error?) in
if granted {
dispatch_sync_safely_main_queue {
UIApplication.shared.registerForRemoteNotifications()
Expand Down
2 changes: 1 addition & 1 deletion Controller/HomeViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class HomeViewController: BaseViewController<HomeViewModel> {
let startRequestAuthorization: () -> Observable<Bool> = {
Single<Bool>.create { single -> Disposable in
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.alert, .sound, .badge], completionHandler: { (_ granted: Bool, _: Error?) -> Void in
center.requestAuthorization(options: [.alert, .sound, .badge, .criticalAlert], completionHandler: { (_ granted: Bool, _: Error?) -> Void in
single(.success(granted))
})
return Disposables.create()
Expand Down
17 changes: 13 additions & 4 deletions NotificationServiceExtension/Processor/CallProcessor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,20 @@ class CallProcessor: NotificationContentProcessor {
guard let content = content.mutableCopy() as? UNMutableNotificationContent else {
return
}
content.sound = nil
if !content.isCritical { // 重要通知的声音可以无视静音模式,所以别把这特性给弄没了
content.sound = nil
}
let request = UNNotificationRequest(identifier: identifier, content: content, trigger: nil)
UNUserNotificationCenter.current().add(request)
}

/// 响铃结束时取消显示远程推送,因为已经用本地推送显示了一遍
private func cancelRemoteNotification(content: UNMutableNotificationContent) {
// 远程推送在响铃结束后静默不显示
// 至于iOS15以下的设备,因不支持这个特性会在响铃结束后再展示一次, 但会取消声音
// 至于iOS15以下的设备,因不支持这个特性会在响铃结束后再展示一次
// 如果设置了 level 参数,就还是以 level 参数为准不做修改
if #available(iOSApplicationExtension 15.0, *), self.content?.userInfo["level"] == nil {
self.content?.interruptionLevel = .passive
} else {
content.sound = nil
}
}

Expand Down Expand Up @@ -152,3 +152,12 @@ class CallProcessor: NotificationContentProcessor {
CFNotificationCenterRemoveObserver(CFNotificationCenterGetDarwinNotifyCenter(), observer, name, nil)
}
}

extension UNMutableNotificationContent {
var isCritical: Bool {
if #available(iOS 15, *) {
return self.interruptionLevel == .critical
}
return false
}
}

0 comments on commit 089b33c

Please sign in to comment.