Reputation: 830
I am using Firebase to send remote push notification for an iOS app. Now I need to show only badge in app icon without alert when app is not running. Is it possible to implement something like that?
Thank you
Upvotes: 2
Views: 594
Reputation: 100523
Request UIUserNotificationTypeBadge
permission only when you set permissions in didFinishLaunchingWithOptions
Remove .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)
}
}
Upvotes: 1
Reputation: 1392
You should set the notification setting options to .badge
only when you request notifications authorization from user:
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")
}
Upvotes: 0