Skip to content

Commit

Permalink
Notifications: Add notification for session expired (#118)
Browse files Browse the repository at this point in the history
  • Loading branch information
anian03 authored Feb 6, 2025
1 parent a4fc70e commit 024124b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Sources/APIClient/APIClient.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Foundation
import Common
import UserNotifications
import UserStore

// swiftlint:disable force_cast
Expand Down Expand Up @@ -193,6 +194,8 @@ public final class APIClient {
UserSessionFactory.shared.savePassword(password: nil)
UserSessionFactory.shared.saveUsername(username: nil)
}
// Remove notification about being logged out
UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
}

private func logoutAndSetTokenExpired() {
Expand Down
27 changes: 27 additions & 0 deletions Sources/PushNotifications/PushNotificationHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,37 @@ public class PushNotificationHandler {
PushNotificationUserInfoKeys.communicationInfo: notification.communicationInfo?.asData]
return content
}

/// Schedules a local notification that will be sent to the user when the current
/// login session, and thus the push notification token, is invalidated
public static func scheduleNotificationForSessionExpired() {
guard let cookies = URLSession.shared.configuration.httpCookieStorage?.cookies else {
return
}
let jwtCookie = cookies.first { $0.name == "jwt" }
if let jwtCookie, let expiryDate = jwtCookie.expiresDate {
let triggerComponents = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute], from: expiryDate)
let trigger = UNCalendarNotificationTrigger(dateMatching: triggerComponents,
repeats: false)

let content = UNMutableNotificationContent()
content.title = R.string.localizable.sessionExpiredTitle()
content.body = R.string.localizable.sessionExpiredBody()

let notification = UNNotificationRequest(identifier: LocalNotificationIdentifiers.sessionExpired,
content: content,
trigger: trigger)
UNUserNotificationCenter.current().add(notification)
}
}
}

class PushNotificationUserInfoKeys {
static var target = "target"
static var type = "type"
static var communicationInfo = "communicationInfo"
}

public class LocalNotificationIdentifiers {
public static let sessionExpired = "sessionExpiredNotification"
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
"artemisApp.singleUserNotification.title.createGroupChat" = "New group chat";
"artemisApp.singleUserNotification.title.createDirectChat" = "New direct chat";
"artemisApp.singleUserNotification.title.mentionedInMessage" = "You were mentioned";
"sessionExpiredTitle" = "Session expired";

// MARK: Body
"artemisApp.singleUserNotification.text.exerciseSubmissionAssessed" = "Your submission for the %2$@ exercise \"%3$@\" in the course \"%1$@\" has been assessed.";
Expand Down Expand Up @@ -149,6 +150,7 @@
"artemisApp.singleUserNotification.text.removeUserGroupChat" = "You have been removed from group chat by %2$@ in course %1$@.";
"artemisApp.singleUserNotification.text.addUserGroupChat" = "You have been added to a new group chat by %2$@ in course %1$@.";
"artemisApp.singleUserNotification.text.createGroupChat" = "You have been added to a new group chat by %2$@ in course %1$@.";
"sessionExpiredBody" = "Please log in again to Artemis to continue receiving notifications.";

// MARK: - Communication
"repliedTo" = "%1$@ (replied to %2$@)";
Expand Down

0 comments on commit 024124b

Please sign in to comment.