PushSharp & Http2

最近由於Apple Push Service憑證過期,重新申請憑證時,發現Apple Push Notification(簡稱APN)失效了。

於是在Apple Store下載一些APNs 測試App來測試推播,都無法接收到訊息。
後來在網路上找到一個C#測試工具-PushSharp GitHub,卻發現之前的憑證都可以正常執行。

但是使用後來申請的憑證,程式都執行不下去,且都沒接收到APN Server所傳來的Response。

有推撥出去的訊息如下:

原來Apple在2021/3/31時,在申請Push憑證時,已不再支援舊有的模式(the legacy interface),往後的Push憑證只支援 Http/2 的模式。

Apple Developer Forums

不過此作者也有支分支可支援Http/2 PushSharp http2 branch GitHub

不過在推送過程中,遇到以下問題。

1. Missing Topic

Apple Guide中提到,使用Http/2推送時,需要傳遞參數topic,也就是App Bundle ID

apns-topic: In general, the topic is your app’s bundle ID/app ID.

所以我們在ApnsHttp2Notification中填入Bundle ID

1
2
3
ApnsHttp2Notification notify = new ApnsHttp2Notification(deviceToken, JObject.Parse(msg));
notify.Topic = Configs.BundleID;

2. 無法指定Push Type

Apple Guide中提到可以使用
apns-push-type來設定。

但是PushSharp中找不到可以設定的地方,後來參考PushSharpHttpTwo的方式來增加此設定。

在以下檔案中增加相關程式

PushSharp.Apple\ApnsHttp2Notification.cs
1
2
3
4
5
/// <summary>
/// apns-push-type
/// </summary>
/// <value>background or alert</value>
public string PushType { get; set; }
PushSharp.Apple\ApnsHttp2Connection.cs
1
2
3
4
5
6
7
8
public async Task Send (ApnsHttp2Notification notification)
{
if ((string.IsNullOrEmpty(notification.PushType)))
{
notification.PushType = "background";
}
headers.Add("apns-push-type", notification.PushType);
}

接下來可以在主程式中使用PushType來設定接收方式。

Main.cs
1
2
3
4
ApnsHttp2Notification notify = new ApnsHttp2Notification(deviceid, JObject.Parse(msg));
notify.Topic = Configs.BundleID;
notify.Priority = ApnsPriority.Low;
notify.PushType = "alert";
作者

Nick Lin

發表於

2021-12-24

更新於

2023-01-18

許可協議


評論