0

I m build ios application using swift 5. I config the app to receive push message using firebase.

Now I m able to see push message, but I m not able to show only the information that I need on push message.

This is the code in my appdelegate

extension AppDelegate: UNUserNotificationCenterDelegate {
    
    func userNotificationCenter(_ center: UNUserNotificationCenter,
                                willPresent notification: UNNotification,
      withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
      
        let userInfo = notification.request.content.userInfo
        print("willPresent \(userInfo)")

        Messaging.messaging().appDidReceiveMessage(userInfo)
      
        /*if let info = userInfo as? Dictionary<String, AnyObject> {
            let title = userInfo["google.c.a.c_l"] as! String
            hanleNotification(info: info, strStatus: title, strFrom: "Front")
        }*/
      completionHandler([[.alert, .sound]])
    }

    func userNotificationCenter(_ center: UNUserNotificationCenter,
                                didReceive response: UNNotificationResponse,
                                withCompletionHandler completionHandler: @escaping () -> Void) {
        let userInfo = response.notification.request.content.userInfo
        print("Did Recceive \(userInfo)")
        if let info = userInfo as? Dictionary<String, AnyObject> {
            //let alert1 = info["aps"]!["alert"] as! Dictionary<String, AnyObject>
            let title = userInfo["aps"] as! JSON
            hanleNotification(info: info, strStatus: "titolo", strFrom: "Back")
        }
        //Messaging.messaging().appDidReceiveMessage(userInfo)
      completionHandler()
    }
    


    //MARK:GoViewCotroller
    func hanleNotification(info:Dictionary<String,AnyObject>,strStatus:String,strFrom:String) {
        
        let visibleVC = UIApplication.topViewController()
        if strStatus == "join_live_session" || strStatus == "exist_session" || strStatus == "join_request" {
        }
        
    }

THe method print this message on my xcode console:

willPresent [AnyHashable("gcm.notification.vibrate"): 1, AnyHashable("aps"): {
    alert =     {
        body = "{\"result\":\"successful\",\"msg\":\"speranza4\",\"date\":\"2021-10-07 06:29:50\",\"italian_msg\":\"Hai un nuovo messaggio\",\"user_id\":\"30\",\"chat_image\":null,\"order_id\":\"\",\"key\":\"You have a new message\",\"status\":\"chat\",\"username\":\"Michele\"}";
        title = ArrivaArriva;
    };
    sound = 1;
}

I need to use italian_message as body of message.

bircastri
  • 2,169
  • 13
  • 50
  • 119
  • the `body` is a JSON object. You need to parse it. See: https://stackoverflow.com/questions/24013410/how-to-parse-a-json-file-in-swift – timbre timbre Oct 07 '21 at 17:01
  • Ok I m able to parse my JSON but I don't know in which method of my code I need to parse it and setting the correct information – bircastri Oct 07 '21 at 17:23
  • Depends how you want to present it: as a popup notification? as a view? depending on that, you need to either create Alert (https://stackoverflow.com/questions/24022479/how-would-i-create-a-uialertview-in-swift) or some view... and display information fropm JSON you want to display – timbre timbre Oct 07 '21 at 17:28
  • No I want to present it as a normal push notification message as mail message, as whatsup message – bircastri Oct 07 '21 at 17:29
  • So I need to intercept this pus message before ios show at the user in homescreen it and display the correct message. – bircastri Oct 07 '21 at 17:49
  • In tthat case you need to upon receiving a remote notification to push local notification with the data you want. Local notifications are explained here: https://developer.apple.com/documentation/usernotifications/scheduling_a_notification_locally_from_your_app The receival part you already have – timbre timbre Oct 07 '21 at 17:55
  • Thanks for your answer. so if I want to display the text "Hello how are you?", i must send from my push server, a message with this text "Hello, how are you?". But if I want when the user tap on push message, open a specific View, by passing also input parameter, what can I do? – bircastri Oct 08 '21 at 07:11
  • Yes, you can send the message in format that won't need additional parsing on client - that's an optimal way. Navigation to specific view can be done in several ways. Most simple way is to use `launchOptions ` in `AppDelegate`'s `didFinishLaunchingWithOptions`. Here's the explanation: https://stackoverflow.com/questions/20771312/push-notification-didfinishlaunchingwithoptions – timbre timbre Oct 08 '21 at 12:52
  • Try going through https://www.raywenderlich.com/11395893-push-notifications-tutorial-getting-started - it explains lots of things you are asking about – timbre timbre Oct 08 '21 at 12:57

0 Answers0