'I'd like to count badges from ios. You can't try everything about how to do it

This is a list of methods I used.

  1. Use firebase on Background Handler.
Future main() async {
  WidgetsFlutterBinding.ensureInitialized(); 
  await Firebase.initializeApp(); 
  FirebaseMessaging.onBackgroundMessage(onBackgroundHandler);
}

Future onBackgroundHandler(RemoteMessage message) async {
  await Firebase.initializeApp();
  FlutterAppBadger.updateBadgeCount(10);
  return Future.value();
}

it`s not working

  1. Code AppDelegate is coded directly.
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
    GeneratedPluginRegistrant.register(with: self)
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }
    override func applicationDidBecomeActive(_ application: UIApplication) {
        UIApplication.shared.applicationIconBadgeNumber = 0
        
    }
}

it`s not working

  1. Coding directly to the code added to Notification Service Extension.

@available(iOSApplicationExtension, unavailable)
class NotificationViewController: UIViewController, UNNotificationContentExtension {

    @IBOutlet var imageView: UIImageView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    @available(iOSApplicationExtension 10.0, *)
    func didReceive(_ notification: UNNotification) {
        let content = notification.request.content
        
       
        let count = UIApplication.shared.applicationIconBadgeNumber
        
        UIApplication.shared.applicationIconBadgeNumber = count + 1
        

        if let urlImageString = content.userInfo["urlImageString"] as? String {
            if let url = URL(string: urlImageString) {
                URLSession.downloadImage(atURL: url) { [weak self] (data, error) in
                    if let _ = error {
                        return
                    }
                    guard let data = data else {
                        return
                    }
                    DispatchQueue.main.async {
                        self?.imageView.image = UIImage(data: data)
                    }
                }
            }
        }
    }
    
}

it`s not working

  1. Coding directly to the code added to Notification controller extension.

@available(iOSApplicationExtension, unavailable)
class NotificationService: UNNotificationServiceExtension {

    var contentHandler: ((UNNotificationContent) -> Void)?
    var bestAttemptContent: UNMutableNotificationContent?

    override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
        self.contentHandler = contentHandler
        bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
        
        let count = UIApplication.shared.applicationIconBadgeNumber
        
        UIApplication.shared.applicationIconBadgeNumber = count + 1
        

https://pushpsenairekar.medium.com/increment-ios-apps-badge-count-in-using-5-simple-steps-3d80e77d45c8 referred to the blog.

I don't want to count badges directly on fcm. How can I increase the count of badges every time I get a notification?



Solution 1:[1]

I understand this might be frustrating. The issue is that you think you need to increase the badge number manually and programmatically and that's not the right way of doing that.

What you want is your notification payload to actually include an integer for its badge key stating to iOS as an operating system what badge integer to display for your application.

This documentation for Firebase notifications will clear things up for you. So instead of trying to do that manually, include the integer value of your badge for iOS inside the notification itself.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Vandad Nahavandipoor