每当通过推送通知打开应用程序时调用 rest-api

call rest-api whenever app is opened through a push notification

对于后端内的统计仪表板,我需要通过 alamofire 调用 rest-api,通知后端用户通过推送通知打开了应用程序。

我怎样才能做到这一点?

我处理过本地通知,所以我将举例说明我的工作。不知道推送通知的方式是否一样

我所做的是设置 appdelegate 以符合 User Notifications delegate

1: 导入

import UserNotifications

2: 添加协议

 class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate

3: 通知中心实例

var notificationCenter: UNUserNotificationCenter!

4:初始化并设置delegate

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

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

    return true  
}

5: NotificationCenter 响应

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

       print("notification pressed") 
}