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 的模式。
不過此作者也有支分支可支援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 | ApnsHttp2Notification notify = new ApnsHttp2Notification(deviceToken, JObject.Parse(msg)); |
2. 無法指定Push Type
在Apple Guide中提到可以使用
apns-push-type來設定。
但是PushSharp中找不到可以設定的地方,後來參考PushSharpHttpTwo的方式來增加此設定。
在以下檔案中增加相關程式
1 | /// <summary> |
1 | public async Task Send (ApnsHttp2Notification notification) |
接下來可以在主程式中使用PushType來設定接收方式。
1 | ApnsHttp2Notification notify = new ApnsHttp2Notification(deviceid, JObject.Parse(msg)); |