1

我正在使用 Onesignar 的 rest api 向 android 设备发送通知,我想像这样向用户发送附加数据

{"user_level","10"}

我正在使用标签,但我无法使用函数检索此标签

 OneSignal.getTags(new OneSignal.GetTagsHandler() {
                    @Override
                    public void tagsAvailable(JSONObject tags) {
                        Log.d("debug_Tag", tags.toString());
                    }
                });

它总是返回空对象。我如何从通知中检索标签或使用 Onesignar 将附加数据设置为通知?

4

1 回答 1

1

如果您想通过通知传递数据,您需要data在 OneSignal创建通知REST API POST 调用上设置该字段。

{
  "app_id": "5eb5a37e-b458-11e3-ac11-000c2940e62c",
  "included_segments": ["All"],
  "data": {"user_level","10"},
  "contents": {"en": "English Message"}
}

或 OneSignal 仪表板上“新消息”页面上选项下的“附加数据”字段。

然后,您可以调用setNotificationOpenedHandler应用程序的代码以将NotificationOpenedHandlerwill fire 添加到您的应用程序中notificationOpened

  public void onCreate() {
      super.onCreate();

      OneSignal.startInit(this)
        .setNotificationOpenedHandler(new ExampleNotificationOpenedHandler())
        .init();
   }

  // This fires when a notification is opened by tapping on it or one is received while the app is running.
  private class ExampleNotificationOpenedHandler implements NotificationOpenedHandler {
    @Override
    public void notificationOpened(String message, JSONObject additionalData, boolean isActive) {
      try {
        if (additionalData != null) {
          Log.d("OneSignalExample", "Full additionalData:\n" + additionalData.toString());
        }
      } catch (Throwable t) {
        t.printStackTrace();
      }
    }

additionalData将包含您在data创建通知时在字段上设置的内容。

OneSignal.getTags仅获取您在用户上设置的标签值OneSiganal.sendTags

于 2016-06-09T21:47:19.610 回答