Guest User

Untitled

a guest
Oct 18th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.79 KB | None | 0 0
  1. func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (_ options: UNNotificationPresentationOptions) -> Void) {
  2.  
  3. let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
  4. let messageVC = mainStoryboard.instantiateViewController(withIdentifier: "messageVC") as! messagesViewController
  5.  
  6. if let incomingMessageSenderId = notification.request.content.userInfo["User_ID"] as? NSString {
  7.  
  8. incomingMsgSenderId = String(incomingMessageSenderId)
  9. }
  10. if let message = notification.request.content.userInfo["Message"] as? String {
  11.  
  12. msg = String(message)
  13.  
  14. }
  15.  
  16. DispatchQueue.main.async {
  17.  
  18. messageVC.checkForMessages(id: incomingMsgSenderId)
  19. }
  20.  
  21. }
  22.  
  23. func checkForMessages(id: String) {
  24.  
  25. messages.removeAll()
  26.  
  27. let context : NSManagedObjectContext = appDel.managedObjectContext
  28.  
  29. let request = NSFetchRequest<NSFetchRequestResult>(entityName: "Nd_conversation")
  30.  
  31. request.returnsObjectsAsFaults = false
  32.  
  33. request.predicate = NSPredicate(format: "to_user_id = %@", id)
  34.  
  35. do {
  36. let results = try context.fetch(request)
  37.  
  38. if results.count > 0 {
  39.  
  40. for result in results as! [NSManagedObject] {
  41.  
  42. let conversation_id = result.value(forKey: "id")
  43.  
  44. let request2 = NSFetchRequest<NSFetchRequestResult> (entityName: "Nd_chat")
  45. request2.returnsObjectsAsFaults = false
  46.  
  47. request2.predicate = NSPredicate(format: "conversation_id = %@", conversation_id! as! CVarArg)
  48.  
  49. do {
  50.  
  51. let results2 = try context.fetch(request2)
  52.  
  53. if results2.count > 0 {
  54. for result2 in results2 as! [NSManagedObject] {
  55.  
  56. if let body = result2.value(forKey: "message_body") as? String {
  57.  
  58.  
  59. loadedMessages.append(body)
  60.  
  61. if let sender_id = result2.value(forKey: "sender_id") as? Int {
  62.  
  63. let sent_id = String(sender_id)
  64.  
  65. print("Sender id: (sent_id)")
  66.  
  67. senderIds.append(sender_id)
  68.  
  69. self.addMessage(sent_id, text: body)
  70. self.finishSendingMessage(animated: true)
  71.  
  72. }
  73.  
  74.  
  75. }
  76. else if let imageData = result2.value(forKey: "media_path") as? NSData{
  77. //photo
  78.  
  79. let image = UIImage(data: imageData as Data)
  80.  
  81. let photoItem = JSQPhotoMediaItem(image: image)
  82.  
  83. if let sender_id = result2.value(forKey: "sender_id") as? Int {
  84.  
  85. let photoMessage = JSQMessage(senderId: String(sender_id), displayName: self.senderDisplayName, media: photoItem)
  86.  
  87. messages.append(photoMessage!)
  88.  
  89. self.finishSendingMessage(animated: true)
  90. }
  91. }
  92. }
  93. }
  94. }
  95. }
  96. }
  97.  
  98. extension Notification.Name {
  99. static let reload = Notification.Name("reload")
  100. }
  101.  
  102. NotificationCenter.default.post(name: .reload, object: nil)
  103.  
  104. override func viewDidLoad() {
  105. super.viewDidLoad()
  106. NotificationCenter.default.addObserver(self, selector:#selector(reloadTableData(_:)), name: .reload, object: nil)
  107. }
  108.  
  109. func reloadTableData(_ notification: Notification) {
  110. tableView.reloadData()
  111. }
Add Comment
Please, Sign In to add comment