Onesignar从通知android获取标签?

时间:2016-06-09 15:21:17

标签: android push-notification onesignal

我使用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来回复通知中的标签或将附加数据设置为通知?

1 个答案:

答案 0 :(得分:1)

如果您想通过通知传递数据,则需要在OneSignal create notification REST API POST呼叫上设置data字段。

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

或"其他数据" "新消息"上的选项下的字段OneSignal仪表板上的页面。

然后,您可以在应用的代码中致电setNotificationOpenedHandler,将NotificationOpenedHandler添加到您的应用中,然后点击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设置的用户标记值。