我试图复制WhatsApp如何向主叫方设备发出有关来电的信号,当来电者开始呼叫关闭该WhatsApp的用户时。根据锁定屏幕,呼叫接收器的设备似乎是以大约1秒的间隔重复接收推送通知,说“"来自UserName"”。但最值得注意的是,通知不会堆积。似乎每个有关来电的通知都会被下一个此类通知所取代。当呼叫者放弃呼叫时,被呼叫者端的最后一个呼入呼叫通知被"未接呼叫"通知。
如何以这种方式实现推送通知的替换/删除?
答案 0 :(得分:2)
您可以使用“ apns-collapse-id”。它将用相同的ID替换Notification内容
https://developer.apple.com/documentation/usernotifications/unnotificationrequest/1649634-identifier
答案 1 :(得分:1)
WhatsApp使用静默通知来触发本地通知的显示。应用可以替换本地通知。这是我最后一次改变他们的过程。他们现在可能会使用Push Kit消息,因为它们是VoIP应用程序。
答案 2 :(得分:0)
Whatsapp,Skype或任何其他与VOIP相关的应用程序都使用推送工具包。
在有效负载中使用content-available = 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
}
}
各种pushkit有效载荷
{
"aps": {
"content-available": 1,
"screen": "IncomingCall",
"alertTitle": "Mr ...",
"alertBody": "Call from ...",
"category": "INCOMINGCALL_CATEGORY",
"data": "Any specific data you want to pass"
}
}