是否可以仅显示Firebase推送通知的徽章,在IOS(Swift或Obj-C)中没有警报?

时间:2018-03-22 12:54:52

标签: ios iphone firebase firebase-cloud-messaging

我正在使用Firebase为iOS应用发送远程推送通知。现在,我需要在应用未运行时仅在应用图标中显示徽章而不发出警报。 是否可以实现类似的东西?

谢谢

2 个答案:

答案 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")
}