2017-01-26 123 views
-1

對於我後臺內部的統計信息顯示板,我需要通過alamofire調用rest-api,通知後端用戶通過推送通知打開了應用程序。每當通過推送通知打開應用程序時調用rest-api

我該如何做到這一點?

+0

你嘗試用** ** applicationWillEnterForeground或 ** ** applicationDidBecomeActive ? –

+0

但我怎麼才能區分,如果應用程序是手動打開或通過推送通知點擊?如果推送有效,我需要在我的儀表板中看到它。 –

+0

「通過推送通知點擊」是什麼意思?請用 –

回答

1

我使用本地通知,所以我會給你一個關於我做什麼的例子。我不知道它是否與推送通知相同。

我做的是設置的appdelegate以符合用戶通知委託

1:導入

import UserNotifications 

2:添加協議

class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate 

3:通知中心實例

var notificationCenter: UNUserNotificationCenter! 

4:初始化並設置委託

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

    notificationCenter = UNUserNotificationCenter.current() 
    notificationCenter.delegate = self 

    return true 
} 

5:通知中心響應

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping() -> Void) { 

     print("notification pressed") 
} 
+0

你可以做同樣的事情(添加noti中心委託)在一個分離的類,但你需要保持它在appDelegate中實例化。希望能幫助到你! –

+0

很有貢獻,很詳細!但必須說,這隻適用於iOS 10或更高版本。 –

+0

Ohhh yes yes對不起:( –

相關問題