What is payload data in iOS?
This is a detail of how Swift implements the Any
type. Given that Swift can't know beforehand what you are putting into e.g. front
, it needs to store a pointer to that object (in payload_data_0
) as well as a pointer to metadata about the stored object's type (in instance_type
). As far as I know, payload_data_1
and payload_data_2
are there as an optimization so that Swift can store up to 24-byte large structs in place rather than having to put them in a wrapper object that needs to be stored in a separate heap location.
So, to answer your question: This is neither a bug in Swift, nor an error on your side. Your data gets stored and accessed in the right way. If you would prefer to inspect front
more easily while debugging, try
(lldb) po front
in the debugger. Alternatively, change front
's type to e.g. UIImage?
. If that is not possible, you could declare a protocol
protocol MemoryStoreable: class { }
and extend every type that needs to be stored in those fields like so:
extension UIImage: MemoryStoreable { }
and declare the variable as
var front: MemoryStoreable?
Note that MemoryStoreable
is restricted to classes, as otherwise a protocol witness would need to be stored (which is again implemented similarly to the Any
field you observed).
Another alternative would be, if you know you'll store e.g. only images and strings in front
, to use an enum:
enum MemoryStoreable {
case `string`(String)
case image(UIImage)
}
var front: MemoryStoreable?
That will still require at least 25 bytes of storage in your object (possibly 32 due to memory alignment constraints), though, as that's the size of the String
struct.
For more details, see e.g. https://mikeash.com/pyblog/friday-qa-2014-08-01-exploring-swift-memory-layout-part-ii.html.
Fetching data from Custom Payload Swift
Parse custom data in notification's userInfo
property instead of body. userInfo
is a dictionary of custom information associated with the notification.
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo
if let data = userInfo["data"] as? [String: Any] {
//
//Do your parsing here..
}
}
Issue in getting payload data from notification in iOS
The value for key data
is a JSON String, not a dictionary. You have to deserialize it separately
struct NotificationData : Decodable {
let message : String
let userImage : URL
let fromUserId : Int
let createdDate : String
private enum CodingKeys: String, CodingKey { case message = "Message", userImage = "UserImage", fromUserId = "FromUserId", createdDate = "CreatedDate" }
}
let delegate = UIApplication.shared.delegate as! AppDelegate
guard let userInfo = delegate.userInfo as? [String:Any],
let notificationString = userInfo["data"] as? String else { return }
let data = Data(notificationString.utf8)
do {
let result = try JSONDecoder().decode(NotificationData.self, from: data)
let message = result.message
let userImage = result.userImage
let fromUserId = result.fromUserId
let createdDate = result.createdDate
} catch { print(error) }
iOS Handle notifications and payload when app is in Background
There are two types of push notifications, alert notifications and background notifications. Alert notifications allow you to deliver visible alerts that can be interacted with in ways that your app can customize.Background notifications allow your application to fetch data from the background, upon receiving push notifications. Background notification should be used to keep your application up to date, even if the application isn't running. Also as of ios 10(and above)instead of using the didReceiveRemoteNotification method you can use didReceive method for handling the alert notifications.
Now coming back to your question in case of the alert notification the didReceive/didReceiveRemoteNotification method is called when the application is in the foreground or when the user taps on the application. Since, you want to update the database you can use the background notifications instead of the alert notification as it will automatically raise your application even when it is in background and will also call the didReceiveRemoteNotification:fetchCompletionHandler. while sending a background push notification make sure you :
- Edit Info.plist and check the "Enable Background Modes" and "Remote notifications" check boxes.
- Add "content-available":1 to your push notification payload, otherwise the app won't be woken if it's in the background
- The notification’s POST request should contain the apns-push-type header field with a value of background, and the apns-priority field with a value of 5.
For more info please refer :
https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/pushing_background_updates_to_your_app
Access custom push notification payload
You can take the dictionary with
let content = response.notification.request.content.userInfo
if let aps = content["aps"] as? [String: AnyObject] {
let myValue = aps["my_value"]
// DO STUFF, in myValue you will find your custom data
}
Related Topics
Your Credentials Do Not Allow Access to This Resource Twitter API Error
Problems with Cropping a Uiimage in Swift
Storing a Variable When the App Is First Installed
Uitableviewcell Initwithstyle:Uitableviewcellstylesubtitle Is Not Working
How to Remove Single Object in Array from Multiple Matching Object
Animating Button Allowuserinteraction Not Working
Swift App Crashes on Real Device But Works on Simulator
iOS App Crashes on Phone But Works Fine on Simulator
Implementing State Restoration in iOS 14
How to Design an Uiscrollview in Storyboard
Cannot Access Appdelegate While Testing Xcode Project
Mysterious "Cryptographic Verification Failure" Error on MACos Sierra, Xcode 8
How to Authorize Twitter with Swifter
Firebase: Provided Bucket Does Not Match the Storage Bucket of the Current Instance in Swift
Swift: Triggering Tableviewcell to Lead to a Link in a Uiwebview in Another Viewcontroller
How to Get the Opposite Value of a Bool in Swift
Error in Facebook Login, iOS 9, Swift
How to Deal with Dynamic Sections and Rows in iOS Uitableview