Search code examples
iosswiftpush-notificationpayload

How to get timestamp of pushnotification in Swift?


I am trying to get pushnotification data. I am able to get title and body but i am also trying to get timestamp.

I am getting data like

   func userNotificationCenter(_ center: UNUserNotificationCenter,
                                didReceive response: UNNotificationResponse,
                                withCompletionHandler completionHandler: @escaping () -> Void) {
        let userInfo = response.notification.request.content.userInfo
        // Print full message.

        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)")

        completionHandler()
    }

is there any way to get timestamp in pushnotification payload itself or via any other way?


Solution

  • You should add custom timestamp data into your Push Notification's payload.

    Normally the payload looks like this;

    {
        "aps":{
            "alert":{
                "title":"Hello",
                "body":"How are you?"
            },
            "badge":0,
            "sound":"default"
        }
    }
    

    If you want to add custom fields into the payload it should be look like ->

    {
        "aps":{
            "alert":{
                "title":"Hello",
                "body":"How are you?"
            },
            "badge":0,
            "sound":"default"
        },
        "timestamp": "1590736069"
    }
    

    The custom fields are has to locate outside to aps object.

    Please check this out -> Apple Documentation


    Then you need to parse it

    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    
        let userInfo = response.notification.request.content.userInfo
        // Print full message.
    
        guard
            let aps = userInfo["aps"] as? NSDictionary,
            let alert = aps["alert"] as? NSDictionary,
            let body = alert["body"] as? String,
            let title = alert["title"] as? String,
            let timestamp = userInfo["timestamp"] as? String
                else {
                    // handle any error here
                    return
                }
    
        print("Title: \(title) \nBody:\(body)")
        print("Timestamp is: \(timestamp)")
        completionHandler()
    }
    

    Note: Don't need to use AnyHashable to use string as a key. Use like userInfo["aps"]


    Getting current date's timestamp

    If you need to know notification received date you can simply get current Date's timestamp value like this ->

    // Declare this in `didReceive` method and now you know the timestamp of notification received date
    let timestamp = Date().timeIntervalSince1970