附件数据格式

如需添加“附近通知”附件,最简单的方法是使用 Google 信标信息中心。或者,您也可以使用 Proximity Beacon API 和下文所述的附件数据格式。

“附近通知”功能的附件必须使用 com.google.nearby 命名空间,以及由两个字母的语言代码和可选的 -debug 后缀组成的类型。

附件应采用 JSON 格式。例如:

    {
      "title": "Example",
      "url": "https://www.example.com"
    }

您可以使用 JSON 格式实现更加具体的定位,如下所示:

    {
      "title": "Example",
      "url": "https://www.example.com",
      "targeting":[
        {
          "startDate": "2017-01-01",
          "endDate": "2017-01-31",
          "startTimeOfDay": "9:00",
          "endTimeOfDay": "17:00",
          "anyOfDaysOfWeek": [1, 2, 3, 4, 5, 6, 7],
          "anyOfAppInstallStates": ["INSTALLED", "NOT_INSTALLED"]
        }
      ]
    }

其中:

  • title - 内容的标题。title 的长度应少于 40 个字符,且必须少于 50 个字符。理想情况下,这应该为用户提供号召性用语。例如 Order with your phone, skip the lineSet up your thermostatLearn more about sea otters
  • url - 应用、网站或服务的网址。
  • 目标 - 可选规则,用于根据设备上下文限制通知的可见性。

网址格式

“附近通知”支持三种网址格式:

网页网址

此类网址就是普通网址。收到网址后,系统会提示用户在默认浏览器中打开网址。无需特殊应用配置。网址必须采用 HTTPS 格式,并采用常规网址格式:

  https://www.example.com

如果您的网址没有触发通知,最可能的原因如下:

  • 使用 HTTP 代替 HTTPS
  • 禁止链接到应用商店(如 play.google.com)。网页应独立成文,并直接在着陆页上提供实用信息或操作。

应用 intent

应用 intent 网址用于在应用中触发 intent。收到应用 intent 网址后,关联的应用会响应网址中包含的参数,前提是存在相应的应用 intent 过滤器。如果用户未安装该应用,系统会将其转到 Play 商店以安装应用。用户安装应用后,即可启动应用,并继续使用开发者指定的功能。应用 intent 网址的格式如下:

  intent://host/path#Intent;scheme=yourscheme;package=com.yourapp.ui;end;

如需详细了解如何设置 intent 网址的格式,请参阅通过 Chrome 设置 Android intent。 请注意,系统不会传递 intent extra。

您还可以通过以下方法构建网址:创建 intent,然后使用 intent.toUri(Intent.URI_INTENT_SCHEME),如下所示:

    Intent intent = new Intent()
        .setData(new Uri.Builder()
            .scheme("yourscheme")
            .authority("host")
            .appendPath("path")
            .build())
        .setPackage("com.yourapp.ui");
    Log.i(TAG, "Use this intent url: " + intent.toUri(Intent.URI_INTENT_SCHEME));

自由格式应用 intent

此选项应用于与架构、路径和软件包名称格式不匹配的应用 intent。只有当您确定 intent 网址的格式正确时,才使用此选项。

通过将 intent 添加 S.browser_回退_url 参数,可在未安装应用时,将用户引导至指定的网址,而不是 Play 商店:

intent://host/path#Intent;scheme=yourscheme;package=com.yourapp.ui; \
  S.browser_fallback_url=http%3A%2F%2Fm.yoursite.com%2Fpath%2F%;end;

内容相关定位

规则

“附近通知”支持四种定位规则:

日期

dateStartdateEnd 用于指定附件可见的日期范围(采用 ISO 8601 格式)。以下示例显示了 2017 年 1 月期间的通知:

    {
      "title": "January 2017",
      "url": "https://www.example.com",
      "targeting":[
        {
          "startDate": "2017-01-01",
          "endDate": "2017-01-31"
        }
      ]
    }

时段

“timeOfDayStart”和“timeOfDayEnd”采用 ISO 8601 格式来指定附件可见的每日时间范围。以下示例显示了每天上午 9 点到下午 5 点之间的通知:

    {
      "title": "Work time",
      "url": "https://www.example.com",
      "targeting":[
        {
          "startTimeOfDay": "9:00",
          "endTimeOfDay": "17:00"
        }
      ]
    }

周几

“anyOfDaysOfWeek”用于指定在星期几显示附件。格式为 ISO 8601,范围为 1(星期一)至 7(星期日)。 以下示例显示了星期六和星期日的通知:

    {
      "title": "Weekends",
      "url": "https://www.example.com",
      "targeting":[
        {
          "anyOfDaysOfWeek": [6, 7]
        }
      ]
    }

应用安装状态

“anyOfAppInstallStates”用于根据应用安装状态设置附件可见性。它仅适用于应用 intent 网址。以下示例显示了未安装应用时的通知。

    {
      "title": "App not installed",
      "url": "intent://host/path#Intent;package=com.example",
      "targeting":[
        {
          "anyOfAppInstallStates": ["NOT_INSTALLED"]
        }
      ]
    }

规则组合

每个附件可以有多个定位规则。来自同一定位对象的规则以 AND 关系连接在一起。以下示例显示了星期六和星期日上午 9 点到下午 5 点的通知。

    {
      "title": "Weekend and work time",
      "url": "https://www.example.com",
      "targeting":[
        {
          "startTimeOfDay": "9:00",
          "endTimeOfDay": "17:00"
          "anyOfDaysOfWeek": [6, 7]
        }
      ]
    }

不同定位对象的规则以 OR 相连。以下示例显示了星期一到星期五每天上午 9 点到下午 5 点的通知,以及星期六和星期日的全天通知。

    {
      "title": "Weekend or work time",
      "url": "https://www.example.com",
      "targeting":[
        {
          "anyOfDaysOfWeek": [6, 7]
        },
        {
          "startTimeOfDay": "9:00",
          "endTimeOfDay": "17:00"
        }
      ]
    }

向您的应用添加 intent 过滤器

应用必须配置为处理给定网址的架构、主机和路径。 为此,您必须在 AndroidManifest.xml 中添加元素,以声明与架构、主机和路径匹配的 <intent-filter>,并使用类别过滤器将其标记为可浏览,如下所示:

  <intent-filter>
    <action android:name="android.intent.action.VIEW"/>
     <!-- both categories below are required -->
     <category android:name="android.intent.category.BROWSABLE"/>
     <category android:name="android.intent.category.DEFAULT"/>
    <data android:host="host"
          android:pathPrefix="/path"
          android:scheme="yourscheme"/>
  </intent-filter>

如需了解详情,请参阅处理应用链接