Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How posting One Signal notification's additional data and receiving that?

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])
like image 400
iamburak Avatar asked Jan 13 '17 12:01

iamburak


People also ask

How do I send a notification from one signal?

Install Your SDK Copy the alpha numeric code that OneSignal generates called Your App ID in the image below. On Thunkable, open the Push Notifications dialog and paste this ID into the Android App ID field. Click the Live Test button. Once the app is on your phone, go back to One Signal.

How can I get notification of data?

Open new activity on click of push notification. Display data coming from push notification of new activity. If the application is closed so after click on notification the app get started.

What is data notification?

Works with iOS, Android and derivatives like Amazon. Background/Data notifications are "silent" meaning they do not display any message or play a sound when received by your app. They are designed to keep your app's data "up-to-date" by providing a way to "wake up" the app to refresh the data in the background.


2 Answers

You set the data field as a key in the dictionary passed to OneSignal.postNotification like the following.

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

Then you need to get ready your keys from additionalData from the payload in the handleNotificationAction function.

if let additionalData = payload?.additionalData {
   let postID: String? = additionalData["postID"]
}
like image 166
jkasten Avatar answered Oct 26 '22 23:10

jkasten


Example from iOS in objC to send additional data...

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

And to receive 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 it helps someone :)

like image 24
edhnb Avatar answered Oct 26 '22 23:10

edhnb