iOS Notifications
Learn how to implement and handle push notifications in your Heapchat iOS application
Setup Steps
1. Enable Push Notification Capability
First, you need to enable push notifications in your Xcode project:
- Open your project in Xcode
- Select your target
- Go to the "Signing & Capabilities" tab
- Click the "+" button and add "Push Notifications"
2. Add Required Permissions
Add the following permission to your Info.plist
:
Required Permissions
- Privacy - Notifications Usage Description: Add a message explaining why your app needs notifications
<key>NSUserNotificationUsageDescription</key> <string>We'll send you updates about new messages and important alerts</string>
3. Configure App Delegate
First, modify your @main
struct to include the app delegate:
@main
struct SampleApp: App {
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
Then, create or update your AppDelegate.swift
with the following implementation:
import UIKit
import HeapchatSDK
class AppDelegate: NSObject, UIApplicationDelegate, UNUserNotificationCenterDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Set notification delegate
UNUserNotificationCenter.current().delegate = self
// Request notification permissions
requestNotificationPermissions(application)
return true
}
private func requestNotificationPermissions(_ application: UIApplication) {
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { success, error in
if let error = error {
print("Error requesting notification permissions: \(error)")
return
}
guard success else {
print("User denied notification permissions")
return
}
DispatchQueue.main.async {
application.registerForRemoteNotifications()
}
}
}
// Handle successful device token registration
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
let tokenParts = deviceToken.map { data in String(format: "%02.2hhx", data) }
let tokenString = tokenParts.joined()
// Send token to Heapchat
Heapchat.shared.setDeviceToken(tokenString)
}
// Handle registration errors
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
print("Failed to register for remote notifications: \(error)")
}
}
4. Handling Notifications
Create a new file called HeapchatNotificationHandler.swift
to handle notification events:
import Foundation
import UIKit
import SwiftUI
import HeapchatSDK
extension AppDelegate {
// Handles navigation to CustomerSupportScreen when a notification is tapped
func userNotificationCenter(
_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void
) {
let result = Heapchat.shared.handleNotification(response: response, withCompletionHandler: completionHandler)
if result {
navigateToCustomerSupportScreen()
}
}
func userNotificationCenter(
_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void
) {
Heapchat.shared.suppressNotification(withCompletionHandler: completionHandler)
}
private func navigateToCustomerSupportScreen() {
DispatchQueue.main.async {
// Your code to navigate to the customer support screen
}
}
}
Understanding Notification Handling
The notification handling implementation provides several key features:
-
Notification Taps: When a user taps a notification, the
didReceive
method is called:- Processes the notification using
Heapchat.shared.handleNotification
- Automatically navigates to the Customer Support screen if needed
- Processes the notification using
-
Foreground Notifications: When a notification arrives while the app is in foreground:
- The
willPresent
method is called - Uses
suppressNotification
to handle the notification according to Heapchat's logic
- The
-
Navigation: The
navigateToCustomerSupportScreen
method:- Ensures UI updates happen on the main thread
- Setup the navigation to the customer support screen