1

I am trying to make an app the launches the app or the incoming call UI when a silent notification is received.

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?

Coder_98
  • 117
  • 3
  • 10
  • You can only do very little upon receiving the notification, opening the app is definitely not on that list. If the user clicks your notification then the app will open, and at this point you can act upon the notification. Why do you need to open the app exactly? – Julian Minde Feb 14 '19 at 09:34
  • @JulianMinde Thanks, which approach should I take in order to open a UI when I receive a phone call? – Coder_98 Feb 14 '19 at 09:36
  • Opening your app on regular phone calls is not possible I think, but if you're creating an app that will receive VoIP calls you might find CallKit useful? https://developer.apple.com/documentation/callkit – Julian Minde Feb 14 '19 at 09:39
  • Yes, it is possible you can use default call UI when a call is received in the application wheater app is running or not. Using VOIP push. If you need an example please let me know. –  Feb 14 '19 at 10:01
  • @iOSTeam Can I please see an example. – Coder_98 Feb 14 '19 at 11:46
  • Hello @Coder_98 could you please help me on how to send voip push notification, I've tried stackoverflow.com/questions/58152554/… and stackoverflow.com/questions/37326450/… but found no luck. I am unable to send voip push notifications and can't seem to find a way. Your help would definitely appreciated. – Arjun Jan 27 '22 at 13:05

1 Answers1

4

First register for VOIP Notification in

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)

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. :)

  • In the background calls is it possible to force the app to open? – João Vitor Aug 31 '20 at 14:41
  • 1
    Hello @iOS Team could you please help me on how to send voip push notification, I've tried https://stackoverflow.com/questions/58152554/how-to-send-voip-push-notification-from-node-js-i-can-send-voip-push-from-curl and https://stackoverflow.com/questions/37326450/does-firebase-cloud-messaging-support-voip-pushkit-services but found no luck. I am unable to send voip push notifications and can't seem to find a way. Your help would definitely appreciated. – Arjun Jan 27 '22 at 13:05