背景:
替换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升级后,脚本运行顺序出了问题