Skip to main content

iOS UI Kit Sample App

Reference implementation of iOS UIKit, APNs and Push Notification Setup.

What this guide covers

  • CometChat dashboard setup (enable push, add APNs Device + APNs VoIP providers) with screenshots.
  • APNs + PushKit/CallKit wiring (tokens, delegates, CallKit).
  • Incoming message/call handling and deep links.
  • Badge count and grouped notifications.
  • Payload customization and testing.

How APNs + CometChat work together

  • APNs is the transport: Apple issues the APNs device/VoIP tokens and delivers the payloads. No FCM bridge is involved.
  • CometChat providers: The APNs Device and APNs VoIP providers you add in the CometChat dashboard hold your APNs key/cert. When you call CometChatNotifications.registerPushToken(..., .APNS_IOS_DEVICE / .APNS_IOS_VOIP, providerId) after login, CometChat binds those tokens to the logged-in user and sends to APNs for you.
  • Flow: Permission prompt → APNs returns device + VoIP tokens → after CometChat.login, register both tokens with the matching provider IDs → CometChat sends to APNs → APNs delivers → UNUserNotificationCenterDelegate (and PushKit/CallKit for VoIP) surface the notification/tap.

1. Enable push and add providers (CometChat Dashboard)

  1. Go to Notifications → Settings and enable Push Notifications.
Enable Push Notifications
  1. Click Add Credentials:
    • Add an APNs Device provider (alerts) using your .p8 key, Team ID, Key ID, and Bundle ID; copy the Provider ID.
    • Add an APNs VoIP provider (calls) with the same .p8 (recommended for CallKit reliability); copy the Provider ID.
Add APNs credentials
Keep the provider IDs—you’ll paste them into your app constants.

2. Apple setup

  1. Capabilities: Push Notifications, Background Modes → Remote notifications & Voice over IP, CallKit usage descriptions in Info.plist (mic/camera).
  2. APNs Auth Key: generate .p8 (or use cert), note Key ID, Team ID, and Bundle ID; upload to CometChat providers.
Enable Push Notifications and Background Modes for APNs

3. Wiring APNs + PushKit/CallKit

  • From below code, copy CometChatAPNsHelper.swift, CometChatPNHelper.swift, and the two AppDelegate extensions (AppDelegate+PN.swift and AppDelegate+VoIP.swift) into your project.
  • These files implement APNs + PushKit/CallKit handling, notification presentation, tap and quick-reply actions, and call management.
  • Update bundle ID, team ID, and provider IDs (AppConstants.PROVIDER_ID etc.). Keep the voip push type.

4. Register APNs device + VoIP tokens with CometChat

  • In your AppDelegate.swift, implement the following methods to handle APNs registration success and failure, and to register the device token with CometChat.
  • Make sure to import the necessary modules at the top of the file.
  • Complete your AppDelegate.swift as shown below:

5. Testing checklist

  1. Install on a device; grant notification permission. Verify APNs device token logs.
  2. Log in, then confirm both device + VoIP tokens register with CometChat (success callbacks).
  3. Send a message from another user:
    • Foreground: ensure willPresent shows your chosen presentation.
    • Background/terminated: tapping opens the correct conversation.
  4. Trigger an incoming call; CallKit UI should show caller info. Accept should join the call; Decline should reject via CometChat and end CallKit.
  5. Rotate tokens (reinstall or toggle VoIP) to ensure re-registration works.

6. Badge count and grouped notifications

CometChat’s Enhanced Push Notification payload includes an unreadMessageCount field (a string) representing the total unread messages across all conversations for the logged-in user. Use this to set the app icon badge and enrich local notifications.

6.1 Enable unread badge count on the CometChat Dashboard

1

Navigate to Push Notification Preferences

Go to CometChat Dashboard → Notifications → Settings → Preferences → Push Notification Preferences.
2

Enable Unread Badge Count

Scroll to the bottom and enable the Unread Badge Count toggle.
This ensures CometChat includes the unreadMessageCount field in every push payload sent to your app.

6.2 Clear badge count when app becomes active

Add the following to your SceneDelegate.swift to reset the badge when the user opens the app:

6.3 Update badge count from push notifications

Update your AppDelegate+PN.swift to handle badge count updates in both foreground and background states:
The unreadMessageCount field is sent as a string in the payload. Always parse it to an integer and validate it’s non-negative before updating the badge.

7. Troubleshooting