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.
.p12 certificates are deprecated. Apple recommends using .p8 Auth Keys for push notifications. .p8 keys are simpler to manage (one key works for all your apps), never expire, and are the only format actively supported going forward. If you are still using .p12, migrate to .p8 at your earliest convenience.
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. Unregister the token on logout

Before logging the user out, unregister the push token so the device stops receiving notifications for that user.
Always call CometChatNotifications.unregisterPushToken() before CometChatUIKit.logout(). If you skip this step, the device may continue to receive pushes for the logged-out user.

6. Badge count

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. You can use this to set the app icon badge.

6.1 Enable unread badge count on the CometChat Dashboard

  1. Go to CometChat Dashboard → Notification Engine → Settings → Preferences → Push Notification Preferences.
  2. 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 Expected payload format

CometChat sends APNs payloads with this structure (relevant fields):
unreadMessageCount is a string representing the total unread messages across all conversations for the logged-in user.

6.3 Update the app badge from the push payload

Inside your UNUserNotificationCenterDelegate method (for example willPresent or a Notification Service Extension), parse unreadMessageCount and update the badge:
Setting applicationIconBadgeNumber to 0 clears the badge.

6.4 Clear badge when the app opens

Clear the badge count when the app launches and every time it returns to the foreground. In your SceneDelegate or AppDelegate:
This keeps the badge in sync with the actual unread state.

7. Navigation from notifications

When the user taps a notification, use userNotificationCenter(_:didReceive:withCompletionHandler:) to extract conversation details and navigate to the correct screen.
The navigateToViewController helper (shown in CometChatPNHelper.swift above) pushes the MessagesVC onto the navigation stack. Ensure the root view controller is ready before navigation — if the app was terminated, wait until login completes before routing.

8. 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. Troubleshooting