【问题标题】:Show badge number when notification received收到通知时显示徽章编号
【发布时间】:2018-09-12 07:45:54
【问题描述】:

当通知到达应用程序时,徽章编号没有任何操作。我在 Capabilities 上设置了后台模式。

还有另一个问题。当应用程序处于后台模式时,此方法不会调用。

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    print("Push notification received2: \(userInfo)")
    completionHandler(UIBackgroundFetchResult.noData)
}

PS:UIApplication.shared.applicationIconBadgeNumber 没有答案。我知道。问题是即使应用程序收到推送通知,徽章编号也不会更新。

【问题讨论】:

标签: ios swift notifications push


【解决方案1】:

APNS 不支持徽章的增量操作。生成的每个推送通知都应该设置当前值。

所以我们需要在某个地方有一个服务/服务器来跟踪您的每个用户应该使用哪些徽章

例如,在您的有效负载中添加一个键badge,获取您的徽章计数并在示例后面增加计数。

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

    var currentBadgeNumber: Int = application.applicationIconBadgeNumber
    currentBadgeNumber += Int(userInfo["badge"] as? String ?? "0")!
    application.applicationIconBadgeNumber = currentBadgeNumber
    completionHandler(UIBackgroundFetchResult.noData)
}

例如有效载荷,请参阅 SO 中的 old answer。

注意:didReceiveRemoteNotification 从 iOS10+ 起已弃用,请参阅example

【讨论】:

  • 我现在使用 willPresent 方法。但是当应用程序在后台时它不会调用。
  • @tersintersi- 抱歉延迟回复,如果您的应用程序是后台,则将调用此方法。 func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-08-18
  • 2013-04-16
  • 2011-09-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-26
相关资源
最近更新 更多