[英]How to set badge count in iOS 10 for pushnotification?
I want to set badge count on app icon when notification recieved.Below method is called when notification is recieved.But the badge count does not set. 我想在收到通知时在应用程序图标上设置徽章计数。收到通知时调用以下方法。但是徽章计数未设置。
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
var userData = userInfo as NSDictionary? as? [AnyHashable: Any] ?? [:]
var aps = userData["aps"] as! NSDictionary
var badge = aps["badge"]
print("data is \(badge!)")
UIApplication.shared.applicationIconBadgeNumber = badge! as! Int
if #available(iOS 10.0, *) {
let content = UNMutableNotificationContent()
content.badge = 10
} else {
// Fallback on earlier versions
}
// your badge count
}
Actually in iOS 10 these two methods are called: 实际上,在iOS 10中,这两种方法称为:
If the app is in the foreground: 如果应用程序在前台:
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
If the app is in the background: 如果应用程序在后台:
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void)
You need to get the content
of the request
of the notification
您需要获取notification
request
的content
foreground method 前台方法
let content = notification.request.content
background method 背景方法
let content = response.notification.request.content
The badge number is in the property badge
of content
徽章编号在content
的属性badge
中
let badgeNumber = content.badge as! Int
I was missing the authorization for badge.I added badge in code 我缺少徽章的授权。我在代码中添加了徽章
if #available(iOS 10.0, *) {
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.alert, .sound,.badge]) { (granted, error) in
// actions based on whether notifications were authorized or not
}
application.registerForRemoteNotifications()
}
I added this in didFinishLaunchWithOptions
我在didFinishLaunchWithOptions
添加了
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.