【问题标题】:iOS push notifications - badges won't showiOS 推送通知 - 徽章不会显示
【发布时间】:2018-09-08 12:05:46
【问题描述】:

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

这是我注册通知的地方(如您所见,已选择徽章):

    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" +
                    " }" +
                    "}";

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

【问题讨论】:

  • 在对象中尝试json有效负载aps对象与此jsonblob.com/920108b6-3358-11e8-80b3-2fe0f921f022
  • 这和我的格式一样,只是它也有声音......
  • 什么是后端语言? PHP ??
  • 语言是C#
  • 确保在通知中启用了该应用并确保允许使用徽章应用图标

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


【解决方案1】:

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

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

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

【讨论】:

  • 在哪里?我试着把它放在func userNotificationCenter(center: UNUserNotificationCenter, didReceiveNotificationResponse response: UNNotificationResponse, withCompletionHandler completionHandler: () -> Void) { 但这没用,因为它从来没有被调用过
  • 您是否在尝试查看徽章时打开了应用程序,或者您没有打开应用程序并在主屏幕上?
  • 我在主屏幕上
  • 如果您在主屏幕上,那么您必须验证 json 有效负载。检查此以验证有效负载 - developer.apple.com/library/content/documentation/…
  • “验证 json 有效负载”是什么意思?该链接与验证无关。
【解决方案2】:

在有效载荷中,“徽章”需要在警报之外。 示例:

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

【讨论】:

  • 已经处于警戒状态。我只是用一个字符串代替字典。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-14
相关资源
最近更新 更多