【问题标题】:How do you replace one push notification with another on the receiving device?如何在接收设备上用另一个推送通知替换一个推送通知?
【发布时间】:2016-11-22 03:14:24
【问题描述】:

我正在尝试复制当呼叫者开始呼叫已关闭此 WhatsApp 的用户时,WhatsApp 如何向被呼叫者的设备发出有关来电的信号。根据锁定屏幕,来电者的设备似乎以大约 1 秒的间隔重复接收推送通知,说“来自用户名的呼叫”。但最值得注意的是,通知不会堆积。似乎每个关于来电的通知都被下一个此类通知所取代。并且当主叫挂断时,被叫端的最后一个来电通知被“未接来电”通知取代。

这种方式如何实现推送通知的替换/删除?

【问题讨论】:

标签: ios push-notification


【解决方案1】:
【解决方案2】:

WhatsApp 使用静默通知来触发本地通知的显示。本地通知可以被应用程序替换。这是我最后一次逆向设计他们的流程。他们现在可能使用 Push Kit 消息,因为它们是 VoIP 应用程序。

【讨论】:

  • 谢谢。如果应用程序被强制关闭,我如何运行任何代码来安排本地通知?
  • 如果您使用 APNS,则无法保证。 iOS 可能不会运行应用程序来处理它们。 WhatsApp 也有同样的问题(如果他们还没有迁移到 PK)。如果您使用 PK,iOS 将在后台启动您的应用程序来处理它们,因为 PK 消息始终是静默的。
【解决方案3】:

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"
    }
}

【讨论】:

  • 这并不能确保您会收到来电通知。使用 PushKit 和 VoIP 曾经是一种选择,但现在它迫使您使用 CallKit。顺便说一句,Whatsapp 有一个特殊的权利。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多