-1

I am receiving notifications in my app using FCM but i am unable to show badge count on my app icon since there seems to be missing variables in my notification. how can i add this?

Below is the error i am getting

  [AnyHashable("icon"): logo.png, AnyHashable("user_id"): 3, AnyHashable("google.c.sender.id"): 938224596608, AnyHashable("aps"): {
alert =     {
    body = "You have new message! Check in the app.";
    title = Tutil;
};
sound = default;
}, AnyHashable("google.c.a.e"): 1, AnyHashable("body"): You have new message! Check in the app., AnyHashable("user_photo"): images/3.png, AnyHashable("type"): message, AnyHashable("gcm.message_id"): 1604897386664267, AnyHashable("title"): Tutil, AnyHashable("user_name"): Mali, AnyHashable("lessons"): []] 

Below is my code

    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    let userInfo = notification.request.content.userInfo
    
    print("%@", userInfo)
    // Print message ID.
    let content = notification.request.content
    
    let badgeCount = content.badge as! Int //<<i am getting my error here as variables may not be available
    
    incrementBadgeNumberBy(badgeNumberIncrement: badgeCount)
    

 
    completionHandler([.alert,.badge,.sound])
}
James Z
  • 12,209
  • 10
  • 24
  • 44
Viva
  • 79
  • 9

1 Answers1

0

You can get badge like this

if let aps = userInfo["aps"] as? Dictionary<String, Any>, let badge = aps["badge"] as? Int {
   print(badge)
}
user12208004
  • 1,704
  • 3
  • 15
  • 37