我正在使用Firebase为iOS应用发送远程推送通知。现在,我需要在应用未运行时仅在应用图标中显示徽章而不发出警报。 是否可以实现类似的东西?
谢谢
答案 0 :(得分:1)
仅在UIUserNotificationTypeBadge
didFinishLaunchingWithOptions
权限
删除.alert
func requestNotificationAuthorization(application: UIApplication) {
if #available(iOS 10.0, *) {
UNUserNotificationCenter.current().delegate = self
let authOptions: UNAuthorizationOptions = [.badge]
UNUserNotificationCenter.current().requestAuthorization(options: authOptions, completionHandler: {_, _ in })
} else {
let settings: UIUserNotificationSettings = UIUserNotificationSettings(types: [.badge], categories: nil)
application.registerUserNotificationSettings(settings)
}
}
答案 1 :(得分:0)
只有在您向用户请求通知授权时,才应将通知设置选项设置为.badge
:
let notificationCenter = UNUserNotificationCenter.current()
notificationCenter.requestAuthorization(options: [.badge], completionHandler: { [weak self] success, error in
guard success else {
guard error == nil else {
print("Error occured while requesting push notification permission. In \(#function)")
print("Error: \(error!.localizedDescription)")
return
}
print("User has denied permission to send push notifications")
return
}
print("User has granted permission to send push notifications")
}