JLChen
2021-08-02 38f4fb064df09f344fc3237409c76a9fba2a8a9e
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*
* Copyright (c) 2010-2020 Belledonne Communications SARL.
*
* This file is part of linphone-iphone
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
 
import UserNotifications
import linphonesw
#if USE_CRASHLYTICS
import Firebase
#endif
 
var APP_GROUP_ID = "group.org.linphone.phone.msgNotification"
var LINPHONE_DUMMY_SUBJECT = "dummy subject"
 
struct MsgData: Codable {
    var from: String?
    var body: String?
    var subtitle: String?
    var callId: String?
    var localAddr: String?
    var peerAddr: String?
}
 
class NotificationService: UNNotificationServiceExtension {
 
    var contentHandler: ((UNNotificationContent) -> Void)?
    var bestAttemptContent: UNMutableNotificationContent?
 
    var lc: Core?
    static var logDelegate: LinphoneLoggingServiceManager!
    static var log: LoggingService!
    
    override init() {
        super.init()
#if USE_CRASHLYTICS
        FirebaseApp.configure()
#endif
    }
 
    override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
        self.contentHandler = contentHandler
        bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
        NSLog("[msgNotificationService] start msgNotificationService extension")
 
        if (VFSUtil.vfsEnabled(groupName: APP_GROUP_ID) && !VFSUtil.activateVFS()) {
            VFSUtil.log("[VFS] Error unable to activate.", .error)
        }
        
        if let bestAttemptContent = bestAttemptContent {
            createCore()
            NotificationService.log.message(message: "received push payload : \(bestAttemptContent.userInfo.debugDescription)")
 
            if let chatRoomInviteAddr = bestAttemptContent.userInfo["chat-room-addr"] as? String, !chatRoomInviteAddr.isEmpty {
                NotificationService.log.message(message: "fetch chat room for invite, addr: \(chatRoomInviteAddr)")
                let chatRoom = lc!.getNewChatRoomFromConfAddr(chatRoomAddr: chatRoomInviteAddr)
 
                if let chatRoom = chatRoom {
                    stopCore()
                    NotificationService.log.message(message: "chat room invite received")
                    bestAttemptContent.title = NSLocalizedString("GC_MSG", comment: "")
                    if (chatRoom.hasCapability(mask:ChatRoomCapabilities.OneToOne.rawValue)) {
                        if (chatRoom.peerAddress?.displayName.isEmpty != true) {
                            bestAttemptContent.body = chatRoom.peerAddress!.displayName
                        } else {
                            bestAttemptContent.body = chatRoom.peerAddress!.username
                        }
                    } else {
                        bestAttemptContent.body = chatRoom.subject
                    }
 
                    bestAttemptContent.sound = UNNotificationSound(named: UNNotificationSoundName("msg.caf")) // TODO : temporary fix, to be removed after flexisip release
                    contentHandler(bestAttemptContent)
                    return
                }
            } else if let callId = bestAttemptContent.userInfo["call-id"] as? String {
                NotificationService.log.message(message: "fetch msg for callid ["+callId+"]")
                let message = lc!.getNewMessageFromCallid(callId: callId)
 
                if let message = message {
                    let msgData = parseMessage(message: message)
 
                    // Extension only upates app's badge when main shared core is Off = extension's core is On.
                    // Otherwise, the app will update the badge.
                    if lc?.globalState == GlobalState.On, let badge = updateBadge() as NSNumber? {
                        bestAttemptContent.badge = badge
                    }
 
                    stopCore()
 
                    bestAttemptContent.sound = UNNotificationSound(named: UNNotificationSoundName(rawValue: "msg.caf"))
                    bestAttemptContent.title = NSLocalizedString("Message received", comment: "")
                    if let subtitle = msgData?.subtitle {
                        bestAttemptContent.subtitle = subtitle
                    }
                    if let body = msgData?.body {
                        bestAttemptContent.body = body
                    }
 
                    bestAttemptContent.categoryIdentifier = "msg_cat"
 
                    bestAttemptContent.userInfo.updateValue(msgData?.callId as Any, forKey: "CallId")
                    bestAttemptContent.userInfo.updateValue(msgData?.from as Any, forKey: "from")
                    bestAttemptContent.userInfo.updateValue(msgData?.peerAddr as Any, forKey: "peer_addr")
                    bestAttemptContent.userInfo.updateValue(msgData?.localAddr as Any, forKey: "local_addr")
 
                    contentHandler(bestAttemptContent)
                    return
                } else {
                    NotificationService.log.message(message: "Message not found for callid ["+callId+"]")
                }
            }
            serviceExtensionTimeWillExpire()
        }
    }
 
    override func serviceExtensionTimeWillExpire() {
        // Called just before the extension will be terminated by the system.
        // Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
        NotificationService.log.warning(message: "serviceExtensionTimeWillExpire")
        stopCore()
        if let contentHandler = contentHandler, let bestAttemptContent =  bestAttemptContent {
            NSLog("[msgNotificationService] serviceExtensionTimeWillExpire")
            bestAttemptContent.categoryIdentifier = "app_active"
 
            if let chatRoomInviteAddr = bestAttemptContent.userInfo["chat-room-addr"] as? String, !chatRoomInviteAddr.isEmpty {
                bestAttemptContent.title = NSLocalizedString("GC_MSG", comment: "")
                bestAttemptContent.body = ""
                bestAttemptContent.sound = UNNotificationSound(named: UNNotificationSoundName("msg.caf")) // TODO : temporary fix, to be removed after flexisip release
            } else {
                bestAttemptContent.title = NSLocalizedString("Message received", comment: "")
                bestAttemptContent.body = NSLocalizedString("IM_MSG", comment: "")
            }
            contentHandler(bestAttemptContent)
        }
    }
 
    func parseMessage(message: PushNotificationMessage) -> MsgData? {
        let content = message.isText ? message.textContent : "🗻"
        let fromAddr = message.fromAddr?.username
        let callId = message.callId
        let localUri = message.localAddr?.asStringUriOnly()
        let peerUri = message.peerAddr?.asStringUriOnly()
        let from: String
        if let fromDisplayName = getDisplayNameFromSipAddress(sipAddr: message.fromAddr?.asStringUriOnly()) {
            from = fromDisplayName
        } else {
            from = fromAddr!
        }
 
 
        var msgData = MsgData(from: fromAddr, body: "", subtitle: "", callId:callId, localAddr: localUri, peerAddr:peerUri)
 
        if let showMsg = lc!.config?.getBool(section: "app", key: "show_msg_in_notif", defaultValue: true), showMsg == true {
            if let subject = message.subject as String?, subject != "" {
                msgData.subtitle = subject
                msgData.body = from + " : " + content
            } else {
                msgData.subtitle = from
                msgData.body = content
            }
        } else {
            if let subject = message.subject as String?, subject != "" {
                msgData.body = subject + " : " + from
            } else {
                msgData.body = from
            }
        }
 
        NotificationService.log.message(message: "received msg size : \(content.count) \n")
        return msgData;
    }
 
    func createCore() {
        NSLog("[msgNotificationService] create core")
        
        let config = Config.newForSharedCore(appGroupId: APP_GROUP_ID, configFilename: "linphonerc", factoryConfigFilename: "")
 
        if (NotificationService.log == nil) {
            NotificationService.log = LoggingService.Instance /*enable liblinphone logs.*/
            NotificationService.logDelegate = try! LinphoneLoggingServiceManager(config: config!, log: NotificationService.log, domain: "msgNotificationService")
        }
        lc = try! Factory.Instance.createSharedCoreWithConfig(config: config!, systemContext: nil, appGroupId: APP_GROUP_ID, mainCore: false)
    }
 
    func stopCore() {
        NotificationService.log.message(message: "stop core")
        if let lc = lc {
            lc.stop()
        }
    }
 
    func updateBadge() -> Int {
        var count = 0
        count += lc!.unreadChatMessageCount
        count += lc!.missedCallsCount
        count += lc!.callsNb
        NotificationService.log.message(message: "badge: \(count)\n")
 
        return count
    }
 
    func getDisplayNameFromSipAddress(sipAddr: String?) -> String? {
        if let sipAddr = sipAddr {
            NotificationService.log.message(message: "looking for display name for \(sipAddr)")
 
            if (sipAddr == "") { return nil }
 
            let defaults = UserDefaults.init(suiteName: APP_GROUP_ID)
            let addressBook = defaults?.dictionary(forKey: "addressBook")
 
            if (addressBook == nil) {
                NotificationService.log.message(message: "address book not found in userDefaults")
                return nil
            }
 
            if let simpleAddr = lc?.interpretUrl(url: sipAddr) {
                simpleAddr.clean()
                let nomalSipaddr = simpleAddr.asString()
                if let displayName = addressBook?[nomalSipaddr] as? String {
                    NotificationService.log.message(message: "display name for \(sipAddr): \(displayName)")
                    return displayName
                }
            }
 
            NotificationService.log.message(message: "display name for \(sipAddr) not found in userDefaults")
            return nil
        }
        return nil
    }
}