Skip to content
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions ios/src/ZotMeet/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

// TODO: if we're using Firebase, uncomment next string
//FirebaseApp.configure()
FirebaseApp.configure()

// [START set_messaging_delegate]
Messaging.messaging().delegate = self
Expand All @@ -22,6 +21,14 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
// [START register_for_notifications]

UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter.current().getNotificationSettings { settings in
switch settings.authorizationStatus {
case .authorized, .ephemeral, .provisional:
registerForPushNotifications()
default:
return
}
}

// let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
// UNUserNotificationCenter.current().requestAuthorization(
Expand All @@ -46,8 +53,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
print("Message ID 1: \(messageID)")
}

// Print full message.
#if DEBUG
print("push userInfo 1:", userInfo)
#endif
sendPushToWebView(userInfo: userInfo)
}

Expand All @@ -62,8 +70,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
print("Message ID 2: \(messageID)")
}

// Print full message. **
#if DEBUG
print("push userInfo 2:", userInfo)
#endif
sendPushToWebView(userInfo: userInfo)

completionHandler(UIBackgroundFetchResult.newData)
Expand Down Expand Up @@ -100,8 +109,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
print("Message ID: 3 \(messageID)")
}

// Print full message.
#if DEBUG
print("push userInfo 3:", userInfo)
#endif
sendPushToWebView(userInfo: userInfo)

// Change this to your preferred presentation option
Expand All @@ -119,8 +129,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

// With swizzling disabled you must let Messaging know about the message, for Analytics
// Messaging.messaging().appDidReceiveMessage(userInfo)
// Print full message.
#if DEBUG
print("push userInfo 4:", userInfo)
#endif
sendPushClickToWebView(userInfo: userInfo)

completionHandler()
Expand All @@ -131,7 +142,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
extension AppDelegate : MessagingDelegate {
// [START refresh_token]
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {
#if DEBUG
print("Firebase registration token: \(String(describing: fcmToken))")
#endif

let dataDict:[String: String] = ["token": fcmToken ?? ""]
NotificationCenter.default.post(name: Notification.Name("FCMToken"), object: nil, userInfo: dataDict)
Expand Down
131 changes: 46 additions & 85 deletions ios/src/ZotMeet/PushNotifications.swift
Original file line number Diff line number Diff line change
@@ -1,67 +1,23 @@
import UIKit
import WebKit
import FirebaseMessaging

class SubscribeMessage {
var topic = ""
var eventValue = ""
var unsubscribe = false
struct Keys {
static var TOPIC = "topic"
static var UNSUBSCRIBE = "unsubscribe"
static var EVENTVALUE = "eventValue"
func registerForPushNotifications() {
DispatchQueue.main.async {
UIApplication.shared.registerForRemoteNotifications()
}
convenience init(dict: Dictionary<String,Any>) {
self.init()
if let topic = dict[Keys.TOPIC] as? String {
self.topic = topic
}
if let unsubscribe = dict[Keys.UNSUBSCRIBE] as? Bool {
self.unsubscribe = unsubscribe
}
if let eventValue = dict[Keys.EVENTVALUE] as? String {
self.eventValue = eventValue
}
}
}

func handleSubscribeTouch(message: WKScriptMessage) {
// [START subscribe_topic]
let subscribeMessages = parseSubscribeMessage(message: message)
if (subscribeMessages.count > 0){
let _message = subscribeMessages[0]
if (_message.unsubscribe) {
Messaging.messaging().unsubscribe(fromTopic: _message.topic) { error in }
}
else {
Messaging.messaging().subscribe(toTopic: _message.topic) { error in }
}
}


// [END subscribe_topic]
}

func parseSubscribeMessage(message: WKScriptMessage) -> [SubscribeMessage] {
var subscribeMessages = [SubscribeMessage]()
if let objStr = message.body as? String {
private let pushPayloadKeys = ["type", "redirect", "title", "message", "groupId", "createdBy"]

let data: Data = objStr.data(using: .utf8)!
do {
let jsObj = try JSONSerialization.jsonObject(with: data, options: .init(rawValue: 0))
if let jsonObjDict = jsObj as? Dictionary<String, Any> {
let subscribeMessage = SubscribeMessage(dict: jsonObjDict)
subscribeMessages.append(subscribeMessage)
} else if let jsonArr = jsObj as? [Dictionary<String, Any>] {
for jsonObj in jsonArr {
let sMessage = SubscribeMessage(dict: jsonObj)
subscribeMessages.append(sMessage)
}
}
} catch _ {

func pushPayloadForWebView(userInfo: [AnyHashable: Any]) -> [String: String] {
var payload = [String: String]()
for key in pushPayloadKeys {
if let value = userInfo[key] as? String, !value.isEmpty {
payload[key] = value
}
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
Outdated
}
return subscribeMessages
return payload
}

func returnPermissionResult(isGranted: Bool){
Expand All @@ -82,36 +38,34 @@ func returnPermissionState(state: String){

func handlePushPermission() {
UNUserNotificationCenter.current().getNotificationSettings () { settings in
switch settings.authorizationStatus {
case .notDetermined:
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: authOptions,
completionHandler: { (success, error) in
if error == nil {
if success == true {
returnPermissionResult(isGranted: true)
DispatchQueue.main.async {
UIApplication.shared.registerForRemoteNotifications()
}
}
else {
returnPermissionResult(isGranted: false)
}
}
else {
switch settings.authorizationStatus {
case .notDetermined:
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: authOptions,
completionHandler: { (success, error) in
if error == nil {
if success == true {
registerForPushNotifications()
returnPermissionResult(isGranted: true)
} else {
returnPermissionResult(isGranted: false)
}
}
)
case .denied:
returnPermissionResult(isGranted: false)
case .authorized, .ephemeral, .provisional:
returnPermissionResult(isGranted: true)
@unknown default:
return;
}
else {
returnPermissionResult(isGranted: false)
}
}
)
case .denied:
returnPermissionResult(isGranted: false)
case .authorized, .ephemeral, .provisional:
registerForPushNotifications()
returnPermissionResult(isGranted: true)
@unknown default:
return;
}
}
}
func handlePushState() {
UNUserNotificationCenter.current().getNotificationSettings () { settings in
Expand All @@ -121,10 +75,13 @@ func handlePushState() {
case .denied:
returnPermissionState(state: "denied")
case .authorized:
registerForPushNotifications()
returnPermissionState(state: "authorized")
case .ephemeral:
registerForPushNotifications()
returnPermissionState(state: "ephemeral")
case .provisional:
registerForPushNotifications()
returnPermissionState(state: "provisional")
@unknown default:
returnPermissionState(state: "unknown")
Expand Down Expand Up @@ -161,24 +118,28 @@ func handleFCMToken(){
}

func sendPushToWebView(userInfo: [AnyHashable: Any]){
let payload = pushPayloadForWebView(userInfo: userInfo)
guard !payload.isEmpty else { return }
var json = "";
do {
let jsonData = try JSONSerialization.data(withJSONObject: userInfo)
let jsonData = try JSONSerialization.data(withJSONObject: payload)
json = String(data: jsonData, encoding: .utf8)!
} catch {
print("ERROR: userInfo parsing problem")
print("ERROR: push payload parsing problem")
return
}
checkViewAndEvaluate(event: "push-notification", detail: json)
}

func sendPushClickToWebView(userInfo: [AnyHashable: Any]){
let payload = pushPayloadForWebView(userInfo: userInfo)
guard !payload.isEmpty else { return }
var json = "";
do {
let jsonData = try JSONSerialization.data(withJSONObject: userInfo)
let jsonData = try JSONSerialization.data(withJSONObject: payload)
json = String(data: jsonData, encoding: .utf8)!
} catch {
print("ERROR: userInfo parsing problem")
print("ERROR: push payload parsing problem")
return
}
checkViewAndEvaluate(event: "push-notification-click", detail: json)
Expand Down
3 changes: 0 additions & 3 deletions ios/src/ZotMeet/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,6 @@ extension ViewController: WKScriptMessageHandler {
if message.name == "print" {
printView(webView: ZotMeet.webView)
}
if message.name == "push-subscribe" {
handleSubscribeTouch(message: message)
}
if message.name == "push-permission-request" {
handlePushPermission()
}
Expand Down
1 change: 0 additions & 1 deletion ios/src/ZotMeet/WebView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ func createWebView(container: UIView, WKSMH: WKScriptMessageHandler, WKND: WKNav
let userContentController = WKUserContentController()

userContentController.add(WKSMH, name: "print")
userContentController.add(WKSMH, name: "push-subscribe")
userContentController.add(WKSMH, name: "push-permission-request")
userContentController.add(WKSMH, name: "push-permission-state")
userContentController.add(WKSMH, name: "push-token")
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"date-fns": "^4.1.0",
"date-fns-tz": "^3.2.0",
"dotenv": "^16.4.5",
"firebase-admin": "^13.10.0",
"googleapis": "^148.0.0",
"lucide-react": "^0.453.0",
"next": "16.1.1",
Expand Down
Loading
Loading