Quick Reference for AI Agents & Developers
Requires: CometChat SDK and UI Kit both configured
Implementation: Handle notification tap in AppDelegate or SceneDelegate
Parse payload: Extract sender UID or group GUID from notification payload
Launch: Present CometChatMessages view controller with user/group
Related: Receive Message · Push Notifications · UI Kit
This guide helps you to launch a chat window from the UI Kit library on receiving a new message notification.
CometChat SDK & UI Kit both need to be configured before launching the chat window.
Step 1. Process push notification payload and grab User or Group object
To present a chat window, firstly you will need a User or a Group object. You can grab this from the push notification payload itself of incoming message notification. You need to call CometChat.processMessage() method to process push notification payload.
func userNotificationCenter ( _ center : UNUserNotificationCenter,
didReceive response : UNNotificationResponse,
withCompletionHandler completionHandler : @escaping () -> Void ) {
if let userInfo = response.notification.request.content.userInfo as? [ String : Any ], let messageObject = userInfo[ "message" ] as? [ String : Any ] {
print ( "didReceive: \\ (userInfo)" )
if let baseMessage = CometChat. processMessage (messageObject).0 {
switch baseMessage.messageCategory {
case . message :
print ( "Message Object Received: \\ (String(describing: (baseMessage as? TextMessage)?.stringValue()))" )
case . action : break
case . call : break
case . custom : break
@unknown default : break
}
}
}
completionHandler ()
}
Sample Payload - User Message Push Notification
userInfo Dictionary Structure (User Message): Parameter Type Description message [String: Any] Message dictionary containing message details. See message Dictionary below aps [String: Any] Apple Push Notification Service payload. See aps Dictionary below
message Dictionary: Parameter Type Description id Int Unique message identifier. Example: 12345 muid String Message unique ID. Example: "muid_xyz123" category String Message category. Example: "message" type String Message type. Example: "text" text String Message content. Example: "Hello there!" sender [String: Any] Sender user details. See sender Dictionary below receiver String Receiver UID. Example: "cometchat-uid-2" receiverType String Receiver type. Example: "user" sentAt Int Unix timestamp. Example: 1699800000 deliveredAt Int Delivery timestamp. Example: 0 readAt Int Read timestamp. Example: 0 conversationId String Conversation ID. Example: "cometchat-uid-1_user_cometchat-uid-2"
sender Dictionary: Parameter Type Description uid String Sender’s unique ID. Example: "cometchat-uid-1" name String Sender’s display name. Example: "Andrew Joseph" avatar String Sender’s avatar URL. Example: "https://example.com/avatar.png" status String User online status. Example: "online" role String User role. Example: "default" lastActiveAt Int Last active timestamp. Example: 1699799000
aps Dictionary: Parameter Type Description alert [String: Any] Alert content. See alert Dictionary below badge Int Badge count. Example: 1 sound String Notification sound. Example: "default" mutable-content Int Enables notification modification. Example: 1
alert Dictionary: Parameter Type Description title String Notification title. Example: "Andrew Joseph" body String Notification body. Example: "Hello there!"
Sample Payload - Group Message Push Notification
userInfo Dictionary Structure (Group Message): Parameter Type Description message [String: Any] Message dictionary containing message details. See message Dictionary below aps [String: Any] Apple Push Notification Service payload. See aps Dictionary below
message Dictionary: Parameter Type Description id Int Unique message identifier. Example: 12346 muid String Message unique ID. Example: "muid_abc456" category String Message category. Example: "message" type String Message type. Example: "text" text String Message content. Example: "Hey team!" sender [String: Any] Sender user details. See sender Dictionary below receiver [String: Any] Receiver group details. See receiver Dictionary below receiverType String Receiver type. Example: "group" sentAt Int Unix timestamp. Example: 1699800000 deliveredAt Int Delivery timestamp. Example: 0 readAt Int Read timestamp. Example: 0 conversationId String Conversation ID. Example: "group_cometchat-guid-1"
sender Dictionary: Parameter Type Description uid String Sender’s unique ID. Example: "cometchat-uid-1" name String Sender’s display name. Example: "Andrew Joseph" avatar String Sender’s avatar URL. Example: "https://example.com/avatar.png" status String User online status. Example: "online" role String User role. Example: "default" lastActiveAt Int Last active timestamp. Example: 1699799000
receiver Dictionary (Group): Parameter Type Description guid String Group’s unique ID. Example: "cometchat-guid-1" name String Group’s display name. Example: "Development Team" icon String Group’s icon URL. Example: "https://example.com/group.png" type String Group type. Example: "public" membersCount Int Number of members. Example: 15 owner String Group owner UID. Example: "cometchat-uid-1" createdAt Int Creation timestamp. Example: 1699700000 hasJoined Bool Whether user has joined. Example: true
aps Dictionary: Parameter Type Description alert [String: Any] Alert content. See alert Dictionary below badge Int Badge count. Example: 1 sound String Notification sound. Example: "default" mutable-content Int Enables notification modification. Example: 1
alert Dictionary: Parameter Type Description title String Notification title. Example: "Development Team" body String Notification body. Example: "Andrew Joseph: Hey team!"
Sample Payload - CometChat.processMessage() Output
Method Signature: Parameter Type Description Input [String: Any] Push notification message dictionary Returns (BaseMessage?, CometChatException?) Tuple containing parsed message or error
Return Tuple Structure: Index Type Description 0 BaseMessage? Parsed message object. See TextMessage Object below 1 CometChatException? Error object if parsing fails. nil on success
TextMessage Object Properties (when type is “text”):Parameter Type Description id Int Message ID. Example: 12345 muid String Message unique ID. Example: "muid_xyz123" messageCategory MessageCategory Category enum. Example: .message messageType MessageType Type enum. Example: .text text String Message text content. Example: "Hello there!" sender User Sender user object. See sender Object below receiver User or Group Receiver object. See receiver Object below receiverType ReceiverType Type of receiver. Example: .user or .group sentAt Int Unix timestamp. Example: 1699800000 deliveredAt Int Delivery timestamp. Example: 0 readAt Int Read timestamp. Example: 0 conversationId String Conversation identifier. Example: "cometchat-uid-1_user_cometchat-uid-2" editedAt Int Edit timestamp. Example: 0 editedBy String? Editor UID. Example: nil deletedAt Int Deletion timestamp. Example: 0 deletedBy String? Deleter UID. Example: nil
sender User Object: Parameter Type Description uid String User’s unique ID. Example: "cometchat-uid-1" name String User’s display name. Example: "Andrew Joseph" avatar String? User’s avatar URL. Example: "https://example.com/avatar.png" status UserStatus Online status. Example: .online role String User role. Example: "default" lastActiveAt Int Last active timestamp. Example: 1699799000
receiver Object (User or Group): When receiverType is .user: Parameter Type Description uid String User’s unique ID. Example: "cometchat-uid-2" name String User’s display name. Example: "Jane Smith" avatar String? User’s avatar URL. Example: "https://example.com/avatar2.png" status UserStatus Online status. Example: .offline
When receiverType is .group: Parameter Type Description guid String Group’s unique ID. Example: "cometchat-guid-1" name String Group’s display name. Example: "Development Team" icon String? Group’s icon URL. Example: "https://example.com/group.png" groupType GroupType Group type. Example: .public membersCount Int Number of members. Example: 15
MessageCategory Enum Values:Value Description .message Regular message (text, media, etc.) .action Action message (member joined, left, etc.) .call Call message .custom Custom message type
ReceiverType Enum Values:Value Description .user One-to-one conversation .group Group conversation
Step 2. Launch Chat Window
You can launch the chat window from your base view controller after you tap on the Message Notification. This method uses NotificationCenter to trigger and present a chat window.
In this method you need to fire notification after you receive the User or Group Object.
In Notification’s user info you can pass User or Group Object to that desired notification.
Trigger notification from App Delegate
if let message = baseMessage as? BaseMessage {
switch baseMessage.receiverType {
case . user :
DispatchQueue. main . asyncAfter ( deadline : . now () + 0.5 ) {
if let user = baseMessage.sender {
NotificationCenter. default . post ( name : NSNotification. Name ( rawValue : "didReceivedMessageFromUser" ), object : nil , userInfo : [ "user" : user])
}
}
case . group :
DispatchQueue. main . asyncAfter ( deadline : . now () + 0.5 ) {
if let group = baseMessage.receiver as? Group {
NotificationCenter. default . post ( name : NSNotification. Name ( rawValue : "didReceivedMessageFromGroup" ), object : nil , userInfo : [ "group" : group])
}
}
@unknown default : break
}
}
Sample Payload - NotificationCenter POST (User Message)
NotificationCenter.default.post() Method Parameters: Parameter Type Description name NSNotification.Name Notification identifier name. Example: NSNotification.Name(rawValue: "didReceivedMessageFromUser") object Any? Sender object (typically nil). Example: nil userInfo [AnyHashable: Any]? Dictionary containing user data. See userInfo Dictionary below
userInfo Dictionary Structure: Parameter Type Description user User The sender User object. See User Object below
User Object Properties:Parameter Type Description uid String User’s unique ID. Example: "cometchat-uid-1" name String User’s display name. Example: "Andrew Joseph" avatar String? User’s avatar URL. Example: "https://example.com/avatar.png" link String? User’s profile link. Example: nil status UserStatus Online status. Example: .online role String User role. Example: "default" lastActiveAt Int Last active timestamp. Example: 1699799000 hasBlockedMe Bool Whether user has blocked current user. Example: false blockedByMe Bool Whether current user has blocked this user. Example: false metadata [String: Any]? Custom metadata. Example: nil tags [String]? User tags. Example: []
UserStatus Enum Values:Value Description .online User is currently online .offline User is offline .available User is available
Notification Dispatch Flow: Step Action Description 1 Check receiverType baseMessage.receiverType == .user2 Extract sender baseMessage.sender3 Delay DispatchQueue.main.asyncAfter(deadline: .now() + 0.5)4 Post NotificationCenter.default.post(...)
Sample Payload - NotificationCenter POST (Group Message)
NotificationCenter.default.post() Method Parameters: Parameter Type Description name NSNotification.Name Notification identifier name. Example: NSNotification.Name(rawValue: "didReceivedMessageFromGroup") object Any? Sender object (typically nil). Example: nil userInfo [AnyHashable: Any]? Dictionary containing group data. See userInfo Dictionary below
userInfo Dictionary Structure: Parameter Type Description group Group The receiver Group object. See Group Object below
Group Object Properties:Parameter Type Description guid String Group’s unique ID. Example: "cometchat-guid-1" name String Group’s display name. Example: "Development Team" icon String? Group’s icon URL. Example: "https://example.com/group.png" description String? Group description. Example: "Team discussions" groupType GroupType Group type. Example: .public membersCount Int Number of members. Example: 15 owner String Group owner UID. Example: "cometchat-uid-1" createdAt Int Creation timestamp. Example: 1699700000 updatedAt Int Last update timestamp. Example: 1699750000 hasJoined Bool Whether current user has joined. Example: true joinedAt Int Join timestamp. Example: 1699710000 scope MemberScope Current user’s scope. Example: .participant metadata [String: Any]? Custom metadata. Example: nil tags [String]? Group tags. Example: []
GroupType Enum Values:Value Description .public Anyone can join .private Requires invitation .password Requires password to join
MemberScope Enum Values:Value Description .admin Administrator privileges .moderator Moderator privileges .participant Regular member
Notification Dispatch Flow: Step Action Description 1 Check receiverType baseMessage.receiverType == .group2 Extract receiver baseMessage.receiver as? Group3 Delay DispatchQueue.main.asyncAfter(deadline: .now() + 0.5)4 Post NotificationCenter.default.post(...)
On the other hand, you need to observe for the above notification in your base view controller.
Base view controller is a controller that launches immediately after the app delegate.
Base view controller should be present to observe the notification when notification fires.
If the view controller is not present in the memory when a new notification receives, then it won’t launch Chat Window.
Observe notification in Base View Controller
class BaseViewController : UIViewController {
override func viewDidLoad () {
NotificationCenter. default . addObserver ( self , selector : #selector ( self . didReceivedMessageFromGroup (_:)), name : NSNotification. Name ( rawValue : "didReceivedMessageFromGroup" ), object : nil )
NotificationCenter. default . addObserver ( self , selector : #selector ( self . didReceivedMessageFromUser (_:)), name : NSNotification. Name ( rawValue : "didReceivedMessageFromUser" ), object : nil )
}
}
Add selector method & Launch Chat Window
@objc func didReceivedMessageFromGroup ( _ notification : NSNotification) {
if let group = notification.userInfo ? [ "group" ] as? Group {
DispatchQueue. main . async {
let messageList = CometChatMessageList ()
let navigationController = UINavigationController ( rootViewController :messageList)
messageList. set ( conversationWith : group, type : . group )
self . present (navigationController, animated : true , completion : nil )
}
}
}
@objc func didReceivedMessageFromUser ( _ notification : NSNotification) {
print ( "didReceivedMessageFromUser" )
if let user = notification.userInfo ? [ "user" ] as? User {
DispatchQueue. main . async {
let messageList = CometChatMessageList ()
let navigationController = UINavigationController ( rootViewController :messageList)
messageList. set ( conversationWith : user, type : . user )
self . present (navigationController, animated : true , completion : nil )
}
}
}
Sample Payload - didReceivedMessageFromGroup Notification
NSNotification Object Structure: Parameter Type Description name NSNotification.Name Notification identifier. Example: "didReceivedMessageFromGroup" object Any? Sender object. Example: nil userInfo [AnyHashable: Any]? Dictionary containing group data. See userInfo Dictionary below
userInfo Dictionary: Parameter Type Description group Group The Group object from notification. See Group Object below
Group Object Properties:Parameter Type Description guid String Group’s unique ID. Example: "cometchat-guid-1" name String Group’s display name. Example: "Development Team" icon String? Group’s icon URL. Example: "https://example.com/group.png" description String? Group description. Example: "Team discussions" groupType GroupType Group type. Example: .public membersCount Int Number of members. Example: 15 owner String Group owner UID. Example: "cometchat-uid-1" createdAt Int Creation timestamp. Example: 1699700000 hasJoined Bool Whether current user has joined. Example: true scope MemberScope Current user’s scope. Example: .participant
CometChatMessageList Configuration: Parameter Type Description set(conversationWith:type:) (Group, ReceiverType) Method to set group conversation
set(conversationWith:type:) Parameters: Parameter Type Description conversationWith Group The Group object to open conversation with type ReceiverType Receiver type. Example: .group
Selector Method Flow: Step Action Description 1 Extract notification.userInfo?["group"] as? Group2 Dispatch DispatchQueue.main.async { ... }3 Initialize let messageList = CometChatMessageList()4 Wrap UINavigationController(rootViewController: messageList)5 Configure messageList.set(conversationWith: group, type: .group)6 Present self.present(navigationController, animated: true)
Sample Payload - didReceivedMessageFromUser Notification
NSNotification Object Structure: Parameter Type Description name NSNotification.Name Notification identifier. Example: "didReceivedMessageFromUser" object Any? Sender object. Example: nil userInfo [AnyHashable: Any]? Dictionary containing user data. See userInfo Dictionary below
userInfo Dictionary: Parameter Type Description user User The User object from notification. See User Object below
User Object Properties:Parameter Type Description uid String User’s unique ID. Example: "cometchat-uid-1" name String User’s display name. Example: "Andrew Joseph" avatar String? User’s avatar URL. Example: "https://example.com/avatar.png" link String? User’s profile link. Example: nil status UserStatus Online status. Example: .online role String User role. Example: "default" lastActiveAt Int Last active timestamp. Example: 1699799000 hasBlockedMe Bool Whether user has blocked current user. Example: false blockedByMe Bool Whether current user has blocked this user. Example: false
CometChatMessageList Configuration: Parameter Type Description set(conversationWith:type:) (User, ReceiverType) Method to set user conversation
set(conversationWith:type:) Parameters: Parameter Type Description conversationWith User The User object to open conversation with type ReceiverType Receiver type. Example: .user
Selector Method Flow: Step Action Description 1 Extract notification.userInfo?["user"] as? User2 Dispatch DispatchQueue.main.async { ... }3 Initialize let messageList = CometChatMessageList()4 Wrap UINavigationController(rootViewController: messageList)5 Configure messageList.set(conversationWith: user, type: .user)6 Present self.present(navigationController, animated: true)