通过单击推送通知打开应用程序时调用哪个方法

which method is getting called when app is opened by push notification clicked

我刚刚在我的应用程序中实现了推送通知。 问题:当我的应用程序关闭并且我收到推送通知时以及当我单击它时。我想打电话给一个 api 所以我想知道我可以通过哪种方法来处理这个问题。 注意:主要是app处于关闭状态。

这是我的代码

if (application.applicationState == .Active)
    {
        print("Active")



    }
    else (application.applicationState == .Inactive)
    {
        print("InActive")
    }

但此代码仅在我的应用处于活动状态或 iactive 时有效。当我的应用程序完全关闭时,我该怎么做。

不幸的是,如果不是 运行,普通推送通知无法启动您的应用。然而,还有一个替代方案,称为 Apple PushKit,它似乎已经测试了几年。它旨在与 VOIP 应用程序一起使用,以取代 "always leave a socket open" 来处理来电,但重要的是,当您的设备收到 PushKit 通知时,它会启动您的应用程序。

我不知道 Apple 是否会允许使用 PushKit 的应用进入商店。

我用过它,(大约一年前),它确实有效,但有问题,有时只是停止接受传入的通知。

当您关闭应用程序并收到推送通知时,如果您单击它,则会调用 didFinishLaunchingWithOptions

在那里,您将获得一个特殊的密钥来检查用户是否已通过单击推送通知打开。您可以通过以下方式查看:

//Checking user has tapped on Notification or not!

if let dicTemp = launchOptions?["UIApplicationLaunchOptionsRemoteNotificationKey"] {
    //Notification Key Found
}

对于swift3你可以通过以下方式查看:

if let dicTemp = launchOptions?[UIApplicationLaunchOptionsKey.localNotification] {
    //Notification Key Found
    print("Local Notification")
} else if let dicTemp = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] {
    print("Remote Notification")
}