Search code examples
swiftpush-notificationfirebase-notifications

How to fix push notification title not showing correct string in swift


i've been facing some problem with FCM push notifications showing in my app. here is the Data that the FCM send me :

["roomId": 253539,
"type": ROOM_SEND_MESSAGE,
"aps": {
    alert = {
        "loc-args" = (
            "TEST STRING"
        );
        "loc-key" = "CHANNEL_MESSAGE_IMAGE";
    };
    "content-available" = 1;
    sound = default;
},"messageId": 15638864319517014,
"gcm.message_id": 1563886435277772]

here is the problem : whenever i receive a notification it is supposed to show me the parameters in "loc-args" but instead it shows me the value in "loc-key" check the image bellow the image for my problem and here is the code in my appDelegate

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

    if SMLangUtil.loadLanguage() == "fa" {
        IGGlobal.languageFileName = "localizationsFa"
    } else {
        IGGlobal.languageFileName = "localizationsEn"
    }
    let stringPath : String! = Bundle.main.path(forResource: IGGlobal.languageFileName, ofType: "json")
    MCLocalization.load(fromJSONFile: stringPath, defaultLanguage: SMLangUtil.loadLanguage())
    MCLocalization.sharedInstance().language = SMLangUtil.loadLanguage()

    if SMLangUtil.loadLanguage() == "fa" {
        UITableView.appearance().semanticContentAttribute = .forceRightToLeft
    } else {
        UITableView.appearance().semanticContentAttribute = .forceLeftToRight
    }

    SMUserManager.clearKeychainOnFirstRun()
    SMUserManager.loadFromKeychain()
    realmConfig()
    Fabric.with([Crashlytics.self])
    _ = IGDatabaseManager.shared
    _ = IGWebSocketManager.sharedManager
    _ = IGFactory.shared
    _ = IGCallEventListener.sharedManager // detect cellular call state

    UITabBar.appearance().tintColor = UIColor.white

    let tabBarItemApperance = UITabBarItem.appearance()
    tabBarItemApperance.setTitleTextAttributes(convertToOptionalNSAttributedStringKeyDictionary([convertFromNSAttributedStringKey(NSAttributedString.Key.foregroundColor):UIColor.red]), for: UIControl.State.normal)
    tabBarItemApperance.setTitleTextAttributes(convertToOptionalNSAttributedStringKeyDictionary([convertFromNSAttributedStringKey(NSAttributedString.Key.foregroundColor):UIColor.white]), for: UIControl.State.selected)

    UserDefaults.standard.setValue(false, forKey:"_UIConstraintBasedLayoutLogUnsatisfiable")

    pushNotification(application)
    detectBackground()
    IGGlobal.checkRealmFileSize()
    return true
}
  /******************* Notificaton Start *******************/

    func pushNotification(_ application: UIApplication){
        FirebaseApp.configure()
        Messaging.messaging().isAutoInitEnabled = true
        Messaging.messaging().delegate = self
        Messaging.messaging().shouldEstablishDirectChannel = true

        if #available(iOS 10.0, *) { // For iOS 10 display notification (sent via APNS)
            /**
             * execute following code in "IGRecentsTableViewController" and don't execute here,
             * for avoid from show permission alert in start of app when user not registered yet
             **/
            //UNUserNotificationCenter.current().delegate = self
            //let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound, .carPlay]
            //UNUserNotificationCenter.current().requestAuthorization(options: authOptions,completionHandler: {_, _ in })
        } else {
            let settings: UIUserNotificationSettings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
            application.registerUserNotificationSettings(settings)
        }

        application.registerForRemoteNotifications()
        self.voipRegistration()

    }

    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        voipRegistration()
    }
    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {

        print("||||||NOTIFICATION||||||||")
        print(userInfo)

        if let roomId = userInfo["roomId"] as? String {
            let unreadCount = IGRoom.updateUnreadCount(roomId: Int64(roomId)!)
            application.applicationIconBadgeNumber = unreadCount
        }
    }
    /******************* Notificaton End *******************/

Solution

  • for any one facing the same or similar problem i found the answer to my question :D

    becoz all notifications are being handled by the os it self all i had to do was to go deeper to find the problem all i had to do was to re create the localizable files and boom every thing goes in order :)