0

I am new to push notification and I managed to get the push notification working. And now whenever a push notification comes and if I press it, the app will open and goes a page.

This is my push notification message

{"to":"eHPnnkSDzWk:APA91bHs...","data":{"message":"test notification","type":"5"}}

and these are the types

Gallery ( Type = 1 )
Messenger ( Type = 4 )
Events ( Type = 5 )
News ( Type = 6 )

This is currently my didReceiveRemoteNotification function

    func application(_ application: UIApplication, didReceiveRemoteNotification 
userInfo: [AnyHashable: Any],fetchCompletionHandler completionHandler: @escaping 
(UIBackgroundFetchResult) -> Void) {

        if let messageID = userInfo[gcmMessageIDKey] {
            print("Message ID: \(messageID)")
        }
        print(userInfo)

        completionHandler(UIBackgroundFetchResult.newData)
    }

Here is my question , how can I fetch this TYPE from the push notification and then open a specific page on the app when user clicks on the push notification?

(sorry if this sounds silly, but this is my first time trying to do this, I tried looking around but couldn't find any proper answer)

5
  • 4
    Possible duplicate of how can i get key's value from dictionary in Swift?
    – Kamran
    Oct 2, 2019 at 6:15
  • You are asking 2 questions 1. can I fetch this TYPE? 2. open a specific page on the app? You said you know how to get value from a dictionary means you should be able to get value from userInfo dictionary.
    – Kamran
    Oct 2, 2019 at 6:32
  • You can ask but it should not be ambiguous as your second question. We don't know what you want to open? Duplicate questions should not be posted. You can always try this first how to get push notification value swift
    – Kamran
    Oct 2, 2019 at 6:45
  • 1
    I agree with @Kamran here, the solution is simply to find out about the dictionary's structure and read the value. To Zyfe3r: As a tip, I usually set a breakpoint where I can debug the dictionary contents and use lldb to learn about the dictionary's structure. Oct 2, 2019 at 12:44
  • 1
    As mentioned, there are several questions within this question and while they are related, it will be hard to address all of them in one answer. It's also a bit unclear what the specific issue is; working with a Dictionary? Getting the data from the notification? Using a viewController? The Firebase Getting Started Guide Receive messages in iOS covers part of that. There's no code in your question that attempts to do any of those things though - can you clarify what the specific issue is? Then we can take a look and try to help.
    – Jay
    Oct 2, 2019 at 17:43

2 Answers 2

1

just use below code it's working tested

    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {

        if let dict = userInfo as? [String:Any] {
            if let dataDict = dict["data"] as? [String:Any],let type = dataDict["type"] as? String{
                print(type)
            }
        }
    }
1
  • 1
    For clarity, even though this is accepted, this answer does not fully answer the question and then open a specific page on the app when user clicks on the push notification?. This answer shows how to read the value from a key in a dictionary which is a duplicate of how can i get key's value from dictionary in Swift?. It's generally a good idea to mark as duplicate instead of re-posting the same info. I would have liked to see the full question addressed as that would have been more helpful to me
    – Jay
    Oct 3, 2019 at 14:04
0

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {

    let userInfo = notification.request.content.userInfo

    guard
        let aps = userInfo[AnyHashable("aps")] as? NSDictionary,
        let alert = aps["alert"] as? NSDictionary,
        let body = alert["body"] as? String,
        let title = alert["title"] as? String
        else {
            // handle any error here
            return
        }

    print("Title: \(title) \nBody:\(body)")

    
    
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.