【问题标题】:How posting One Signal notification's additional data and receiving that?如何发布 Onesignal 通知附加数据并接收该数据?
【发布时间】:2017-05-28 20:01:19
【问题描述】:

我查看了 OneSignal 文档,但作为初学者,我无法清楚地理解如何在 iOS Native SDK 中将字典设置为发布通知的附加数据(如 postID、userID、类型),以便在用户与通知交互时决定和重定向。

为了发帖,我只是这样做:

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

【问题讨论】:

    标签: ios swift push-notification onesignal


    【解决方案1】:

    您将data 字段设置为传递给OneSignal.postNotification 的字典中的键,如下所示。

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

    然后你需要从handleNotificationAction函数中的payload准备好你的密钥。

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

    【讨论】:

    • 感谢您对我的帮助!
    【解决方案2】:

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

    希望它可以帮助某人:)

    【讨论】:

      【解决方案3】:

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

      Swift 3 代码(将 PATH 更改为要输出的附加数据参数):

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

      【讨论】:

        【解决方案4】:

        如果您希望在通知服务扩展中做同样的事情,请查看我们的updated documentation

        通知服务扩展用于: - 徽章 - 使用 Firebase Analytics 影响打开次数 - 媒体附件 - 操作按钮

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-11-15
          • 1970-01-01
          • 2018-09-03
          • 1970-01-01
          • 1970-01-01
          • 2017-02-23
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多