適用於 iOS 13 的 Azure 通知中樞更新

Apple 最近對其公用推播服務進行了一些變更;變更大多與 iOS 13 和 Xcode 的版本一致。 本文說明這些變更對 Azure 通知中樞的影響。

APNS 推送承載變更

APNS 推送類型

Apple 現在要求開發人員透過 APNS API 中的新 apns-push-type 標頭,將通知識別為警示或背景通知。 根據 Apple 的文件:「此標頭的值必須精確地反映您通知承載的內容。 如果有不相符的情形,或是必要系統上缺少標頭,APNs 可能會傳回錯誤、延遲通知的傳遞或直接捨棄。」

開發人員現在必須在透過 Azure 通知中樞在傳送通知的應用程式中設定此標頭。 由於技術限制,客戶必須使用權杖型驗證進行 APNS 認證,並搭配包含此屬性的要求。 如果您要針對 APNS 認證使用憑證型驗證,則必須切換為使用權杖型驗證。

下列程式碼範例示範如何在透過 Azure 通知中樞傳送的通知要求中設定此標頭屬性。

範本通知 - .NET SDK

var hub = NotificationHubClient.CreateFromConnectionString(...);
var headers = new Dictionary<string, string> {{"apns-push-type", "alert"}};
var tempprop = new Dictionary<string, string> {{"message", "value"}};
var notification = new TemplateNotification(tempprop);
notification.Headers = headers;
await hub.SendNotificationAsync(notification);

原生通知 - .NET SDK

var hub = NotificationHubClient.CreateFromConnectionString(...);
var headers = new Dictionary<string, string> {{"apns-push-type", "alert"}};
var notification = new AppleNotification("notification text", headers);
await hub.SendNotificationAsync(notification);

直接 REST 呼叫

var request = new HttpRequestMessage(method, $"<resourceUri>?api-version=2017-04");
request.Headers.Add("Authorization", createToken(resourceUri, KEY_NAME, KEY_VALUE));
request.Headers.Add("ServiceBusNotification-Format", "apple");
request.Headers.Add("apns-push-type", "alert");

為了協助您進行此轉換,當 Azure 通知中樞偵測到未設定 apns-push-type 的通知時,服務會從通知要求推斷推送類型,並自動設定值。 請記住,您必須將 Azure 通知中樞設定為使用權杖型驗證以設定必要的標頭;如需詳細資訊,請參閱適用於 APNS 的權杖型 (HTTP/2) 驗證

APNS 優先順序

另一個次要變更,但需要變更傳送通知的後端應用程式,則要求針對背景通知,apns-priority 標頭現在必須設定為 5。 許多應用程式會將 apns-priority 標頭設定為 10 (表示立即傳遞),或未設定並取得預設值 (這也是 10)。

背景通知不再允許將此值設定為 10,且您必須為每個要求設定值。 如果遺漏此值,Apple 將不會傳遞背景通知。 例如:

var hub = NotificationHubClient.CreateFromConnectionString(...);
var headers = new Dictionary<string, string> {{"apns-push-type", "background"}, { "apns-priority", "5" }};
var notification = new AppleNotification("notification text", headers);
await hub.SendNotificationAsync(notification);

SDK 變更

多年來,iOS 開發人員使用傳送至推送權杖委派 deviceToken 資料的 description 屬性,來擷取後端應用程式用來將通知傳送至裝置的推送權杖。 使用 Xcode 11 時,該 description 屬性會變更為不同的格式。 開發人員用於此屬性的現有程式碼現在已中斷。 我們已更新 Azure 通知中樞 SDK 以配合這項變更,因此請將您的應用程式所使用的 SDK 更新為 2.0.4 版或更新版本的 Azure 通知中樞 iOS SDK