简体   繁体   English

徽章计数未显示在 iOS 应用程序中

[英]Badge Count is not shown in iOS App

The Badge count is not shown in my app for background and termination cases.对于背景和终止案例,徽章计数未显示在我的应用程序中。 I added this code in appdelegate didfinishlaunch我在 appdelegate didfinishlaunch 中添加了这段代码

 UIApplication.shared.applicationIconBadgeNumber = 0

        if #available(iOS 10, *)
        {
            UNUserNotificationCenter.current().delegate = self
            UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { (granted, error) in

                guard error == nil else
                {
                    return
                }
                if granted
                {
                    UIApplication.shared.registerForRemoteNotifications()
                }
            }
        }
        else
        {
            let settings : UIUserNotificationSettings = UIUserNotificationSettings(types: [.alert, .sound , .badge], categories: nil)
            UIApplication.shared.registerUserNotificationSettings(settings)
            UIApplication.shared.registerForRemoteNotifications()
        }



 UIApplication.shared.applicationIconBadgeNumber += 1
 print("Badge Count ===>   \( UIApplication.shared.applicationIconBadgeNumber)")

The print shows correct count.打印显示正确的计数。 But not shown in the app.但未显示在应用程序中。

Actually I dont need to update badge count for each notification.实际上我不需要为每个通知更新徽章计数。 For some share or like we need to update badge count.对于某些分享或类似内容,我们需要更新徽章数量。 So I created a method and update the count.所以我创建了一个方法并更新了计数。

func setBadgeCount()
        {
            if(self.currentAppState() == 1 || self.currentAppState() == 3)
            {
                sleep(2)
                UIApplication.shared.applicationIconBadgeNumber += 1
                print("Badge Count ===>   \( UIApplication.shared.applicationIconBadgeNumber)")
     }
        }

callthis method from从以下位置调用此方法

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void)
    {

setBadgeCount()
}

The app is not going to update the badge number with this method unless the app is actually open.除非应用程序实际打开,否则应用程序不会使用此方法更新徽章编号。 If you want to update the badge number upon receiving a notification then you have to set the badge number in the payload of notification.如果您想在收到通知时更新徽章编号,则必须在通知的有效负载中设置徽章编号。 Below is the sample of payload looks like.下面是有效负载的示例。 Put your desired number there and send it will work.将您想要的号码放在那里并发送即可。

{
    "aps": {
        "alert": "Test Push Notification",
        "sound": "yourSound.aiff",
        "Badge": "desiredNumber"
    }
}

You have written this code in didFinishLaunchingWithOptions which is called only once.您已经在didFinishLaunchingWithOptions中编写了这段代码,它只被调用一次。 So how would it update badge number.那么它将如何更新徽章编号。 Only add UIApplication.shared.applicationIconBadgeNumber = 0 in didFinishLaunchingWithOptions仅在didFinishLaunchingWithOptions中添加UIApplication.shared.applicationIconBadgeNumber = 0

Instead use,而是使用,

func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {

    application.applicationIconBadgeNumber = application.applicationIconBadgeNumber + 1; 
}

For push notification use,对于推送通知使用,

func application(application: UIApplication,didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {

    application.applicationIconBadgeNumber = application.applicationIconBadgeNumber + 1; 
}

As per your updated question, you are not getting proper badge count when app is entering background or terminating, while you are getting proper log.根据您更新的问题,当应用程序进入后台或终止时,您没有获得正确的徽章计数,而您正在获得正确的日志。 This is because to handle these cases we need to store badge counts somewhere.这是因为要处理这些情况,我们需要在某处存储徽章计数。 You can store it in UserDefaults and then fetch it accordingly.您可以将其存储在UserDefaults中,然后相应地获取它。

Just go to onesignal message settings and turn on badge count只需转到一个信号消息设置并打开徽章计数在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM