Skip to content

Commit

Permalink
Ready for test flight
Browse files Browse the repository at this point in the history
  • Loading branch information
binwiederhier committed May 21, 2022
1 parent 81bd4fd commit 7edab23
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 47 deletions.
68 changes: 24 additions & 44 deletions ntfy/App/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,63 +8,41 @@ import CoreData
class AppDelegate: UIResponder, UIApplicationDelegate {
let tag = "AppDelegate"

func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
Log.d(tag, "ApplicationDelegate didFinishLaunchingWithOptions.")
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
Log.d(tag, "Launching AppDelegate")

Messaging.messaging().delegate = self

registerForPushNotifications()
// Register app permissions for push notifications
UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { success, error in
guard success else {
Log.e(self.tag, "Failed to register for local push notifications", error)
return
}
Log.d(self.tag, "Successfully registered for local push notifications")
}

// Register too receive remote notifications
application.registerForRemoteNotifications()

// Set self as messaging delegate
Messaging.messaging().delegate = self

return true
}

func application(
_ application: UIApplication,
didFailToRegisterForRemoteNotificationsWithError error: Error
) {
Log.e(tag, "Failed to register for remote notifications", error)
}

func application(
_ application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data
) {
let token = deviceToken
.map { data in String(format: "%02.2hhx", data) }
.joined()
Log.d(tag, "Registered for remote notifications. Passing APNs token to Firebase: \(token)")
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
let token = deviceToken.map { data in String(format: "%02.2hhx", data) }.joined()
Messaging.messaging().apnsToken = deviceToken
Log.d(tag, "Registered for remote notifications. Passing APNs token to Firebase: \(token)")
}

func registerForPushNotifications() {
Log.d(tag, "Registering for local push notifications")
UNUserNotificationCenter.current()
.requestAuthorization(options: [.alert, .sound, .badge]) { success, error in
guard success else {
Log.e(self.tag, "Failed to register for local push notifications", error)
return
}
Log.d(self.tag, "Successfully registered for local push notifications")
self.registerForRemoteNotifications()
}
}

func registerForRemoteNotifications() {
UNUserNotificationCenter.current().getNotificationSettings { settings in
print("Notification settings: \(settings)")
guard settings.authorizationStatus == .authorized else { return }
DispatchQueue.main.async {
UIApplication.shared.registerForRemoteNotifications()
}
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
Log.e(tag, "Failed to register for remote notifications", error)
}
}

extension AppDelegate: UNUserNotificationCenterDelegate {
/// Executed when the app is in the foreground. Nothing has to be done here, except call the completionHandler.
func userNotificationCenter(
_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
Expand All @@ -75,13 +53,15 @@ extension AppDelegate: UNUserNotificationCenterDelegate {
completionHandler([[.banner, .sound]])
}

/// Executed when the user clicks on the notification.
func userNotificationCenter(
_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void
) {
let userInfo = response.notification.request.content.userInfo
Log.d(tag, "Notification received via userNotificationCenter(didReceive)", userInfo)
// TODO: This should navigate to the detail view
completionHandler()
}
}
Expand Down
1 change: 0 additions & 1 deletion ntfy/App/AppMain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import Firebase

// Must have before release:
// TODO: Verify whether model version needs to be specified
// TODO: Make AppDelegate prettier

// Nice to have
// TODO: Make notification click open detail view
Expand Down
4 changes: 2 additions & 2 deletions ntfy/Persistence/Subscription.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import Foundation

extension Subscription {
func urlString() -> String {
return topicUrl(baseUrl: baseUrl!, topic: topic!)
return topicUrl(baseUrl: baseUrl ?? "?", topic: topic ?? "?")
}

func displayName() -> String {
return topicShortUrl(baseUrl: baseUrl!, topic: topic!)
return topicShortUrl(baseUrl: baseUrl ?? "?", topic: topic ?? "?")
}

func topicName() -> String {
Expand Down

0 comments on commit 7edab23

Please sign in to comment.