我正在尝试复制当呼叫者开始呼叫关闭WhatsApp的用户时,WhatsApp如何向被呼叫者的设备发出来电信号。根据锁定屏幕,呼叫接收者的设备似乎以大约1秒的间隔重复接收推送通知,上面写着"来自用户名的呼叫"。但最值得注意的是,这些通知并没有堆积起来。似乎每个关于来电的通知都被下一个这样的通知所取代。当呼叫者挂断电话时,被呼叫者端的最后一个来电通知将被"未接来电"通知取代。
如何通过这种方式实现推送通知的替换/删除?
您可以使用"apns collapse id"。它将用相同的id 替换通知内容
https://developer.apple.com/documentation/usernotifications/unnotificationrequest/1649634-identifier
https://medium.com/the-guardian-mobile-innovation-lab/how-to-replace-the-content-of-an-ios-notification-2d8d93766446
WhatsApp使用静默通知来触发本地通知的显示。本地通知可以被应用程序替换。这是我最后一次对他们的工艺进行逆向设计。他们现在可能使用Push Kit消息,因为他们是一个VoIP应用程序。
Whatsapp、skype或任何其他与VOIP相关的应用程序都使用推送套件。
使用有效负载中的可用内容=1使其成为静默推送通知。
即使你的应用程序处于终止状态,静默推送通知也会在后台调用你的应用,因此它将允许你安排本地通知。
-
一旦你有了来电时间表的有效载荷本地通知
-
一旦您获得未接来电的有效载荷,取消来电本地通知并安排未接来电本地通知
-
注意-传入或未接来电本地通知对象始终保持在NSUserDefault中,以确保即使重新启动设备也可以取消。
-
您可以保存在localnotification.userInfo 中的负载相关详细信息
Pushkit代码
import UIKit
import PushKit
class AppDelegate: UIResponder, UIApplicationDelegate,PKPushRegistryDelegate{
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let types: UIRemoteNotificationType = [.Alert, .Badge, .Sound]
application.registerForRemoteNotificationTypes(types)
self. PushKitRegistration()
return true
}
//MARK: - PushKitRegistration
func PushKitRegistration()
{
let mainQueue = dispatch_get_main_queue()
// Create a push registry object
if #available(iOS 8.0, *) {
let voipRegistry: PKPushRegistry = PKPushRegistry(queue: mainQueue)
// Set the registry's delegate to self
voipRegistry.delegate = self
// Set the push type to VoIP
voipRegistry.desiredPushTypes = [PKPushTypeVoIP]
} else {
// Fallback on earlier versions
}
}
@available(iOS 8.0, *)
func pushRegistry(registry: PKPushRegistry!, didUpdatePushCredentials credentials: PKPushCredentials!, forType type: String!) {
// Register VoIP push token (a property of PKPushCredentials) with server
let hexString : String = UnsafeBufferPointer<UInt8>(start: UnsafePointer(credentials.token.bytes),
count: credentials.token.length).map { String(format: "%02x", $0) }.joinWithSeparator("")
print(hexString)
}
@available(iOS 8.0, *)
func pushRegistry(registry: PKPushRegistry!, didReceiveIncomingPushWithPayload payload: PKPushPayload!, forType type: String!) {
// Process the received push
// As per payload schedule local notification / cancel local notification
}
}
一种简易有效载荷
{
"aps": {
"content-available": 1,
"screen": "IncomingCall",
"alertTitle": "Mr ...",
"alertBody": "Call from ...",
"category": "INCOMINGCALL_CATEGORY",
"data": "Any specific data you want to pass"
}
}