简体   繁体   English

iOS推送通知-徽章不会显示

[英]iOS push notifications - badges won't show

If I receive the notification, the badge doesn't show. 如果我收到通知,则徽章不会显示。

Here's where I register for notifications (as you can see, badge is selected): 我在这里注册通知(如您所见,已选择徽章):

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

                if granted {
                    print("granted")

                    DispatchQueue.main.async {
                        UIApplication.shared.registerForRemoteNotifications()
                        UNUserNotificationCenter.current().delegate = self
                    }
                } else {
                    print("not granted")
                }
            }
        }
    } else {
        let settings: UIUserNotificationSettings = UIUserNotificationSettings(types: [UIUserNotificationType.badge, UIUserNotificationType.sound, UIUserNotificationType.alert], categories: nil)
        UIApplication.shared.registerForRemoteNotifications()
        UIApplication.shared.registerUserNotificationSettings(settings)
    }

Here's my payload: 这是我的有效载荷:

                notificationJsonPayload =
                    "{\"aps\" : " +
                    " {" +
                    " \"alert\": \"$(Message)\"," +
                    " \"badge\": 1" +
                    " }" +
                    "}";

The notification appears at the top but there isn't any badge on the app icon. 通知显示在顶部,但应用程序图标上没有任何徽章。

In the payload the "badge" needs to be outside alert. 在有效载荷中,“徽章”需要处于外部警报状态。 Example: 例:

    {"aps": {
       "alert": {
         "title": "title",
         "body": "body"
       },
       "sound": "default",
        "badge": 8
     }
}

Also you need to set application badge - 另外,您需要设置应用程序徽章-

UIApplication.shared.applicationIconBadgeNumber = "your badge number from response "

This badge will be hidden when badge number is 0. 徽章编号为0时,此徽章将被隐藏。

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

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