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

时间:2018-03-29 13:40:57

标签: ios iphone swift push-notification apple-push-notifications

如果我收到通知,徽章就不会显示。

我在这里注册通知(如您所见,已选择徽章):

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

这是我的有效载荷:

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

通知显示在顶部,但应用图标上没有任何徽章。

2 个答案:

答案 0 :(得分:0)

在有效载荷中,“徽章”需要处于警戒状态之外。 示例:

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

答案 1 :(得分:0)

您还需要设置应用程序徽章 -

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

当徽章编号为0时,此徽章将被隐藏。

相关问题