为第一方和第三方提供的本分步指南详细介绍了如何将应用程序连续性(恢复功能)与 Windows 推送通知服务(WNS)的原始通知进行集成。 它包括先决条件、对相关公共文档的引用,以及用于向通道 URI 发出 POST 请求的代码片段。
载入到 Windows 中的“恢复”
简历是受限访问功能(LAF)。 若要获取此功能的访问权限,需要从Microsoft获得批准才能在 Windows 上启用应用程序。
若要请求访问权限,请 wincrossdeviceapi@microsoft.com 发送电子邮件,其中包含以下信息:
- 请查看您的应用程序 WNS 注册状态,并提供应用程序的“包 SID”。
- 用户体验的说明。
- 应用程序的屏幕截图,显示用户正在执行可在其 Windows 电脑上继续的操作。
如果Microsoft批准你的请求,则会收到有关如何解锁该功能的说明。 审批基于您的沟通。
先决条件
在继续集成之前,请确保已完成以下任务:
- 向 Windows 推送通知服务(WNS)注册应用程序:必须将应用程序注册到 WNS 才能接收通知。 有关详细信息,请参阅 使用 Azure 通知中心将通知发送到通用 Windows 平台应用 。
- 获取访问凭据:从 Azure 门户获取包安全标识符(SID)和客户端密码。
- 配置通道 URI:确保应用可以请求和存储通知的通道 URI。 有关详细信息 ,请参阅“如何请求、创建和保存通知通道 ”。
- WNS 通知使用 XML 有效负载进行传输。 有关详细信息 ,请参阅应用通知内容 。 对于简历,我们将使用原始通知。 确保 Windows 应用程序支持与 wns/raw 通知关联的有效负载。 有关详细信息,请参阅 原始通知概述 。
实施步骤
以下步骤概述了使用 WNS 原始通知集成应用程序连续性的过程。
步骤 1 - 配置通道 URI
从 Windows 应用程序配置通道 URI 并将其发送到应用服务器:确保应用可以请求和存储通知的通道 URI。 有关详细信息,请参阅 “如何请求、创建和保存通知通道”。
步骤 2 - 启动恢复情景
从移动应用程序中,调用必要的应用程序服务 API 来启动基于 WNS 的恢复请求。
步骤 3 - 设置 HTTP 请求
当应用服务器从移动客户端接收 API 请求时,它会准备对 WNS 通道 URI 的 POST 请求。 请求必须包含所需的标头,例如 Authorization、Content-Type 和 X-WNS-Type。 若要添加新标头,请将其包含在请求标头配置中。
步骤 3.1 - 发送简历 POST 请求的示例
以下代码片段演示如何使用 Python 和 JavaScript 通过简历标头发送新的 WNS 通知。
以下 Python 代码片段演示如何使用应用程序连续性所需的标头发送 WNS 原始通知:
import requests
# Define the channel URI
channel_uri = "[URL]"
# Define the notification payload
payload = """
Sample Notification
This is a sample message
"""
# Define the headers
headers = {
"Content-Type": "application/octet-stream",
"X-WNS-Type": "wns/raw",
"Authorization": "Bearer YOUR_ACCESS_TOKEN",
"X-WNS-RawNotificationType": "wns/raw/resume",
"X-WNS-ResumeMetadata": {"title":"Continue call from…","expiry":"300", "type":"1"},
}
# Send the POST request
response = requests.post(channel_uri, data=payload, headers=headers)
# Print the response status
print(f"Response Status: {response.status_code}")
print(f"Response Body: {response.text}")
以下 JavaScript 代码片段演示如何使用应用程序连续性所需的标头发送 WNS 原始通知:
const axios = require('axios');
// Define the channel URI
const channelUri = "[URL]";
// Define the notification payload
const payload = `Sample Notification
This is a sample message`;
// Define the headers
const headers = {
"Content-Type": "application/octet-stream",
"X-WNS-Type": "wns/raw",
"Authorization": "Bearer YOUR_ACCESS_TOKEN",
"X-WNS-RawNotificationType": "wns/raw/resume",
"X-WNS-ResumeMetadata": JSON.stringify({
title: "Continue call from…",
expiry: "300",
type: "1"
})
};
// Send the POST request
axios.post(channelUri, payload, { headers })
.then(response => {
console.log(`Response Status: ${response.status}`);
console.log(`Response Body: ${response.data}`);
})
.catch(error => {
console.error(`Error Status: ${error.response?.status}`);
console.error(`Error Body: ${error.response?.data}`);
});
“X-WNS-RawNotificationType”标头指定要发送的原始通知的类型。 通常不需要包含此标头,但它可以帮助您将通知分类为不同的目的,例如“简历”。 此标头的可能值可能包括 “wns/raw/resume” 等类型,这些类型表示应用程序继续来自 Windows。
“X-WNS-ResumeMetadata”标头提供有关要发送的简历通知的元数据。 虽然并非严格要求,但通过添加标题或过期时间等信息,可以增强通知的效果。 将元数据作为 JSON/Dictionary 对象(任何其他类型引发验证异常)传递,并且可以包括如下字段:
- title:用于通知的描述性标题(例如,“继续来自...的通话”)。
- 有效期:通知的有效期(例如“300 秒”表示 5 分钟)。
- type:通知类型 - 1 代表新的简历请求,2 代表删除(如果未指定,则不执行任何操作)。
以下 JavaScript 代码片段演示如何发送带有必要标头的 WNS 原始通知来更新简历通知:
const axios = require('axios');
// Define the channel URI
const channelUri = "[URL]";
// Define the notification payload
const payload = `Sample Notification
This is a sample message`;
// Define the headers
const headers = {
"Content-Type": "application/octet-stream",
"X-WNS-Type": "wns/raw",
"Authorization": "Bearer YOUR_ACCESS_TOKEN",
"X-WNS-RawNotificationType": "wns/raw/resume",
"X-WNS-ResumeMetadata": JSON.stringify({
title: "Continue call from…",
expiry: "300",
type: "2" // 2-represents update type.
})
};
// Send the POST request
axios.post(channelUri, payload, { headers })
.then(response => {
console.log(`Response Status: ${response.status}`);
console.log(`Response Body: ${response.data}`);
})
.catch(error => {
console.error(`Error Status: ${error.response?.status}`);
console.error(`Error Body: ${error.response?.data}`);
});
以下 JavaScript 代码片段演示如何发送带有必要标头的 WNS 原始通知来删除简历通知:
const axios = require('axios');
// Define the channel URI
const channelUri = "[URL]";
// Define the notification payload
const payload = `Sample Notification
This is a sample message`;
// Define the headers
const headers = {
"Content-Type": "application/octet-stream",
"X-WNS-Type": "wns/raw",
"Authorization": "Bearer YOUR_ACCESS_TOKEN",
"X-WNS-RawNotificationType": "wns/raw/resume",
"X-WNS-ResumeMetadata": JSON.stringify({
title: "Continue call from…",
expiry: "300",
type: "3" // 3-represents delete type.
})
};
// Send the POST request
axios.post(channelUri, payload, { headers })
.then(response => {
console.log(`Response Status: ${response.status}`);
console.log(`Response Body: ${response.data}`);
})
.catch(error => {
console.error(`Error Status: ${error.response?.status}`);
console.error(`Error Body: ${error.response?.data}`);
});
这两个标头都灵活地定制原始通知,以满足应用程序的功能要求。
步骤 4 - 验证实现
确保应用程序成功注册通知,并将简历标头包含在 POST 请求中。 可以使用 vscode REST 客户端、Postman 或 Fiddler 等工具来检查 HTTP 请求和响应。
下面是使用 Visual Studio Code REST 客户端扩展发送请求的示例。
X-WNS-RawNotificationType和X-WNS-ResumeMetadata标头是应用程序连续性所需的:
POST {{channel_uri}}
Content-Type: application/octet-stream
X-WNS-Type: wns/raw
X-WNS-RequestForStatus: true
X-WNS-RawNotificationType: wns/raw/resume
X-WNS-ResumeMetadata: {"title": "Continue call from...", "expiry": "300", "type": "1"}
Authorization: Bearer {{bearer}}
[{"hello"}]
VS Code REST 客户端的响应以与现有通知响应相同的格式显示。 有关状态代码的详细信息,请参阅参考链接: 发送 Windows 推送通知服务 (WNS) 本机通知。
结论
按照本指南中所述的步骤作,第一方和第三方可以成功将自定义标头添加到 WNS 通知。 确保满足所有先决条件并彻底进行测试,以确保无缝集成。
有关实现的任何查询或帮助,请通过以下方式联系我们的团队。
电子邮件: wincrossdeviceapi@microsoft.com
我们来这里有助于确保一个流畅的集成过程。