ios – Implementing notifications into MessageKit


I carried out chat utilizing MessageKit and Firebase and was questioning the best way to implement notifications each time the person receives a message, I’ve arrange my permissions however I am having hassle determining figuring out when a brand new message is distributed. Any concepts can be appreciated.

 override func viewWillAppear(_ animated: Bool) {
        tremendous.viewWillAppear(animated)
        
        let content material = UNMutableNotificationContent()
        
        databaseListener = channelRef?.order(by:"time").addSnapshotListener() { (querySnapshot, error) in
        
            if let error = error {
                print(error)
                return
            }
            
            querySnapshot?.documentChanges.forEach() { diff in
                
                if diff.kind == .added {
                    let snapshot = diff.doc
                    
                    let id = snapshot.documentID
                    let senderId = snapshot["senderId"] as! String
                    let senderName = snapshot["senderName"] as! String
                    let textual content = snapshot["text"] as! String
                    let timestamp = snapshot["time"] as! Timestamp
                    let date = sentTimestamp.dateValue()

                    let sender = Sender(id: senderId, identify: senderName)
                    let message = ChatMessage(sender: sender, messageId: id, sentDate: date, message: textual content)
                    
                    self.messagesList.append(message)
                    
                    // Notification
                    content material.title = "New message"
                    content material.physique = "Faucet to reply"
                    content material.categoryIdentifier = AppDelegate.CATEGORY_IDENTIFIER
                    let set off = UNTimeIntervalNotificationTrigger(timeInterval: 2, repeats: false)
                    
                    let request = UNNotificationRequest(identifier: ChatMessagesViewController.NOTIFICATION_IDENTIFIER, content material: content material, set off: set off)
                    
                    UNUserNotificationCenter.present().add(request, withCompletionHandler: nil)
                    
                    self.messagesCollectionView.insertSections([self.messagesList.count-1])
                }
            }
        }
    }

See also  Introduction to SPM artifact bundles

Leave a Reply