I checked OneSignal documentation but I couldn't understand clearly as beginner how setting dictionary as a post notification's additional data (like postID, userID, type) in iOS Native SDK using Swift to decide and redirect when user interacted with notification.
For posting I'm doing only like that:
OneSignal.sendTag("username", value: "\(user)")
OneSignal.postNotification(["contents": ["en": "@\(user) added an additive to your '\(title)' experience: \"\(strLast)\""],
"include_player_ids": [postOwnerPlayerID],
For receiving:
OneSignal.initWithLaunchOptions(launchOptions, appId: "______", handleNotificationReceived: nil, handleNotificationAction: {
(result) in
// This block gets called when the user reacts to a notification received
let payload = result?.notification.payload
//Try to fetch the action selected
if let additionalData = payload?.additionalData {
print("payload")
print(additionalData)
}
// After deciding which action then I can redirect user..
let username: String? = UserDefaults.standard.string(forKey: KEY_UID)
if username != nil {
if let tabbarController = self.window!.rootViewController as? UITabBarController {
tabbarController.selectedViewController = tabbarController.viewControllers?[2]
// NotificationCenter.default.post(name: Foundation.Notification.Name(rawValue: "notificationsUp"), object: nil)
}
}
}, settings: [kOSSettingsKeyInFocusDisplayOption : OSNotificationDisplayType.none.rawValue])
You set the
data
field as a key in the dictionary passed toOneSignal.postNotification
like the following.Then you need to get ready your keys from
additionalData
from thepayload
in thehandleNotificationAction
function.If you're looking to do the same but in the Notification Service Extension, take a look at our updated documentation.
The Notification Service Extension is used for: - Badges - Influenced Opens with Firebase Analytics - Media Attachments - Action Buttons
Example from iOS in objC to send additional data...
And to receive the data...
Hope it helps someone :)
Thanks to @jkasten helped me in the right direction! helped me get rid of the
AnyHashable
warning I was getting.Swift 3 code (change PATH to the additionalData parameter you want to output):