Swift Read Userinfo of Remote Notification

Swift read userInfo of remote notification

The root level item of the userInfo dictionary is "aps", not "alert".

Try the following:

if let aps = userInfo["aps"] as? NSDictionary {
if let alert = aps["alert"] as? NSDictionary {
if let message = alert["message"] as? NSString {
//Do stuff
}
} else if let alert = aps["alert"] as? NSString {
//Do stuff
}
}

See Push Notification Documentation

How to get userInfo when user click in notification when app is closed?

From iOS 10 onwards, the method you mention should be called whether app is opening from background or inactive state. Perhaps you are not accessing the userInfo correctly. The example below works for me.

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {

let userInfo = response.notification.request.content.userInfo
if let yourData = userInfo["yourKey"] as? String {
// Handle your data here, pass it to a view controller etc.
}
}

Edit: as per the edit to the question, the notification centre delegate must be set in the didFinishLaunching() method, or the method above will not get called.

UNUserNotificationCenter.current().delegate = self

How to read custom data from a notification in IOS swift

You can try

let userInfo = response.notification.request.content.userInfo
print(userInfo["url"])

How to get userInfo when user click in notification when app is closed?

From iOS 10 onwards, the method you mention should be called whether app is opening from background or inactive state. Perhaps you are not accessing the userInfo correctly. The example below works for me.

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {

let userInfo = response.notification.request.content.userInfo
if let yourData = userInfo["yourKey"] as? String {
// Handle your data here, pass it to a view controller etc.
}
}

Edit: as per the edit to the question, the notification centre delegate must be set in the didFinishLaunching() method, or the method above will not get called.

UNUserNotificationCenter.current().delegate = self

How to read title or body of a push notification in swift?

You can use the following code to extract both title and body from userInfo dictionary:

let aps = userInfo["aps"] as? [String: Any]
let alert = aps?["alert"] as? [String: String]
let title = alert?["title"]
let body = alert?["body"]
print(title ?? "nil")
print(body ?? "nil")

Update: If you want to use Codable to map all the possible fields, it's slightly more difficult.

You can create a model like this:

struct Payload: Decodable {
let aps: APS
let acme1: String?
let acme2: [String]?
}

struct APS: Decodable {
let alert: Alert
let badge: Int?
}

struct Alert: Decodable {
let title: String
let body: String?
let action: String?
}

Then, you have to convert the userInfo dictionary back to Data, using JSONSerialization and finally decode the data using JSONDecoder:

let decoder = JSONDecoder()

do {
let data = try JSONSerialization.data(withJSONObject: userInfo)
let payload = try decoder.decode(Payload.self, from: data)
print(payload.aps.alert.title)
print(payload.aps.alert.body ?? "nil")
} catch {
print(error)
}

Why is userInfo not present when user clicks notification?

Figured out why!

I was trying to get userInfo in applicationDidFinishLaunching, but the right way to get userInfo is using userNotificationCenter (both if app is running or not).

func userNotificationCenter(
_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void
) {
DispatchQueue.main.async {
let alert = NSAlert.init()
alert.messageText = String(describing: response.notification.request.content.userInfo)
alert.runModal()
}
}

How to fetch data inside a push notification?

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


Related Topics



Leave a reply



Submit