Skip to content

Commit

Permalink
WIP Added more FCM to the code
Browse files Browse the repository at this point in the history
- Added Topics to the section replacing the Pusher Interests
- Still need to handle the rejection and the all statess
- Need to test with a Device

Signed-off-by: kcw-grunt <[email protected]>
  • Loading branch information
kcw-grunt committed Jul 23, 2024
1 parent 99cdc1e commit 50cf9cc
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 58 deletions.
4 changes: 0 additions & 4 deletions litewallet.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,6 @@
C3BD4A5325975C6000D97079 /* View+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = C3BD4A5225975C6000D97079 /* View+Extension.swift */; };
C3C8973825CD6B9300241FBE /* HostingTransactionCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = C3C8973725CD6B9300241FBE /* HostingTransactionCell.swift */; };
C3D41F3A2C4A4C0200887523 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = C3D41F392C4A4C0200887523 /* Assets.xcassets */; };
C3D41F3C2C4A625000887523 /* PushHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = C3D41F3B2C4A625000887523 /* PushHelper.swift */; };
C3D41F3F2C4A64D700887523 /* FirebaseAnalytics in Frameworks */ = {isa = PBXBuildFile; productRef = C3D41F3E2C4A64D700887523 /* FirebaseAnalytics */; };
C3D41F412C4A64D700887523 /* FirebaseCrashlytics in Frameworks */ = {isa = PBXBuildFile; productRef = C3D41F402C4A64D700887523 /* FirebaseCrashlytics */; };
C3D41F432C4A64D700887523 /* FirebaseInAppMessaging-Beta in Frameworks */ = {isa = PBXBuildFile; productRef = C3D41F422C4A64D700887523 /* FirebaseInAppMessaging-Beta */; };
Expand Down Expand Up @@ -1449,7 +1448,6 @@
C3BDB42826CC0338004DAE77 /* tr */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = tr; path = tr.lproj/Localizable.strings; sourceTree = "<group>"; };
C3C8973725CD6B9300241FBE /* HostingTransactionCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HostingTransactionCell.swift; sourceTree = "<group>"; };
C3D41F392C4A4C0200887523 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
C3D41F3B2C4A625000887523 /* PushHelper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PushHelper.swift; sourceTree = "<group>"; };
C3D4379E2566EA3E00F423E1 /* LWActivityIndicator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LWActivityIndicator.swift; sourceTree = "<group>"; };
C3DBBE302BFE15AF00B95939 /* BuyTileView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BuyTileView.swift; sourceTree = "<group>"; };
C3E5A9042BFDEEF1002FBE04 /* BuyViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BuyViewModel.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -3328,7 +3326,6 @@
24313C732381E73200A83F69 /* TransactionManager.swift */,
24470E4623A6B6E900ADDA27 /* MockSeeds.swift */,
584E24F22951C119005E0E8B /* Localization.swift */,
C3D41F3B2C4A625000887523 /* PushHelper.swift */,
);
name = Models;
sourceTree = "<group>";
Expand Down Expand Up @@ -4138,7 +4135,6 @@
CEF3E8321DE55540007C0A9E /* CheckView.swift in Sources */,
CECCE5A91E0378FB00D99448 /* PinPadViewController.swift in Sources */,
584E24F32951C11A005E0E8B /* Localization.swift in Sources */,
C3D41F3C2C4A625000887523 /* PushHelper.swift in Sources */,
C3423C1C2B7903CA0051BD6D /* LaunchView.swift in Sources */,
C32DAE0725925B7E003FC978 /* Color+Extension.swift in Sources */,
C36DBF5F28F18D2C00FBCB24 /* LocalWebView.swift in Sources */,
Expand Down
99 changes: 45 additions & 54 deletions litewallet/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import UIKit
import UserNotifications

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, MessagingDelegate {
class AppDelegate: UIResponder, UIApplicationDelegate, MessagingDelegate {
var window: UIWindow?
let applicationController = ApplicationController()
let gcmMessageIDKey = "gcm.litewawllet_message_id"

var resourceRequest: NSBundleResourceRequest?

Expand Down Expand Up @@ -79,21 +80,28 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
return true
}

/// Pusher Related funcs
func application(_: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken _: Data) {
// Firebase Messaging
// pushNotifications.registerDeviceToken(deviceToken)

func application(_: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data)
{
let acceptanceDict: [String: String] = ["did_accept": "true",
"date_accepted": Date().ISO8601Format()]
"date_accepted": Date().ISO8601Format(),
"apns_token_status": "\(deviceToken)"]
LWAnalytics.logEventWithParameters(itemName: ._20231225_UAP, properties: acceptanceDict)
}

func application(_: UIApplication, didReceiveRemoteNotification _: [AnyHashable: Any],
fetchCompletionHandler _: @escaping (UIBackgroundFetchResult) -> Void)
func application(_: UIApplication,
didFailToRegisterForRemoteNotificationsWithError error: Error)
{
print("Unable to register for remote notifications: \(error.localizedDescription)")
}

func application(_: UIApplication,
didReceiveRemoteNotification userInfo: [AnyHashable: Any])
{
// Firebase Messaging
// pushNotifications.handleNotification(userInfo: userInfo)
/// Print message ID.
if let messageID = userInfo[gcmMessageIDKey] {
print("Message ID: \(messageID) \n \(userInfo)")
}
}

/// Sets the correct Google Services plist file
Expand All @@ -111,14 +119,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
if let fboptions = FirebaseOptions(contentsOfFile: filePath) {
FirebaseApp.configure(options: fboptions)

// [START set_messaging_delegate]
Messaging.messaging().delegate = self
// [END set_messaging_delegate]

// Register for remote notifications. This shows a permission dialog on first run, to
// show the dialog at a more appropriate time move this registration accordingly.
// [START register_for_notifications]

UNUserNotificationCenter.current().delegate = self

let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
Expand All @@ -127,42 +128,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
completionHandler: { _, _ in }
)

// Firebase Messaging
// self.pushNotifications.start(instanceId: Partner.partnerKeyPath(name: .pusher))
// let generalInterest = String.preferredLanguageInterest(currentId: UserDefaults.selectedLanguage)
// let debugGeneralInterest = "debug-general"
//
// try? self.pushNotifications.clearDeviceInterests()
//
// try? self.pushNotifications
// .addDeviceInterest(interest: generalInterest)
// try? self.pushNotifications
// .addDeviceInterest(interest: debugGeneralInterest)
//
// let interests = self.pushNotifications.getDeviceInterests()?.joined(separator: "|") ?? ""
// let device = UIDevice.current.identifierForVendor?.uuidString ?? "ID"
// let interestsDict: [String: String] = ["device_id": device,
// "pusher_interests": interests]
//
// LWAnalytics.logEventWithParameters(itemName: ._20231202_RIGI,
// properties: interestsDict)
//
// let current = UNUserNotificationCenter.current()
//
// current.getNotificationSettings(completionHandler: { settings in
//
// debugPrint(settings.debugDescription)
// if settings.authorizationStatus == .denied {
// self.pushNotifications.clearAllState {
// LWAnalytics.logEventWithParameters(itemName: ._20240506_DPN)
// }
//
// self.pushNotifications.stop {
// LWAnalytics.logEventWithParameters(itemName: ._20240510_SPN)
// }
// }
// })

} else {
let properties = ["error_message": "firebase_config_failed"]
LWAnalytics.logEventWithParameters(itemName: ._20200112_ERR,
Expand Down Expand Up @@ -217,3 +182,29 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
}
}
}

extension AppDelegate: UNUserNotificationCenterDelegate {
// Receive displayed notifications for iOS 10 devices.
func userNotificationCenter(_: UNUserNotificationCenter,
willPresent notification: UNNotification) async
-> UNNotificationPresentationOptions
{
let userInfo = notification.request.content.userInfo
if let messageID = userInfo[gcmMessageIDKey] {
print("Message ID: \(messageID) \n \(userInfo)")
}

// All options
return [[.badge, .sound, .banner]]
}

func userNotificationCenter(_: UNUserNotificationCenter,
didReceive response: UNNotificationResponse) async
{
let userInfo = response.notification.request.content.userInfo

if let messageID = userInfo[gcmMessageIDKey] {
print("Message ID: \(messageID) \n \(userInfo)")
}
}
}
40 changes: 40 additions & 0 deletions litewallet/MainViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,49 @@ class MainViewController: UIViewController, Subscriber, LoginViewControllerDeleg
}

private func addSubscriptions() {
/// Simple Redux
store.subscribe(self, selector: { $0.isLoginRequired != $1.isLoginRequired },
callback: { self.isLoginRequired = $0.isLoginRequired
})

/// Firebase Messaging
let generalTopic = String.preferredLanguageInterest(currentId: UserDefaults.selectedLanguage)
let debugTopic = "debug-tpoic"
let allTopics: [String] = [generalTopic, debugTopic]

let topicsJoined = allTopics.joined(separator: "|")
let device = UIDevice.current.identifierForVendor?.uuidString ?? "ID"
let topicsDict: [String: String] = ["device_id": device,
"topics": topicsJoined]

LWAnalytics.logEventWithParameters(itemName: ._20231202_RIGI, properties: topicsDict)

Messaging.messaging().subscribe(toTopic: debugTopic) { error in
if error != nil {
LWAnalytics.logEventWithParameters(itemName: ._20200112_ERR)
}
}

Messaging.messaging().subscribe(toTopic: generalTopic) { error in
if error != nil {
LWAnalytics.logEventWithParameters(itemName: ._20200112_ERR)
}
}

//
// current.getNotificationSettings(completionHandler: { settings in
//
// debugPrint(settings.debugDescription)
// if settings.authorizationStatus == .denied {
// self.pushNotifications.clearAllState {
// LWAnalytics.logEventWithParameters(itemName: ._20240506_DPN)
// }
//
// self.pushNotifications.stop {
// LWAnalytics.logEventWithParameters(itemName: ._20240510_SPN)
// }
// }
// })
}

private func addAppLifecycleNotificationEvents() {
Expand Down

0 comments on commit 50cf9cc

Please sign in to comment.