asking for notification permissions on authentication for more reliability for it to work off the bat and permissions for notifications
This commit is contained in:
@@ -6,8 +6,14 @@
|
|||||||
//
|
//
|
||||||
|
|
||||||
import Foundation
|
import Foundation
|
||||||
|
import Firebase
|
||||||
|
import FirebaseFirestore
|
||||||
|
import FirebaseMessaging
|
||||||
|
import UserNotifications
|
||||||
|
import UIKit
|
||||||
|
|
||||||
class PushNotificationService {
|
class PushNotificationService: NSObject, MessagingDelegate, UNUserNotificationCenterDelegate {
|
||||||
|
let firestoreService = FirestoreService()
|
||||||
|
|
||||||
func sendPushNotification(to token: String, title: String, body: String) {
|
func sendPushNotification(to token: String, title: String, body: String) {
|
||||||
let urlString = "https://fcm.googleapis.com/fcm/send"
|
let urlString = "https://fcm.googleapis.com/fcm/send"
|
||||||
@@ -34,5 +40,43 @@ class PushNotificationService {
|
|||||||
}
|
}
|
||||||
task.resume()
|
task.resume()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func registerForPushNotifications() {
|
||||||
|
if #available(iOS 10.0, *) {
|
||||||
|
// For iOS 10 display notification (sent via APNS)
|
||||||
|
UNUserNotificationCenter.current().delegate = self
|
||||||
|
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
|
||||||
|
UNUserNotificationCenter.current().requestAuthorization(
|
||||||
|
options: authOptions,
|
||||||
|
completionHandler: {_, _ in })
|
||||||
|
// For iOS 10 data message (sent via FCM)
|
||||||
|
Messaging.messaging().delegate = self
|
||||||
|
} else {
|
||||||
|
let settings: UIUserNotificationSettings =
|
||||||
|
UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
|
||||||
|
UIApplication.shared.registerUserNotificationSettings(settings)
|
||||||
|
}
|
||||||
|
UIApplication.shared.registerForRemoteNotifications()
|
||||||
|
}
|
||||||
|
|
||||||
|
func updateFirestorePushTokenIfNeeded() {
|
||||||
|
if let deviceToken = Messaging.messaging().fcmToken {
|
||||||
|
if let user = Auth.auth().currentUser {
|
||||||
|
// User is signed in.
|
||||||
|
print("have access to user Id when I received registration token: \(user.uid)")
|
||||||
|
|
||||||
|
// kind of have to store in database or update every time, because don't know if the user logged in and out or if there is a new token or not
|
||||||
|
// https://firebase.google.com/docs/cloud-messaging/ios/client#access_the_registration_token
|
||||||
|
|
||||||
|
firestoreService.updateUserDeviceToken(userId: user.uid, deviceToken: deviceToken) { res in
|
||||||
|
print(res)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// No user is signed in.
|
||||||
|
// ...
|
||||||
|
print("can't save user device token to a user")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -143,7 +143,9 @@ struct InnerCircleView: View {
|
|||||||
// activate 3 data listeners once for authsessionstore/usermanager if not already called, but authsessionstore will handle that
|
// activate 3 data listeners once for authsessionstore/usermanager if not already called, but authsessionstore will handle that
|
||||||
self.authSessionStore.activateMainDataListeners()
|
self.authSessionStore.activateMainDataListeners()
|
||||||
|
|
||||||
// save device token
|
// set up push notifications and such + save up to date device token
|
||||||
|
// TODO: this is firing too often?
|
||||||
|
self.innerCircleVM.setUpPushNotifications()
|
||||||
}
|
}
|
||||||
// .onDisappear {
|
// .onDisappear {
|
||||||
// print("deiniting data listeners, but current data should still be cached!")
|
// print("deiniting data listeners, but current data should still be cached!")
|
||||||
|
|||||||
@@ -18,9 +18,9 @@ class InnerCircleViewModel: ObservableObject {
|
|||||||
|
|
||||||
private var audioLocalUrl: URL?
|
private var audioLocalUrl: URL?
|
||||||
|
|
||||||
let cloudStorageService = CloudStorageService()
|
private let cloudStorageService = CloudStorageService()
|
||||||
let firestoreService = FirestoreService()
|
private let firestoreService = FirestoreService()
|
||||||
let pushNotificationService = PushNotificationService()
|
private let pushNotificationService = PushNotificationService()
|
||||||
|
|
||||||
init() {
|
init() {
|
||||||
do {
|
do {
|
||||||
@@ -32,7 +32,6 @@ class InnerCircleViewModel: ObservableObject {
|
|||||||
print("Can not setup the Recording")
|
print("Can not setup the Recording")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// audio stuff
|
// audio stuff
|
||||||
@@ -104,5 +103,9 @@ extension InnerCircleViewModel {
|
|||||||
|
|
||||||
// handling saving device token for notifications
|
// handling saving device token for notifications
|
||||||
extension InnerCircleViewModel {
|
extension InnerCircleViewModel {
|
||||||
|
func setUpPushNotifications() {
|
||||||
|
self.pushNotificationService.registerForPushNotifications()
|
||||||
|
|
||||||
|
self.pushNotificationService.updateFirestorePushTokenIfNeeded()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -123,7 +123,7 @@ class AppDelegate: NSObject, UIApplicationDelegate {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
|
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
|
||||||
print("failed ot register for remote notifications with error: \(error)")
|
print("failed to register for remote notifications with error: \(error)")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -140,29 +140,6 @@ extension AppDelegate: MessagingDelegate {
|
|||||||
)
|
)
|
||||||
|
|
||||||
// Note: This callback is fired at each app startup and whenever a new token is generated.
|
// Note: This callback is fired at each app startup and whenever a new token is generated.
|
||||||
let firestoreService = FirestoreService()
|
|
||||||
let user = Auth.auth().currentUser
|
|
||||||
|
|
||||||
// TODO: store it on loading of circle view and not on launch as this may just never get called
|
|
||||||
// use the notification center above
|
|
||||||
// https://stackoverflow.com/questions/58818046/how-to-set-addobserver-in-swiftui
|
|
||||||
if user != nil && fcmToken != nil {
|
|
||||||
// User is signed in.
|
|
||||||
// ...
|
|
||||||
print("have access to user Id when I received registration token: \(user!.uid)")
|
|
||||||
|
|
||||||
// kind of have to store in database or update every time, because don't know if the user logged in and out or if there is a new token or not
|
|
||||||
// https://firebase.google.com/docs/cloud-messaging/ios/client#access_the_registration_token
|
|
||||||
|
|
||||||
firestoreService.updateUserDeviceToken(userId: user!.uid, deviceToken: fcmToken!) { res in
|
|
||||||
print(res)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// No user is signed in.
|
|
||||||
// ...
|
|
||||||
print("can't save user device token to a user")
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user