Skip to content

Commit

Permalink
push notificatinos for messaging done. need to add for session booking
Browse files Browse the repository at this point in the history
  • Loading branch information
skucheria committed Jul 8, 2019
1 parent e4761fe commit 414f2c8
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 4 deletions.
4 changes: 4 additions & 0 deletions Helpers/LoginHandlers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ extension LoginVC : UIImagePickerControllerDelegate, UINavigationControllerDeleg
}
// imageRef.putData(uploadData)
}
let pushManager = PushNotificationManager(userID: Auth.auth().currentUser!.uid)
pushManager.registerForPushNotifications()
let newVC = MainTabController()
self.present(newVC, animated: true)

Expand All @@ -98,6 +100,8 @@ extension LoginVC : UIImagePickerControllerDelegate, UINavigationControllerDeleg
}
}



}

func saveData(){
Expand Down
7 changes: 5 additions & 2 deletions Helpers/PushNotificationManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import FirebaseFirestore
import FirebaseMessaging
import UIKit
import UserNotifications

class PushNotificationManager: NSObject, MessagingDelegate, UNUserNotificationCenterDelegate {
let userID: String
init(userID: String) {
Expand All @@ -37,8 +38,10 @@ class PushNotificationManager: NSObject, MessagingDelegate, UNUserNotificationCe
}
func updateFirestorePushTokenIfNeeded() {
if let token = Messaging.messaging().fcmToken {
let usersRef = Firestore.firestore().collection("users_table").document(userID)
usersRef.setData(["fcmToken": token], merge: true)
let ref = Database.database().reference().child("user").child(Auth.auth().currentUser!.uid)
ref.updateChildValues(["fcmToken" : token])
// let usersRef = Firestore.firestore().collection("users_table").document(userID)
// usersRef.setData(["fcmToken": token], merge: true)
}
}
func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
Expand Down
37 changes: 37 additions & 0 deletions Helpers/PushNotificationSender.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//
// PushNotificationSender.swift
// LearnLab
//
// Created by Siddharth Kucheria on 7/7/19.
// Copyright © 2019 Siddharth Kucheria. All rights reserved.
//

import UIKit

class PushNotificationSender {
func sendPushNotification(to token: String, title: String, body: String) {
let urlString = "https://fcm.googleapis.com/fcm/send"
let url = NSURL(string: urlString)!
let paramString: [String : Any] = ["to" : token,
"notification" : ["title" : title, "body" : body],
"data" : ["user" : "uid"]
]
let request = NSMutableURLRequest(url: url as URL)
request.httpMethod = "POST"
request.httpBody = try? JSONSerialization.data(withJSONObject:paramString, options: [.prettyPrinted])
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.setValue("key=AAAAULYm_20:APA91bFDuFG1w5sH30TF8Zc5Lv2b8ALwhSl9Y1X2i3_c5IqFZMK4Au51aWE_pFAEY2WPEUg8Y4oIAd3Ldb7qVMwmxcxPvvkot4hY86r1qenYDYAq8cP-EBk0cgaLlemHFCgBnLJXrYFi", forHTTPHeaderField: "Authorization")
let task = URLSession.shared.dataTask(with: request as URLRequest) { (data, response, error) in
do {
if let jsonData = data {
if let jsonDataDict = try JSONSerialization.jsonObject(with: jsonData, options: JSONSerialization.ReadingOptions.allowFragments) as? [String: AnyObject] {
NSLog("Received data:\n\(jsonDataDict))")
}
}
} catch let err as NSError {
print(err.debugDescription)
}
}
task.resume()
}
}
4 changes: 4 additions & 0 deletions LearnLab.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
3501305E22BDFE4A00A5DB9C /* Reviews.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3501305D22BDFE4A00A5DB9C /* Reviews.swift */; };
3526143D22D078DD0062476B /* Registration3VC.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3526143C22D078DD0062476B /* Registration3VC.swift */; };
3526143F22D2F4160062476B /* PushNotificationManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3526143E22D2F4160062476B /* PushNotificationManager.swift */; };
3526144122D306DB0062476B /* PushNotificationSender.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3526144022D306DB0062476B /* PushNotificationSender.swift */; };
352C9D5922CC7AF80094B775 /* BookSessionVC.swift in Sources */ = {isa = PBXBuildFile; fileRef = 352C9D5822CC7AF80094B775 /* BookSessionVC.swift */; };
352C9D5B22CC854D0094B775 /* PendingSessionCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 352C9D5A22CC854D0094B775 /* PendingSessionCell.swift */; };
353AD7F6229515B70063596E /* Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 353AD7F5229515B70063596E /* Extensions.swift */; };
Expand Down Expand Up @@ -87,6 +88,7 @@
3501305D22BDFE4A00A5DB9C /* Reviews.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Reviews.swift; sourceTree = "<group>"; };
3526143C22D078DD0062476B /* Registration3VC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Registration3VC.swift; sourceTree = "<group>"; };
3526143E22D2F4160062476B /* PushNotificationManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PushNotificationManager.swift; sourceTree = "<group>"; };
3526144022D306DB0062476B /* PushNotificationSender.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PushNotificationSender.swift; sourceTree = "<group>"; };
352C9D5822CC7AF80094B775 /* BookSessionVC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BookSessionVC.swift; sourceTree = "<group>"; };
352C9D5A22CC854D0094B775 /* PendingSessionCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PendingSessionCell.swift; sourceTree = "<group>"; };
353AD7F5229515B70063596E /* Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Extensions.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -212,6 +214,7 @@
357D4137228E6CB700EEEFE6 /* LoginHandlers.swift */,
35B33D1F229CA5AC00B207F4 /* LoadingIndicator.swift */,
3526143E22D2F4160062476B /* PushNotificationManager.swift */,
3526144022D306DB0062476B /* PushNotificationSender.swift */,
);
path = Helpers;
sourceTree = "<group>";
Expand Down Expand Up @@ -692,6 +695,7 @@
35B597CB22A4577E00730535 /* NewChatVC.swift in Sources */,
353AD7F822952D420063596E /* ChatLogVC.swift in Sources */,
35C1C7A1228E24390060AAA3 /* User.swift in Sources */,
3526144122D306DB0062476B /* PushNotificationSender.swift in Sources */,
3560D8592289030C003C12E0 /* SettingsVC.swift in Sources */,
3560D85122888AF1003C12E0 /* MainTabController.swift in Sources */,
35D2995A22855346005DF851 /* AppDelegate.swift in Sources */,
Expand Down
Binary file not shown.
5 changes: 3 additions & 2 deletions LearnLab/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let pushManager = PushNotificationManager(userID: "currently_logged_in_user_id")
pushManager.registerForPushNotifications()
FirebaseApp.configure()

// let pushManager = PushNotificationManager(userID: Auth.auth().currentUser!.uid)
// pushManager.registerForPushNotifications()

STPPaymentConfiguration.shared().publishableKey = "pk_test_WtVz0DDMD8ZALlmcqpT1WTCd00bopR2M5R"

Expand Down
1 change: 1 addition & 0 deletions LearnLab/Models - Chat/User.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ class User: NSObject {
var reviews : [Review]?
var availability : String?
var rate : String?
var fcmToken : String?
}
3 changes: 3 additions & 0 deletions LearnLab/ViewControllers/ChatLogVC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,9 @@ class ChatLogVC : UICollectionViewController, UITextFieldDelegate, UICollectionV
let recipientMessageRef = Database.database().reference().child("group-messages").child(toUser!.id!)

recipientMessageRef.updateChildValues([childRef.key! : 1])

let sender = PushNotificationSender()
sender.sendPushNotification(to: toUser!.fcmToken!, title: "New Message", body: "Message to \(toUser!.name!)")
}

func groupMessages(key : String){
Expand Down
1 change: 1 addition & 0 deletions LearnLab/ViewControllers/MessagesVC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ class MessagesVC: UITableViewController {
curr.name = item.value["name"] as? String
curr.profLinik = item.value["profilePic"] as? String
curr.id = chatPartnerID
curr.fcmToken = item.value["fcmToken"] as? String
self.showChatVC(curr)
}
}
Expand Down

0 comments on commit 414f2c8

Please sign in to comment.