简体   繁体   中英

How can an app determine if its being launched from a terminated state due to Voip Push?

[NOTE: This question is about VoiP pushes, not about ordinary pushes] [NOTE2: The question is NOT about how to set up an app to receive Voip Pushes, read it properly what it is actually asking].

If the app is terminated and a Voip push arrives then within didFinishLaunchingWithOptions the options a nil. (This can be contrasted against if an app is launched when the user taps on a local notification for example).

How does an app know its being launched due to the arrival of a VoIP push, as opposed to a push arriving while the app is already in the background?

You need to set a delegate to the PKPushRegistry object you have created and specify it as a VOIP type:

let voipRegistry = PKPushRegistry(queue: nil)
voipRegistry.delegate = myPushDelegate
voipRegistry.desiredPushTypes = [PKPushType.voIP]

Your delegate will have to conform to the PKPushRegistryDelegate protocol.

Then, implement the pushRegistry(_:didReceiveIncomingPushWith:for:completion:) protocol function ( reference ) - this function will be called upon receiving a push (even if at the app was terminated earlier).

the function description:

Notifies the delegate that a remote push has been received.

the specified push type. Call the completion handler as soon as you have finished processing the payload.This method is invoked when a push notification has been received for

please note thet the function: pushRegistry(_:didReceiveIncomingPushWith:for:) has been deprecated so don't be confused with the one I mentioned above.

More information about the PKPushRegistryDelegate protocol: here

About the actual question, there is a work around for that - the idea is to create create a flag that will be destroyed when app will terminate but as soon as it wakes up it status will change - if the app woke up from background the flag status will be saved but if the app woke up after it was terminated the flag will hold the default value.

In the push's delegate class:

import PushKit
class PushDelegate: PKPushRegistryDelegate  {

    var flag = false

      func pushRegistry(_ registry: PKPushRegistry, didReceiveIncomingPushWith payload: PKPushPayload, forType type: PKPushType) {
            if flag {
                print("the app woke up from background")
            } else {
                print("the app was terminated")
                flag = true
            }
        }
}

In addition, in order to detect if the user launched the app (this is the only case that the app will be in active state) use the code below:

let state = UIApplication.shared.applicationState

    if state == .background {

        // background
    }
    else if state == .active {

        // foreground
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM