添加重要通知功能

close: 152
This commit is contained in:
Fin 2024-11-12 10:17:29 +08:00
parent ad442cd7da
commit 089b33caa3
4 changed files with 17 additions and 6 deletions

View File

@ -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>

View File

@ -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()

View File

@ -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()

View File

@ -43,7 +43,9 @@ 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)
}
@ -51,12 +53,10 @@ class CallProcessor: NotificationContentProcessor {
///
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
}
}
@ -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
}
}