TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

Badge notification permanently set to 100. Please help

Forums > Swift

Hello Everyone,

First off, I am sorry if this is the wrong section to post my development question. I am new to the forums. I have a weird issue with my app that I am creating. I am making a reminders app and while testing out notifications (alerts, badge, sound, etc) I set the badge number = 100 because I wasn’t noticing any changes to the badge. Once I saw that it set to 100 I removed the code and added this code:

if let badge = badge { var currentBadgeCount = UIApplication.shared.applicationIconBadgeNumber currentBadgeCount += badge notificationContent.badge = NSNumber(integerLiteral: currentBadgeCount) }

but for some reason, regardless of any notifications/reminders being inside my app the badge number always reflects 100. I’ve included some screenshots to show my app delegate code.

What did I do wrong?

Edit: I have no idea how to upload a photo. Here is the app delegate code:

//
//  AppDelegate.swift
//  MyToDoList
//
//  Created by Bill Helmick on 9/23/20.
//

import UIKit
import UserNotifications
import UserNotificationsUI

@main
class AppDelegate: UIResponder, UIApplicationDelegate {

    private func requestNotificationAuth(application: UIApplication) {

        let center = UNUserNotificationCenter.current()
        let options: UNAuthorizationOptions = [.alert, .badge, .sound]

        center.requestAuthorization(options: options) { granted, error in
            if let error = error {
                print(error.localizedDescription)
            }
        }

    }

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        requestNotificationAuth(application: application)
        return true
    }

    // MARK: UISceneSession Lifecycle

    func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
        // Called when a new scene session is being created.
        // Use this method to select a configuration to create the new scene with.
        return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
    }

    func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
        // Called when the user discards a scene session.
        // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
        // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
    }

    //NOT WORKING.  Need to clear the badge number once you enter the app

    func applicationDidBecomeActive(_ application: UIApplication) {
        UIApplication.shared.applicationIconBadgeNumber = 0
    }

    func applicationWillEnterForeground(_ application: UIApplication) {
        UIApplication.shared.applicationIconBadgeNumber = 0
    }

}

2      

Bump. Does anyone have any ideas?

2      

Hacking with Swift is sponsored by Blaze.

SPONSORED Still waiting on your CI build? Speed it up ~3x with Blaze - change one line, pay less, keep your existing GitHub workflows. First 25 HWS readers to use code HACKING at checkout get 50% off the first year. Try it now for free!

Reserve your spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.