Integrate Apple Push Notifications into your iOS app to deliver instant updates about messages, mentions, and community activities, keeping users engaged even when your app isn’t active.
Push notifications require proper APNs certificate setup and valid entitlements. Production certificates work with App Store and TestFlight builds only.

Prerequisites

Before implementing push notifications, ensure you have:

Apple Developer Account

Active Apple Developer Program membership

Xcode Project

iOS project with proper Bundle ID configured

social.plus SDK

social.plus iOS SDK properly integrated

Push Capability

Push Notifications capability enabled in Xcode

Step 1: Generate APNs Certificate

Create Certificate in Apple Developer Console

  1. Navigate to Certificates
  2. Select Certificate Type
    • Choose Apple Push Notification service SSL (Sandbox & Production)
    • This universal certificate works for both development and production
Universal Certificate: The Sandbox & Production certificate eliminates the need for separate development and production certificates, simplifying your workflow.
  1. Configure Certificate
    • Select your app’s Bundle ID from the dropdown
    • Follow Apple’s certificate creation wizard
    • Download the generated .cer file

Convert Certificate to .p12 Format

  1. Install Certificate
    • Double-click the downloaded .cer file
    • Keychain Access will open automatically
    • The certificate will be installed in your Login keychain
  2. Export as .p12
    • In Keychain Access, navigate to Login → My Certificates
    • Locate your Apple Push Services certificate
    • Right-click and select Export “Apple Push Services…”
    • Choose .p12 format and set a secure password
    • Save the file (you’ll need both the file and password for console upload)
Certificate Security: Store your .p12 file and password securely. When you create a new certificate, the previous one is automatically revoked.

Step 2: Upload to social.plus Console

  1. Access Console
  2. Upload Certificate
    • Click Upload Certificate in the iOS section
    • Select your .p12 file
    • Enter the password you set during export
    • Click Save to complete the setup
Certificate Validation: The console will validate your certificate upon upload. If there are issues, check that the Bundle ID matches your app configuration.

Step 3: iOS App Configuration

Enable Push Notifications Capability

In Xcode, enable the Push Notifications capability:
  1. Select your app target
  2. Go to Signing & Capabilities
  3. Click + Capability
  4. Add Push Notifications

Request Permission and Handle Registration

import UIKit
import UserNotifications
import AmitySDK

@main
class AppDelegate: UIResponder, UIApplicationDelegate {
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        
        // Configure notification center
        UNUserNotificationCenter.current().delegate = self
        
        // Request notification permissions
        requestNotificationPermissions()
        
        return true
    }
    
    private func requestNotificationPermissions() {
        UNUserNotificationCenter.current().requestAuthorization(
            options: [.alert, .badge, .sound, .provisional]
        ) { granted, error in
            if let error = error {
                print("Push notification permission error: \(error.localizedDescription)")
                return
            }
            
            if granted {
                print("Push notifications authorized")
                DispatchQueue.main.async {
                    UIApplication.shared.registerForRemoteNotifications()
                }
            } else {
                print("Push notifications denied")
            }
        }
    }
}

Handle Device Token Registration

extension AppDelegate {
    
    func application(_ application: UIApplication, 
                    didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        
        // Convert token to string
        let tokenString = deviceToken.map { String(format: "%02.2hhx", $0) }.joined()
        print("Device token: \(tokenString)")
        
        // Register with social.plus SDK
        registerDeviceWithAmitySDK(token: tokenString)
    }
    
    func application(_ application: UIApplication, 
                    didFailToRegisterForRemoteNotificationsWithError error: Error) {
        print("Failed to register for remote notifications: \(error.localizedDescription)")
        
        // Handle registration failure
        handleRegistrationFailure(error: error)
    }
    
    private func registerDeviceWithAmitySDK(token: String) {
        AmityManager.shared.client?.registerDevice(
            withToken: token,
            completion: { [weak self] success, error in
                if success {
                    print("Successfully registered device with Amity SDK")
                    self?.onDeviceRegistrationSuccess(token: token)
                } else {
                    print("Failed to register device: \(error?.localizedDescription ?? "Unknown error")")
                    self?.handleRegistrationFailure(error: error)
                }
            }
        )
    }
    
    private func onDeviceRegistrationSuccess(token: String) {
        // Store token locally for reference
        UserDefaults.standard.set(token, forKey: "device_token")
        
        // Update UI or perform additional setup
        NotificationCenter.default.post(
            name: .deviceTokenRegistered,
            object: nil,
            userInfo: ["token": token]
        )
    }
    
    private func handleRegistrationFailure(error: Error?) {
        // Implement retry logic
        DispatchQueue.main.asyncAfter(deadline: .now() + 30) {
            UIApplication.shared.registerForRemoteNotifications()
        }
    }
}

// MARK: - Notification Names
extension Notification.Name {
    static let deviceTokenRegistered = Notification.Name("deviceTokenRegistered")
}

Best Practices

Testing & Debugging

Debug Notification Status

class NotificationDebugger {
    
    static func printDebugInfo() {
        print("=== Notification Debug Info ===")
        
        // Check authorization status
        UNUserNotificationCenter.current().getNotificationSettings { settings in
            print("Authorization Status: \(settings.authorizationStatus)")
            print("Alert Setting: \(settings.alertSetting)")
            print("Badge Setting: \(settings.badgeSetting)")
            print("Sound Setting: \(settings.soundSetting)")
        }
        
        // Check device token
        if let token = UserDefaults.standard.string(forKey: "device_token") {
            print("Stored Device Token: \(token)")
        } else {
            print("No device token stored")
        }
        
        // Check badge count
        print("Current Badge Count: \(UIApplication.shared.applicationIconBadgeNumber)")
    }
    
    static func testLocalNotification() {
        let content = UNMutableNotificationContent()
        content.title = "Test Notification"
        content.body = "This is a test notification from your app"
        content.sound = .default
        content.badge = 1
        
        let request = UNNotificationRequest(
            identifier: UUID().uuidString,
            content: content,
            trigger: UNTimeIntervalNotificationTrigger(timeInterval: 2, repeats: false)
        )
        
        UNUserNotificationCenter.current().add(request) { error in
            if let error = error {
                print("Failed to schedule test notification: \(error)")
            } else {
                print("Test notification scheduled")
            }
        }
    }
}

Troubleshooting

Production Requirements: Push notifications only work with production builds distributed through App Store or TestFlight. Debug builds from Xcode cannot receive push notifications.