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
}
}