Skip to main content
Quick Reference for AI Agents & Developers
  • Requires: UNNotificationServiceExtension target in your app
  • Purpose: Mark messages delivered even when app is in background/terminated
  • Implementation: Call CometChat.markAsDelivered() in notification extension
  • Parse payload: Extract message ID, sender ID, receiver type from notification
  • Related: Delivery & Read Receipts · Push Notifications
Implementing the capability to mark a message as “delivered” through a push notification payload can prove to be a pivotal feature. This functionality serves as an accurate representation, confirming to the sender that their message has indeed reached its intended recipient, thereby enhancing the overall user experience. Setting up Mark as delivered from push notification in iOS requires to have Notification extension to your app project. So let’s begin with adding a Notification extension to your app project.
If you already have a Notification extension on your project then you can skip the first part of creating a new extension and use the one you have.

Setting up Notification extension

  1. Navigate to your project’s target section and click on the plus iOS
  1. Then Scroll down, select Notification Service Extension and click on the next.
  1. Give it a name and click finish
  • Your notification service extension is ready now, you can see that in the target list. Now we will create app group for the extension. This app group will be needed to share user default between the main application and notification service extension.
  1. Click on the notification service extension name on the target list, then select Signing & Capabilities, then click on “+ Capability” from the top. Make sure All is selected besides it.
  1. Then search for App Group and click on it. You can see a section of the app group section must have been added on the ‘Signing & Capabilities’.
  1. Select any of the suggested app group ID or you can add your own ID by clicking on the plus icon. Till this step app group is added on the notification extension target and app group is selected.
  1. Now we will add the Capability of App Group on the main app’s target as well. Just repeat the same steps that you have done on the notification extension(from step 4). You have to select your main app from the target list, then as we did for the notification extension switch to ‘Signing & Capabilities’, then click on “+ Capability” from the top and select App Group.
  2. Then select the same group ID that you have selected on the notification extension target’s app group. If it is not there in the suggested IDs, then add it by clicking on the plus icon.
Make sure the app ID is exactly the same on both the notification extension target and the main app target.
  1. Lastly, add CometChatSDK in your notification extension target in the pod file.
# Uncomment the next line to define a global platform for your project
# platform :ios, '12.0'

target 'CometChatSwift' do

  pod 'CometChatSDK', '4.1.0'
  pod 'CometChatCallsSDK', '4.2.2'

end

# add your notification extension name here
target 'NotificationExtension' do

  pod 'CometChatSDK', '4.0.67'

end
All the setup is done, let’s see some code now.

Code for Mark as read

  1. Firstly navigate to the cometchat initialization code on you project and add the group ID that you have selected on app groups on the AppSettingsBuilder.
let appSettings = AppSettings.AppSettingsBuilder()
                .subscribePresenceForAllUsers()
                .setRegion(region: Constants.region)
                .setExtensionGroupID(id: "group.com.comechatcalls.appgroup") //add you app group ID here
                .build()
Configuration Parameters:
ParameterTypeDescription
subscribePresenceForAllUsers()MethodSubscribe to presence updates for all users
setRegion(region:)StringCometChat region. Example: "us"
setExtensionGroupID(id:)StringApp Group ID shared between main app and extension. Example: "group.com.yourapp.appgroup"
Important Notes:
NoteDescription
App Group IDMust match exactly in both main app and notification extension
CapabilityApp Groups capability must be enabled in both targets
Shared dataAllows SDK to share user session between app and extension
  1. Now navigate to your notification extension named group that is been created And open its swift file.
  2. Then import CometChatSDK on that file and call these 2 functions on the didReceive(_ request: , withContentHandler: ) function.
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
        self.contentHandler = contentHandler
        bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)        

        CometChat.setExtensionGroupID(id: "group.com.comechatcalls.appgroup") //add you group id
    
        if let bestAttemptContent = bestAttemptContent {
            
            CometChat.markAsDelivered(withNotificationPayload: bestAttemptContent.userInfo) //send the payload here
            
            contentHandler(bestAttemptContent)
        }
    }
Method Signature:
MethodParameterDescription
setExtensionGroupID(id:)StringSet the App Group ID before marking delivered
markAsDelivered(withNotificationPayload:)[AnyHashable: Any]Push notification payload containing message data
Sample Push Notification Payload (userInfo):
ParameterTypeDescription
apsDictionaryApple Push Notification Service data
aps.alert.titleStringNotification title. Example: "John Doe"
aps.alert.bodyStringNotification body. Example: "Hello, how are you?"
aps.mutable-contentIntMust be 1 for notification extension. Example: 1
messageDictionaryCometChat message data
message.idIntUnique message identifier. Example: 1772174760
message.senderStringSender UID. Example: "cometchat-uid-2"
message.receiverStringReceiver UID or GUID. Example: "cometchat-uid-1"
message.receiverTypeStringType of receiver. Example: "user" or "group"
Effect After Marking Delivered:
EffectDescription
Server updateMessage marked as delivered on CometChat server
Delivery receiptSender receives delivery receipt (if listening)
TimestampMessage deliveredAt timestamp updated
Background supportWorks even when app is in background/terminated
Complete userInfo Dictionary:
ParameterTypeDescription
aps.alert.titleStringSender name. Example: "John Doe"
aps.alert.bodyStringMessage text. Example: "Hello, how are you?"
aps.mutable-contentIntEnables modification. Example: 1
aps.soundStringNotification sound. Example: "default"
message.idIntMessage ID. Example: 12345
message.muidStringMessage unique ID. Example: "msg_abc123"
message.senderStringSender UID. Example: "john_doe"
message.receiverStringReceiver UID. Example: "jane_smith"
message.receiverTypeStringReceiver type. Example: "user"
message.typeStringMessage type. Example: "text"
message.textStringMessage content. Example: "Hello, how are you?"
message.sentAtIntUnix timestamp. Example: 1699800000
message.conversationIdStringConversation ID. Example: "john_doe_user_jane_smith"
Complete userInfo Dictionary:
ParameterTypeDescription
aps.alert.titleStringGroup name. Example: "Team Chat"
aps.alert.bodyStringMessage with sender. Example: "John: Meeting at 3pm"
aps.mutable-contentIntEnables modification. Example: 1
message.idIntMessage ID. Example: 12346
message.senderStringSender UID. Example: "john_doe"
message.receiverStringGroup GUID. Example: "team_chat_guid"
message.receiverTypeStringReceiver type. Example: "group"
message.typeStringMessage type. Example: "text"
message.textStringMessage content. Example: "Meeting at 3pm"
message.sentAtIntUnix timestamp. Example: 1699800100
Usage Scenarios:
ScenarioUse markAsDelivered?Reason
App in foregroundNoSDK handles automatically via WebSocket
App in backgroundYesVia Notification Service Extension
App terminatedYesVia Notification Service Extension
Prerequisites:
RequirementDescription
Notification ExtensionUNNotificationServiceExtension target added to project
App GroupsCapability enabled in both main app and extension
Same Group IDIdentical App Group ID in both targets
CometChatSDKAdded to extension target in Podfile
And we are finally done, run your notification extension by selecting the notification extension target from the run target on the top.
Run the main app target first and make sure you are receiving notifications there. And then run the notification extension target.