应用程序被杀后,Swift向服务器发送请求

时间:2018-05-11 08:52:03

标签: ios swift push-notification appdelegate

我想在应用程序进入onBackground或onAppWillTerminate模式时发出请求。我向AppDelegate方法添加了一个观察者,但没有工作。我只想发送请求并推送通知作为回复。我怎样才能做到这一点?我的要求不是新闻或音乐,简单的JSON。

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

    Fabric.with([Crashlytics.self])

    GADMobileAds.configure(withApplicationID: "ca-app-pub-8446699920682817~7829108")

   let onesignalInitSettings = [kOSSettingsKeyAutoPrompt: false]
    //ONE SIGNAL

    OneSignal.initWithLaunchOptions(launchOptions,
                                    appId: "f5854e88-0b58-495f-8d3f-e7899202d",
                                    handleNotificationAction: nil,
                                    settings: onesignalInitSettings)

    OneSignal.inFocusDisplayType = OSNotificationDisplayType.notification;
    OneSignal.promptForPushNotifications(userResponse: { accepted in
        print("User accepted notifications: \(accepted)")
    })


    UIApplication.shared.statusBarStyle = .lightContent



     UIApplication.shared.setMinimumBackgroundFetchInterval(UIApplicationBackgroundFetchIntervalMinimum)


    mS.StartTimer()

    //NotificationCenter.default.addObserver(self, selector:#selector(AppDelegate.applicationWillTerminate(_:)), name:NSNotification.Name.UIApplicationWillTerminate, object:nil)

    NotificationCenter.default.addObserver(self, selector:#selector(AppDelegate.applicationWillEnterForeground(_:)), name:NSNotification.Name.UIApplicationWillEnterForeground, object:nil)

    return true

}


func applicationWillTerminate(_ application: UIApplication) {

   //mS.StartTimer()

}
func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {

        self.mS.StartTimer()
    if let vc = window?.rootViewController as? LiveScores{
        vc.getLiveMatches(url : URL(string: "http://test.com/test/index.php/Service/lastLive"))
    }
    completionHandler(.newData)

}


func application(_ application: UIApplication,
                          didReceiveRemoteNotification userInfo: [AnyHashable : Any],
                          fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void){
    completionHandler(UIBackgroundFetchResult.newData)
}

func applicationDidEnterBackground(_ application: UIApplication) {
    if let vc = window?.rootViewController as? LiveScores{
        vc.getLiveMatches(url : URL(string: "http://opucukgonder.com/tipster/index.php/Service/lastLive"))
    }
    mS.StartTimer()

}

func applicationWillEnterForeground(_ application: UIApplication) {
    if let vc = window?.rootViewController as? LiveScores{
        vc.getLiveMatches(url : URL(string: "http://opucukgonder.com/tipster/index.php/Service/lastLive"))
    }
    mS.StartTimer()
}

}`

0 个答案:

没有答案