-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPushMessagingServiceImpl.swift
More file actions
74 lines (63 loc) · 2.05 KB
/
Copy pathPushMessagingServiceImpl.swift
File metadata and controls
74 lines (63 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//
// PushMessagingServiceImpl.swift
// DevLogInfra
//
// Created by opfic on 5/15/26.
//
import Foundation
import DevLogData
import FirebaseMessaging
import UserNotifications
final class PushMessagingServiceImpl: NSObject, PushMessagingService {
private enum CrashlyticsError {
static let domain = "DevLogInfra.PushMessagingServiceImpl"
enum Code: Int {
case fetchFCMToken = 1
}
}
private weak var delegate: PushMessagingServiceDelegate?
func setDelegate(_ delegate: PushMessagingServiceDelegate?) {
self.delegate = delegate
Messaging.messaging().delegate = self
}
func setAPNSToken(_ deviceToken: Data) {
Messaging.messaging().apnsToken = deviceToken
}
func isNotificationAuthorized() async -> Bool {
let settings = await UNUserNotificationCenter.current().notificationSettings()
switch settings.authorizationStatus {
case .authorized, .provisional, .ephemeral:
return true
case .denied, .notDetermined:
return false
@unknown default:
return false
}
}
func fetchFCMToken() async throws -> String? {
do {
return try await Messaging.messaging().token()
} catch {
if error.isMissingAPNSTokenForFCMToken {
return nil
}
FirebaseCrashlyticsHelper.record(
error,
domain: "\(CrashlyticsError.domain).\(CrashlyticsError.Code.fetchFCMToken)",
code: CrashlyticsError.Code.fetchFCMToken.rawValue
)
throw error
}
}
}
extension PushMessagingServiceImpl: MessagingDelegate {
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {
delegate?.pushMessagingService(self, didReceiveRegistrationToken: fcmToken)
}
}
private extension Error {
var isMissingAPNSTokenForFCMToken: Bool {
let nsError = self as NSError
return nsError.domain == "com.google.fcm" && nsError.code == 505
}
}