如何发布One Signal通知的附加数据并接收该数据?

时间:2017-01-13 12:33:27

标签: ios swift push-notification onesignal

我检查了OneSignal文档,但我无法清楚地理解初学者如何在iOS Native SDK中使用Swift来设置字典作为发布通知的附加数据(如postID,userID,type),以便在用户与通知进行交互时进行决定和重定向。 / p>

对于发布我只是这样做:

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

接收:

 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])

4 个答案:

答案 0 :(得分:10)

您将# does `cat` read fd and print? $ cat file # does `cat` read from stdin and print? $ cat < file $ cat - < file # with heredoc or herestring, what methods `cat` command use to read from heredoc?stdin? $ cat << EOF heredoc> test heredoc> EOF test $ cat <<< "test" $ cat - << EOF heredoc> test heredoc> EOF test $ cat - <<< "test" # and I dont why these commands works? $ cat <(echo "test") $ cat - <<(echo "test") # why this command doesn't work? $ cat - <(echo "test") 字段设置为传递给data的字典中的键,如下所示。

OneSignal.postNotification

然后,您需要从OneSignal.postNotification(["contents": ["en": "Test Message"], "include_player_ids": ["3009e210-3166-11e5-bc1b-db44eb02b120"], "data": ["postID": "id"]]) 功能additionalData中的payload准备好您的密钥。

handleNotificationAction

答案 1 :(得分:4)

从objC中的iOS发送附加数据的示例...

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

并接收数据......

 [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}];

希望它可以帮助某人:)

答案 2 :(得分:0)

感谢@jkasten帮助我朝着正确的方向前进!帮助我摆脱了AnyHashable警告。

Swift 3代码(将PATH更改为您要输出的additionalData参数):

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

答案 3 :(得分:0)

如果您希望这样做,但是在Notification Service Extension中,请查看我们的updated documentation

Notification Service Extension用于: -徽章 -Firebase Analytics带来的影响 -媒体附件 -动作按钮