Reputation: 476
I am currently attempting to send a notification through firebase to iOS which contains data which I would like to save to a variable.
My server is pushing a notification which my phone receives, and the notification contains topic_name, message_body, data_message, sound
. All of which work, although I am confused as to how I can access the data. Essentially, I want to save the sent data to a variable.
How would I go about doing this?
Upvotes: 1
Views: 2985
Reputation: 397
payload like this
{
"aps" : {
"alert" : "Notification with custom payload!",
"badge" : 1,
"content-available" : 1
},
"data" :{
"title" : "Game Request",
"body" : "Bob wants to play poker",
"action-loc-key" : "PLAY"
}
}
Read the payload data
@available(iOS 10, *)
extension AppDelegate : UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
let userInfo = notification.request.content.userInfo
if let aps = userInfo["aps"] as? NSDictionary
{
let alert = aps["alert"]as? NSString
let badge = aps["badge"] as? Int
}
completionHandler([.alert, .badge, .sound])
}
Upvotes: 5
Reputation: 3947
As long as you actually have the data you can pass it around in your code in many ways. One way is to post a notification with the data which the class/object responsible for using the data can listen to. The best solution to use sort of depends on how your app is structured...
Upvotes: 0