I'm using FCM for cloud messaging. I want to add app badge when I receive a push notification from the server in background and foreground app states. What I'm missing? Main problem is adding/updating/removing app badge according to push notification, I can receive and handle push messages .I'm 3 days on this problem. Help me, please !? * Badge numbers changes according to inside contents, like if new email messages received to gmail app, badge number changes to unreaded message count in both background and foreground app states.
using XCode 9.2, swift 3.2, iOS 11.6
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure()
var fcmtoken: String = ""
if #available(iOS 10.0, *) {
// For iOS 10 display notification (sent via APNS)
UNUserNotificationCenter.current().delegate = self
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: authOptions,
completionHandler: {_, _ in })
} else {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
}
application.registerForRemoteNotifications()
if let token = Messaging.messaging().fcmToken {
fcmtoken = token
print("FCM token: \(fcmtoken)")
} else {
print("FCM token: \(fcmtoken) == no FCM token")
}
return true
}
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
print("Firebase registration token: \(fcmToken)")
Messaging.messaging().subscribe(toTopic: "all")
print("subscribed to all topic in didReceiveRegistrationToken")
// TODO: If necessary send token to application server.
// Note: This callback is fired at each app startup and whenever a new token is generated.
}
func application(_ application: UIApplication, didRegister notificationSettings: UIUserNotificationSettings) {
Messaging.messaging().subscribe(toTopic: "all")
print("subscribed to all topic in notificationSettings")
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
print("userInfo -- \(userInfo)")
}
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo
print("user info in didReceive response -- \(userInfo)")
}
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
print("called to foreground app")
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
Messaging.messaging().subscribe(toTopic: "all")
print("subscribed to all topic in didRegisterForRemoteNotificationsWithDeviceToken")
}