0

I've found this strange behavior on ipad running ipados 14 (I've in both simulator and physical device installed 14.5, with 14.5.1 in the real one).

I need to show an icon badge with a number based on work made within the app, so there is no external push notification service involved in this.

I've set up a ViewModel that keep track of the number to show in the notification badge:

import Combine
import Foundation

class CounterViewModel: ObservableObject {
    @Published private(set) var notificationCount = 42

    func up() {
        self.notificationCount += 1
    }

    func down() {
        self.notificationCount -= 1
    }
}

To publish this number on the notification badge on the app icon I'm using the .onAppear method of the contentView

struct ContentView: View {
    @EnvironmentObject var counterViewModel: CounterViewModel
    @State private var counterCancellable: AnyCancellable?

    var body: some View {
        Text("Hello World!")
        .onAppear {
            UNUserNotificationCenter.current().requestAuthorization(options: .badge) { _, error in
                if error == nil {
                    self.counterCancellable = self.counterViewModel.$notificationCount.sink { count in
                        DispatchQueue.main.async {
                            UIApplication.shared.applicationIconBadgeNumber = count
                        }
                    }
                }
            }
        }
    }
}

Finally, the EnvironmentObject is set up on the app

struct IBreviaryApp: App {
    @ObservedObject private var counterViewModel = CounterViewModel()

    var body: some Scene {
        WindowGroup {
            ContentView()       
                .environmentObject(self.counterViewModel)               
        }
    }
}

so I can share on all the view involved in this count mechanism.

The problem: on the first run of the app, and only on the first one, after accepting the alert about authorising the notifications, when the app is put on background the badge within the icon is shown, but suddenly disappear. In the dock does not even show. If I click the app (both on dock or on the "desktop") the badge appear in the animation that open the app. If I scroll between the pages of installed applications and return to the one where my app is installed, the badge now is fixed. If I open another app, so the dock show this new app in the recent used app list, the badge appear here also (and stays this time).

Restarting the ipad or reinstalling the app fixes the problem, but itself remain for the first time you run the app.

EDIT: repo here: https://github.com/artecoop/badgebug To actually make the problem arise, I needed to change the count number from another function.

EDIT 2: Here's a video: https://youtu.be/tPWYRm5xFXI As you may already see on the repo, to mimic another interaction, I've added a 5 second delay and then set 42 in the counter.

Valerio
  • 3,297
  • 3
  • 27
  • 44
  • I have built a small sample program with exactly your sample code and can't reproduce the problem with it. I start the app and then go to the homescreen. It displays "42" as a badge and the badge does not disappear. – D. Mika Jun 14 '21 at 15:07
  • @D.Mika I've managed to reproduce, I've edited the question with the github repo – Valerio Jun 14 '21 at 15:33
  • Even with the code from the GitHub repo I'm unable to reproduce the problem. :-/ I'm currently trying on the iPod touch 14.5 Simulator. – D. Mika Jun 14 '21 at 15:43
  • @D.Mika as I stated on the question, the problem is on ipad with ipados 14 – Valerio Jun 14 '21 at 18:01
  • Still no problem. Maybe you can list the steps to reproduce the problem in detail? – D. Mika Jun 14 '21 at 18:47
  • I've added an hope-to-be explanatory video in the edited question – Valerio Jun 15 '21 at 13:06
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/233799/discussion-between-d-mika-and-valerio). – D. Mika Jun 15 '21 at 14:17
  • @Valerio Did you end up fixing the problem ? Your discussion continues in a chat and the chat is deleted. I also experiment a similar problem. – Léon Pelletier Mar 28 '22 at 13:40
  • @LéonPelletier in the end *it seems* the problem is related to apps installed via TestFlight that overwrite existing ones, either from production or previous tests. In production no one of my users experience this problem so far, FWIK – Valerio Mar 29 '22 at 07:59

0 Answers0