ios: ensure that device token is registered once (#794)
This commit is contained in:
committed by
GitHub
parent
b1c732f3cc
commit
06cb564eae
@@ -31,6 +31,7 @@ final class ChatModel: ObservableObject {
|
|||||||
@Published var appOpenUrl: URL?
|
@Published var appOpenUrl: URL?
|
||||||
@Published var deviceToken: DeviceToken?
|
@Published var deviceToken: DeviceToken?
|
||||||
@Published var savedToken: DeviceToken?
|
@Published var savedToken: DeviceToken?
|
||||||
|
@Published var tokenRegistered = false
|
||||||
@Published var tokenStatus: NtfTknStatus?
|
@Published var tokenStatus: NtfTknStatus?
|
||||||
@Published var notificationMode = NotificationsMode.off
|
@Published var notificationMode = NotificationsMode.off
|
||||||
@Published var notificationPreview: NotificationPreviewMode? = ntfPreviewModeGroupDefault.get()
|
@Published var notificationPreview: NotificationPreviewMode? = ntfPreviewModeGroupDefault.get()
|
||||||
|
|||||||
@@ -238,12 +238,14 @@ func apiDeleteChatItem(type: ChatType, id: Int64, itemId: Int64, mode: CIDeleteM
|
|||||||
throw r
|
throw r
|
||||||
}
|
}
|
||||||
|
|
||||||
func apiGetNtfToken() throws -> (DeviceToken?, NtfTknStatus?, NotificationsMode) {
|
func apiGetNtfToken() -> (DeviceToken?, NtfTknStatus?, NotificationsMode) {
|
||||||
let r = chatSendCmdSync(.apiGetNtfToken)
|
let r = chatSendCmdSync(.apiGetNtfToken)
|
||||||
switch r {
|
switch r {
|
||||||
case let .ntfToken(token, status, ntfMode): return (token, status, ntfMode)
|
case let .ntfToken(token, status, ntfMode): return (token, status, ntfMode)
|
||||||
case .chatCmdError(.errorAgent(.CMD(.PROHIBITED))): return (nil, nil, .off)
|
case .chatCmdError(.errorAgent(.CMD(.PROHIBITED))): return (nil, nil, .off)
|
||||||
default: throw r
|
default:
|
||||||
|
logger.debug("apiGetNtfToken response: \(String(describing: r), privacy: .public)")
|
||||||
|
return (nil, nil, .off)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -256,7 +258,8 @@ func apiRegisterToken(token: DeviceToken, notificationMode: NotificationsMode) a
|
|||||||
func registerToken(token: DeviceToken) {
|
func registerToken(token: DeviceToken) {
|
||||||
let m = ChatModel.shared
|
let m = ChatModel.shared
|
||||||
let mode = m.notificationMode
|
let mode = m.notificationMode
|
||||||
if mode != .off {
|
if mode != .off && !m.tokenRegistered {
|
||||||
|
m.tokenRegistered = true
|
||||||
logger.debug("registerToken \(mode.rawValue)")
|
logger.debug("registerToken \(mode.rawValue)")
|
||||||
Task {
|
Task {
|
||||||
do {
|
do {
|
||||||
@@ -545,7 +548,7 @@ func startChat() throws {
|
|||||||
let chats = try apiGetChats()
|
let chats = try apiGetChats()
|
||||||
m.chats = chats.map { Chat.init($0) }
|
m.chats = chats.map { Chat.init($0) }
|
||||||
try refreshCallInvitations()
|
try refreshCallInvitations()
|
||||||
(m.savedToken, m.tokenStatus, m.notificationMode) = try apiGetNtfToken()
|
(m.savedToken, m.tokenStatus, m.notificationMode) = apiGetNtfToken()
|
||||||
if let token = m.deviceToken {
|
if let token = m.deviceToken {
|
||||||
registerToken(token: token)
|
registerToken(token: token)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user