Firebase推送通知徽章计数在iOS中是否自动增加?

Firebase推送通知徽章计数在iOS中是否自动增加?,ios,swift,firebase,firebase-cloud-messaging,Ios,Swift,Firebase,Firebase Cloud Messaging,我收到来自firebase的远程推送通知。我正在尝试获取应用程序图标中的徽章计数。 在Firebase中,有以下选项提供徽章计数 至于现在,我没有设备来测试。我的问题是,如果我每次都把1作为徽章计数,它会在应用程序图标上自动增加徽章计数吗?如果否,那么如何使用firebase增加它 您想使用UserDefaults来跟踪收到的通知数量 1-首先将徽章计数注册为UserDefaults,值为0。我通常在viewDidLoad的登录屏幕上注册需要注册的任何其他值 var dict = [Strin

我收到来自firebase的远程推送通知。我正在尝试获取应用程序图标中的徽章计数。 在Firebase中,有以下选项提供徽章计数


至于现在,我没有设备来测试。我的问题是,如果我每次都把1作为徽章计数,它会在应用程序图标上自动增加徽章计数吗?如果否,那么如何使用firebase增加它

您想使用
UserDefaults
来跟踪收到的通知数量

1-首先将徽章计数注册为
UserDefaults
,值为
0
。我通常在viewDidLoad的登录屏幕上注册需要注册的任何其他值

var dict = [String: Any]()
dict.updateValue(0, forKey: "badgeCount")
UserDefaults.standard.register(defaults: dict)
2-当您的通知从Firebase发送到应用程序时,请更新
“badgeCount”
。以下是通知进入
AppDelegate
的示例:

// this is inside AppDelegate
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {

    // A. get the dict info from the notification
    let userInfo = notification.request.content.userInfo

    // B. safely unwrap it 
    guard let userInfoDict = userInfo as? [String: Any] else { return }

    // C. in this example a message notification came through. At this point I'm not doing anything with the message, I just want to make sure that it exists
    guard let _ = userInfoDict["message"] as? String else { return }

    // D. access the "badgeCount" from UserDefaults that you registered in step 1 above
    if var badgeCount = UserDefaults.standard.value(forKey: "badgeCount") as? Int {

        // E. increase the badgeCount by 1 since one notification came through
        badgeCount += 1

        // F. update UserDefaults with the updated badgeCount
        UserDefaults.standard.setValue(badgeCount, forKey: "badgeCount")

        // G. update the application with the current badgeCount so that it will appear on the app icon
        UIApplication.shared.applicationIconBadgeNumber = badgeCount
    }
}
3-无论您在哪个vc中使用何种逻辑在用户查看通知时进行确认,请将
UserDefaults'
badgeCount重置为零。同时将
UIApplication.shared.applicationBadgeNumber
设置为零

SomeVC:

func resetBadgeCount() {

    // A. reset userDefaults badge counter to 0
    UserDefaults.standard.setValue(0, forKey: "badgeCount")

    // B. reset this back to 0 too
    UIApplication.shared.applicationIconBadgeNumber = 0
}
有关以下答案的信息,请使用UserDefaults。非常容易实现。徽章计数是确定的,而不是累积的。这意味着如果你有一个徽章4,那么你发送另一个1,4将被替换为1,而不是添加。您必须自己存储徽章计数,并添加每个传入通知中包含的号码,以获得累积号码。