How to send One Signal messages to receive and receive additional data?

I checked the OneSignal documentation, but I could not understand, as a beginner, how to set the dictionary as an additional message notification (e.g. postID, userID, type) in the iOS Native SDK using Swift to make a decision and redirect when the user interacts with the notification.

For publication, I do this:

OneSignal.sendTag("username", value: "\(user)") OneSignal.postNotification(["contents": ["en": "@\(user) added an additive to your '\(title)' experience: \"\(strLast)\""], "include_player_ids": [postOwnerPlayerID], 

For getting:

  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]) 
+9
source share
4 answers

You set the data field as the key in the dictionary passed to OneSignal.postNotification , as shown below.

 OneSignal.postNotification(["contents": ["en": "Test Message"], "include_player_ids": ["3009e210-3166-11e5-bc1b-db44eb02b120"], "data": ["postID": "id"]]) 

Then you need to prepare the keys for additionalData from payload in the handleNotificationAction function.

 if let additionalData = payload?.additionalData { let postID: String? = additionalData["postID"] } 
+10
source

An example from iOS to objC to send additional data ...

 [OneSignal postNotification:@{@"contents":@{@"en":text}, @"include_player_ids":oneSignalIds, @"data":@{@"key": @"value"}, }]; 

And to get the data ...

  [OneSignal initWithLaunchOptions:launchOptions appId:ONESIGNAL_APPID handleNotificationReceived:^(OSNotification *notification) { if (notification.payload.additionalData) { NSDictionary* additionalData = notification.payload.additionalData; if (additionalData[@"key"]){ NSLog(@"Received Data - %@", additionalData[@"key"]); } } } handleNotificationAction:nil settings:@{kOSSettingsKeyInAppAlerts:@YES}]; 

Hope this helps someone :)

+4
source

Thanks @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 optional data parameter you want to output):

 let PATH = notification!.payload.additionalData["PATH"] print("PATH: ",PATH as Any) 
0
source

If you want to do the same, but in the Notification Service extension, check out our updated documentation .

The notification service extension is used for: - icons - discoveries influenced by Firebase Analytics - multimedia attachments - action buttons

0
source

Source: https://habr.com/ru/post/1014055/


All Articles