共用方式為


使用 WNS 通知來實作跨裝置恢復(XDR)

這份針對第一方及第三方的逐步指南,詳細說明如何將應用程式連續性(resume)與 Windows 推播通知服務(WNS)原始通知整合。 它包含前置條件、相關公開文件的參考,以及用於向頻道 URI 提出 POST 請求的程式碼片段。

在 Windows 中恢復上線入職程序

簡歷是一項有限存取功能 (LAF)。 要取得此功能,您需要取得 Microsoft 的批准,才能在您的應用程式上啟用 Windows。

申請存取請寄電子郵件 wincrossdeviceapi@microsoft.com 提供以下資訊:

  • 請提供您的應用程式 WNS 註冊狀態,並提供您的應用程式「套件 SID」。
  • 描述你的使用者體驗。
  • 這是你應用程式的截圖,使用者正在執行可以在 Windows PC 上繼續執行的動作。

如果 Microsoft 批准你的申請,你會收到解鎖該功能的指示。 核准取決於你的溝通。

先決條件

在進行整合前,請確保以下任務已完成:

實作步驟

以下步驟概述利用 WNS 原始通知整合應用程式連續性的流程。

步驟 1 - 設定通道 URI

從你的 Windows 應用程式設定通道 URI 並傳送到你的應用程式伺服器:確保應用程式能夠請求並儲存通道 URI 以接收通知。 欲了解更多資訊,請參閱 如何請求、建立及儲存通知頻道

步驟二 - 啟動履歷情境

從您的行動應用程式呼叫所需的應用程式服務 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」標頭會指定你要發送的原始通知類型。 通常不需要包含這個標頭,但它有助於你將通知分類到不同用途,例如「Resume」。 此標頭可能包含的值包括 「wns/raw/resume」等類型, 表示應用程式從 Windows 繼續。

X-WNS-ResumeMetadata」標頭提供你要發送的履歷通知的元資料。 雖然不是強制要求,但透過新增標題或有效期限等資訊,提升通知效果。 你可以將中繼資料以 JSON/字典物件的形式傳遞(其他類型會拋出驗證例外),其中可以包含以下欄位:

  • 標題:通知的描述性標題(例如「繼續通話來自...」)。
  • 過期期:通知的壽命(以秒計)(例如「300」持續5分鐘)。
  • 類型:通知類型 - 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 Client、Postman 或 Fiddler 這類工具來檢查 HTTP 請求與回應。

這裡有一個使用 Visual Studio Code REST Client 擴充功能發送請求的範例。 X-WNS-RawNotificationTypeX-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)原生通知

Conclusion

透過遵循本指南中所述步驟,第一方及第三方都能成功為 WNS 通知新增自訂標頭。 確保你符合所有先決條件,並徹底測試以確保無縫整合。

如對實施有任何疑問或需要協助,請透過以下方式聯絡我們的團隊:

電子郵件: wincrossdeviceapi@microsoft.com

我們在這裡協助確保整合過程順利進行。