背景:
替換iOS推送通知的默認左側圖標
實現原理:
1.客戶端實現互動通知擴展,并實現相關代理函數
2.推送消息到蘋果服務器的時候攜帶 "mutable-content": 1
的字段,然后端上的通知擴展程序就會被激活,然后代理函數回調用,下載圖片資源,替換通知圖片
官方例子
{
"aps" : {
"category" : "SECRET",
"mutable-content" : 1,
"alert" : {
"title" : "Secret Message!",
"body" : "(Encrypted)"
},
},
"ENCRYPTED_DATA" : "Salted__·??Qê$UDì_?ù∞èΩ^?%gq∞N?òQùw"
}
測試例子
{"aps":{"alert":"This is some fancy message.","badge":6,"sound": "default","mutable-content": 1},"avatar_url":"https://img.isupermeet.com/MTcyMjIzNjMxMTczMSM4NzIjanBn.jpg"}
注意:"mutable-content": 1 一定到對,否則代理函數無法調用(ps: 筆者做的時候參考了網上資料文檔,有些資料文檔里的
mutable-content
字段是錯誤的,如mutable_content
, 導致無法調試通)
注意:plist文件里的字段順序有嚴格的要求,
INStartCallIntent
在前,INSendMessageIntent
在后
其他問題:
1.默認創建出來的通知擴展,代碼文件是藍色的
image.png
需要將其轉換成group
文件: 選中藍色的文件夾,右鍵選擇 Covert to Group
完成轉換
image.png
2.編譯運行報錯:
ycle inside Runner; building could produce unreliable results.
Cycle details:
→ Target 'Runner' has copy command from '/Users/gg/Library/Developer/Xcode/DerivedData/Runner-bylfgjqadshquibfkfwdljlsxqkj/Build/Products/Release-iphoneos/spns.appex' to '/Users/gg/Library/Developer/Xcode/DerivedData/Runner-bylfgjqadshquibfkfwdljlsxqkj/Build/Products/Release-iphoneos/Runner.app/PlugIns/spns.appex'
○ That command depends on command in Target 'Runner': script phase “[CP] Copy Pods Resources”
○ That command depends on command in Target 'Runner': script phase “[CP] Embed Pods Frameworks”
○ That command depends on command in Target 'Runner': script phase “Thin Binary”
○ Target 'Runner' has process command with output '/Users/gg/Library/Developer/Xcode/DerivedData/Runner-bylfgjqadshquibfkfwdljlsxqkj/Build/Products/Release-iphoneos/Runner.app/Info.plist'
○ Target 'Runner' has copy command from '/Users/gg/Library/Developer/Xcode/DerivedData/Runner-bylfgjqadshquibfkfwdljlsxqkj/Build/Products/Release-iphoneos/spns.appex' to '/Users/gg/Library/Developer/Xcode/DerivedData/Runner-bylfgjqadshquibfkfwdljlsxqkj/Build/Products/Release-iphoneos/Runner.app/PlugIns/spns.appex'
解決參考:stackoverflow
檢查發現我的 Thin Binary
不在最下面,然后移動了最下面解決問題,最終效果如下:
image.png
猜測:Xcode升級后,腳本運行順序出了問題