简体   繁体   English

当我在iOS应用上收到静默推送通知时,如何启动应用或来电UI?

[英]How do I launch an App or the Incoming Call UI when I receive a silent push notification on an iOS app?

I am trying to make an app the launches the app or the incoming call UI when a silent notification is received. 我正在尝试让一个应用程序在收到无提示通知时启动该应用程序或来电UI。

Currently I have the notifications working and I am able to send a notification and print out a log when I receive the notification 目前,我正在处理通知,并且能够在收到通知时发送通知并打印出日志

This function handels the receiving of notifications: 此函数可以接收通知:

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void){
    print("Enitre message \(userInfo)")

    let state: UIApplication.State = application.applicationState
    switch state {
    case UIApplication.State.active:
        print("State: App is active")
    case UIApplication.State.inactive:
        print("State: App is inactive")
    case UIApplication.State.background:
        print("State: App is running in the background")
    default:
        print("State: Unknown")
    }
    completionHandler(UIBackgroundFetchResult.newData)
}

Is it possible to open the app or incoming call UI in this function? 是否可以通过此功能打开应用程序或来电界面?

First register for VOIP Notification in 首先在中注册VOIP通知

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        requestPushKit()
        return true
}

Import the call kit 导入通话工具

import CallKit

Register Push kit 注册推包

fileprivate func requestPushKit() {
        let voipRegistry = PKPushRegistry(queue: DispatchQueue.main)
        voipRegistry.delegate = self
        voipRegistry.desiredPushTypes = [.voIP]
}

Delegates of VOIP(Push Kit) VOIP(推包)代表

extension AppDelegate: PKPushRegistryDelegate {

    func pushRegistry( registry: PKPushRegistry, didUpdate pushCredentials: PKPushCredentials, for type: PKPushType) {

        if type == PKPushType.voIP {
            let tokenParts = pushCredentials.token.map { data -> String in
                return String(format: "%02.2hhx", data)
            }

            let tokenString = tokenParts.joined()

            print(tokenString)

        }
    }

    func pushRegistry( registry: PKPushRegistry, didInvalidatePushTokenFor type: PKPushType) {

    }

  func pushRegistry( registry: PKPushRegistry, didReceiveIncomingPushWith payload: PKPushPayload, for type:PKPushType, completion: @escaping () -> Void) {

        if type == PKPushType.voIP {
            self.incomingCall()
        }
    }

    func pushRegistry( registry: PKPushRegistry, didReceiveIncomingPushWith payload: PKPushPayload, for type: PKPushType) {
        if type == PKPushType.voIP {
            self.incomingCall()
        }
    }

}

Call incomingcall Method 呼叫来电方法

fileprivate func defaultConfig() -> CXProviderConfiguration{
        let config = CXProviderConfiguration(localizedName: "My App")
        config.includesCallsInRecents = true
        config.supportsVideo = true
        config.maximumCallGroups = 5
        config.maximumCallsPerCallGroup = 10
//        config.iconTemplateImageData = UIImagePNGRepresentation(UIImage(named: "pizza")!)
//        config.ringtoneSound = "ringtone.caf" 

        return config
    }


func incomingCall(){
        let provider = CXProvider(configuration: defaultConfig())
        provider.setDelegate(self, queue: nil)
        let update = CXCallUpdate()
        update.remoteHandle = CXHandle(type: .generic, value: "Pete Za")
        update.hasVideo = true
        provider.reportNewIncomingCall(with: UUID(), update: update, completion: { error in })
    }

And Call Kit Delegates Methods 和呼叫工具包代表方法

extension AppDelegate : CXProviderDelegate {

    func providerDidReset( provider: CXProvider) {

    }

    func providerDidBegin( provider: CXProvider) {

    }

    func provider( provider: CXProvider, perform action: CXAnswerCallAction) {
        action.fulfill()
    }

    func provider( provider: CXProvider, perform action: CXEndCallAction) {
        action.fulfill()
    }

    func provider( provider: CXProvider, perform action: CXStartCallAction) {

    }

    func provider( provider: CXProvider, perform action: CXSetHeldCallAction) {

    }

    func provider( provider: CXProvider, timedOutPerforming action: CXAction) {

    }

    func provider( provider: CXProvider, perform action: CXPlayDTMFCallAction) {

    }

    func provider( provider: CXProvider, perform action: CXSetGroupCallAction) {

    }

    func provider( provider: CXProvider, perform action: CXSetMutedCallAction) {

    }

//    func provider(_ provider: CXProvider, execute transaction: CXTransaction) -> Bool {
//
//    }
}

Still if you require anything so please let us know. 不过,如果您需要任何内容​​,请告诉我们。 :) :)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我收到静默推送通知时,iOS 会唤醒我的应用程序吗?(当应用程序未处于运行状态时) - Will iOS awake my app when i receive silent push notification?(When app is not in running state) 当应用程序处于终止状态时,iOS静默推送通知不会收到 - iOS Silent Push Notification does not receive when app in killed state 使用Incoming OneSignal推送通知时,如何在应用程序位于前台时阻止警报? - How do I prevent Alert when App is on Foreground with Incoming OneSignal Push Notification? 如何在收到远程推送通知 (Swift) 时自动启动 IOS 应用程序? - How do I auto launch IOS app on received Remote Push Notification (Swift)? 如何从另一个应用程序接收到一个应用程序的推送通知? - How do I receive a push notification to one app from another? 如何在iOS中杀死应用程序时打开应用程序并显示推送通知 - How do I open the app and show push notification when app is killed in ios 当app在后台时,使用React Native进行无提示iOS推送通知 - Silent iOS Push Notification with React Native when app is in background 当应用程序在 ios 13 中的静默推送通知中终止时运行代码 - Run code when app is terminated on silent push notification in ios 13 静默推送通知在前台 iOS 应用程序上不起作用 - Silent push notification not working on foreground iOS app 使用Sinch iOS SDK在后台运行应用程序时无法处理来电推送通知 - Can't handle incoming call push notification when app is in background with Sinch iOS SDK
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM