Can apple push notifications send more parameters than alert and sound?
Of course. You can send custom parameters as payload with apple push notification. Like Kevin Ballard said the payload will looks like the above. But remember one thing always you are dealing with push notification, As per the apple constraints the push notifications, The maximum size allowed for a notification payload is 256 bytes; Apple Push Notification Service refuses any notification that exceeds this. So please take this too in your consideration when you are going to add more data to notification.
You are not allowed to put custom tags inside aps tag. Here's what documentations says about it:
Providers can specify custom payload values outside the Apple-reserved aps namespace. Custom values must use the JSON structured and primitive types: dictionary (object), array, string, number, and Boolean.
So in your case you should do something like:
{
"aps": {
"alert": "Hello Push",
"sound": "default"
},
"People": {
"Address": "Your address here",
"Name": "Your Name here",
"Number": "XXXXXXXXXX"
}
}
Therefore you can read your custom payload with looking for it's key in main JSON, rather than in "aps":
Yes. In the Push Notification Programming Guide section The Notification Payload it states
Providers can specify custom payload values outside the Apple-reserved aps namespace. Custom values must use the JSON structured and primitive types: dictionary (object), array, string, number, and Boolean. You should not include customer information as custom payload data. Instead, use it for such purposes as setting context (for the user interface) or internal metrics. For example, a custom payload value might be a conversation identifier for use by an instant-message client application or a timestamp identifying when the provider sent the notification. Any action associated with an alert message should not be destructive—for example, deleting data on the device.
So your payload might look like
{
"aps": {
"alert": "joetheman",
"sound": "default"
},
"message": "Some custom message for your app",
"id": 1234
}
Further down on that same page are a number of examples that demonstrate this.
Here's how I send my custom key/values based on this tutorial
func sendPushNotification(to token: String, title: String, body: String, messageId: String, fromUsername: String, fromUID: String, timeStamp: Double) {
let urlString = "https://fcm.googleapis.com/fcm/send"
let url = NSURL(string: urlString)!
// apple's k/v
var apsDict = [String: Any]()
apsDict.updateValue(title, forKey: "title")
apsDict.updateValue(body, forKey: "body")
apsDict.updateValue(1, forKey: "badge")
apsDict.updateValue("default", forKey: "sound")
// my custom k/v
var myCustomDataDict = [String: Any]()
myCustomDataDict.updateValue(messageId, forKey: "messageId")
myCustomDataDict.updateValue(fromUsername, forKey: "username")
myCustomDataDict.updateValue(fromUID, forKey: "uid")
myCustomDataDict.updateValue(timeStamp, forKey: "timeStamp")
// everything above to get posted
var paramDict = [String: Any]()
paramDict.updateValue(token, forKey: "to")
paramDict.updateValue(apsDict, forKey: "notification")
paramDict.updateValue(myCustomDataDict, forKey: "data")
let request = NSMutableURLRequest(url: url as URL)
request.httpMethod = "POST"
// ** add the paramDict here **
request.httpBody = try? JSONSerialization.data(withJSONObject: paramDict, options: [.prettyPrinted])
// send post ...
}
Here's how I read everything from the notification in AppDelegate. The method below is a delegate method in AppleDelegate. Use this tutorial for it
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void {
let userInfo = response.notification.request.content.userInfo
if let userInfo = userInfo as? [String: Any] {
readPushNotification(userInfo: userInfo)
}
completionHandler()
}
func readPushNotification(userInfo: [String: Any]) {
for (k,v) in userInfo {
print(k)
print(v)
}
// my custom k/v
if let messageId = userInfo["messageId"] as? String {
print(messageId)
}
if let fromUsername = userInfo["username"] as? String {
print(fromUsername)
}
if let fromUID = userInfo["uid"] as? String {
print(fromUID)
}
if let timeStamp = userInfo["timeStamp"] as? String {
print(timeStamp)
}
// apple's k/v
if let aps = userInfo["aps"] as? NSDictionary {
if let alert = aps["alert"] as? [String: Any] {
if let body = alert["body"] as? String {
print(body)
}
if let title = alert["title"] as? String {
print(title)
}
}
if let badge = aps["badge"] as? Int {
print(badge)
}
}
}