swift 如何检查打开iOS应用程序时是否有推送通知(以及该通知的数据)?

1mrurvl1  于 2022-10-31  发布在  Swift
关注(0)|答案(1)|浏览(258)

我找不到任何关于如何处理这个问题的文档,我使用firebase消息发送了一个推送通知,包含数据负载、标题和消息。通知很好地通过了,但是从后台打开应用程序并没有触发任何特定的选项:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {        FirebaseApp.configure();
    // Override point for customization after application launch.
    return true
}

函数似乎未被调用。

8yoxcaq7

8yoxcaq71#

这是按下推送通知时调用的函数

func userNotificationCenter(_ center: UNUserNotificationCenter,
                              didReceive response: UNNotificationResponse,
                              withCompletionHandler completionHandler: @escaping () -> Void) {
    let userInfo = response.notification.request.content.userInfo

    let application = UIApplication.shared

     if(application.applicationState == .active){
       //Prints the message when user tapped the notification bar when the app is in foreground
       print(userInfo)
     }

     if(application.applicationState == .inactive)
     {
       print("user tapped the notification bar when the app is in background")
     }

    completionHandler()
  }

相关问题