Skip to content

Commit a40b1da

Browse files
committed
feat(macOS): Notification click
1 parent 2376207 commit a40b1da

File tree

6 files changed

+54
-15
lines changed

6 files changed

+54
-15
lines changed

apple/Config.xcconfig.template

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
# Your dev API IP/hostname eg. 192.168.1.100 or mac.local
1+
# Your dev API IP/hostname eg. 192.168.1.100 or mac.local get it via hostname shell command
22
DEV_HOST = $(API_BASE_URL_VALUE)

apple/InlineKit/Sources/InlineKit/Models/Message.swift

-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,6 @@ public extension Message {
153153
if let existing = try? Message
154154
.fetchOne(db, key: ["messageId": self.messageId, "chatId": self.chatId])
155155
{
156-
print("found existing message \(existing)")
157156
self.globalId = existing.globalId
158157
}
159158
}

apple/InlineMac/AppDelegate.swift

+38-4
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ import UserNotifications
88
class AppDelegate: NSObject, NSApplicationDelegate {
99
let notifications = NotificationsManager()
1010
let navigation: NavigationModel = .shared
11+
let log = Log.scoped("AppDelegate")
1112

1213
func applicationWillFinishLaunching(_ notification: Notification) {
1314
// Disable native tabbing
1415
NSWindow.allowsAutomaticWindowTabbing = false
1516

1617
// Setup Notifications Delegate
17-
notifications.setup()
18-
UNUserNotificationCenter.current().delegate = notifications
18+
setupNotifications()
1919
}
2020

2121
func applicationDidFinishLaunching(_ aNotification: Notification) {
@@ -31,12 +31,24 @@ class AppDelegate: NSObject, NSApplicationDelegate {
3131
func applicationSupportsSecureRestorableState(_ app: NSApplication) -> Bool {
3232
return true
3333
}
34+
}
3435

36+
// MARK: - Notifications
37+
38+
extension AppDelegate {
39+
func setupNotifications() {
40+
notifications.setup()
41+
notifications.onNotificationReceived { response in
42+
self.handleNotification(response)
43+
}
44+
UNUserNotificationCenter.current().delegate = notifications
45+
}
46+
3547
func application(
3648
_ application: NSApplication,
3749
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data
3850
) {
39-
Log.shared.debug("Registered for remote notifications: \(deviceToken)")
51+
log.debug("Registered for remote notifications: \(deviceToken)")
4052

4153
notifications.didRegisterForRemoteNotifications(deviceToken: deviceToken)
4254
}
@@ -45,6 +57,28 @@ class AppDelegate: NSObject, NSApplicationDelegate {
4557
_ application: NSApplication,
4658
didFailToRegisterForRemoteNotificationsWithError error: Error
4759
) {
48-
Log.shared.error("Failed to register for remote notifications \(error)")
60+
log.error("Failed to register for remote notifications \(error)")
61+
}
62+
63+
func handleNotification(_ response: UNNotificationResponse) {
64+
log.debug("Received notification: \(response)")
65+
66+
// TODO: Navigate
67+
guard let userInfo = response.notification.request.content.userInfo as? [String: Any] else {
68+
return
69+
}
70+
71+
if let peerId = getPeerFromNotification(userInfo) {
72+
navigation.select(.chat(peer: peerId))
73+
// TODO: Handle spaceId
74+
}
75+
}
76+
77+
func getPeerFromNotification(_ userInfo: [String: Any]) -> Peer? {
78+
if let peerUserId = userInfo["userId"] as? Int64 {
79+
return .user(id: peerUserId)
80+
} else {
81+
return nil
82+
}
4983
}
5084
}

apple/InlineMac/Utils/Notifications.swift

+13-4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@ class NotificationsManager: NSObject {
2121
center.delegate = self
2222
log.debug("Notifications manager setup completed.")
2323
}
24+
25+
var onNotificationReceivedAction: ((_ response: UNNotificationResponse) -> Void)?
26+
27+
func onNotificationReceived(action: @escaping (_ response: UNNotificationResponse) -> Void) {
28+
if onNotificationReceivedAction != nil {
29+
log.error("onNotificationReceived action already attached. It must only be called once.")
30+
}
31+
log.trace("Attached onNotificationReceived action")
32+
onNotificationReceivedAction = action
33+
}
2434
}
2535

2636
// Delegate
@@ -40,12 +50,11 @@ extension NotificationsManager: UNUserNotificationCenterDelegate {
4050
func userNotificationCenter(
4151
_: UNUserNotificationCenter,
4252
didReceive response: UNNotificationResponse,
43-
withCompletionHandler _: @escaping () -> Void
53+
withCompletionHandler completionHandler: @escaping () -> Void
4454
) {
4555
log.debug("Received notification: \(response.notification.request.content.userInfo)")
46-
let userInfo = response.notification.request.content.userInfo
47-
48-
// Handle the notification here.
56+
self.onNotificationReceivedAction.map { $0(response) }
57+
completionHandler() // Is this correct?
4958
}
5059
}
5160

apple/InlineMac/Views/Chat/AppKit/MessageTableView.swift

-2
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,6 @@ class MessagesTableView: NSViewController {
138138
private func scrollToBottom(animated: Bool) {
139139
guard messages.count > 0 else { return }
140140

141-
print("scrollTopInset \(scrollView.contentInsets.top)")
142-
143141
let lastRow = messages.count - 1
144142

145143
if animated {

server/src/methods/sendMessage.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -273,9 +273,8 @@ const sendPushNotificationToUser = async ({
273273
session.clientType === "macos"
274274
? isProd
275275
? "chat.inline.InlineMac"
276-
: "chat.inline.InlineMac"
277-
: // : "chat.inline.InlineMac.debug"
278-
"chat.inline.InlineIOS"
276+
: "chat.inline.InlineMac.debug"
277+
: "chat.inline.InlineIOS"
279278

280279
// Configure notification
281280
const notification = new APN.Notification()

0 commit comments

Comments
 (0)