Skip to content

App Badge

App badges display a count number on your application icon, providing users with visual feedback about unread notifications, pending tasks, or other relevant information when the app is not in focus.

Implementation

  1. Integrate with Your View Controller
    • Add the following method to MainViewController.swift to handle app badge updates:
swift
@objc func updateApplicationBadgeCount(_ params: [String: String]) {
    let badge: Int = Int(params["badge"] ?? "0") ?? 0
    let center = UNUserNotificationCenter.current()

    Task {
        let settings = await center.notificationSettings()

        switch settings.authorizationStatus {
        case .authorized:
            Task {
                try await center.setBadgeCount(badge)
            }
        case .notDetermined:
            // Request permission if not determined
            let granted = try? await center.requestAuthorization(options: [.badge])
            if granted == true {
                Task {
                    try await center.setBadgeCount(badge)
                }
            } else {
                print("User denied badge permission")
            }
        default:
            print("Notification authorization denied or restricted")
        }
    }
}
  1. Trigger the Share Sheet in JavaScript
    • To update the application badge count, call the native updateApplicationBadgeCount method:
javascript
enClose({
    nativeCall: 'updateApplicationBadgeCount',
    data: {
        badge: 5
    }
});

Clear Badge

javascript
enClose({
    nativeCall: 'updateApplicationBadgeCount',
    data: {
        badge: 0
    }
});

Important Notes

  • Badge permissions are part of the notification permissions. Users must grant notification permissions for badges to work.
  • Setting the badge count to 0 will clear the badge from your app icon.
  • Badge counts should be meaningful and not used excessively.
  • Badge counts only appear when the app is not in the foreground.
  • Some users may disable badge notifications in their device settings.