Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/100.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 应用程序打开时如何获取推送通知?_Ios_Swift_Push Notification_Unusernotificationcenter - Fatal编程技术网

Ios 应用程序打开时如何获取推送通知?

Ios 应用程序打开时如何获取推送通知?,ios,swift,push-notification,unusernotificationcenter,Ios,Swift,Push Notification,Unusernotificationcenter,我正在使用swift 4.2。当应用程序处于后台时,我会收到通知,但当应用程序处于活动状态时,我不会收到通知 func userNotificationCenter(center:unuservificationcenter,将显示通知:UNNotification,with completionHandler completionHandler:@escaping(UNNotificationPresentationOptions)->Void){ completionHandler([.al

我正在使用swift 4.2。当应用程序处于后台时,我会收到通知,但当应用程序处于活动状态时,我不会收到通知

func userNotificationCenter(center:unuservificationcenter,将显示通知:UNNotification,with completionHandler completionHandler:@escaping(UNNotificationPresentationOptions)->Void){
completionHandler([.alert、.badge、.sound])
}

我想知道当应用程序打开时如何获取通知

您需要在AppDelegate类中确认UnuseNotificationCenterDelegate

import UserNotifications

class AppDelegate : UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate

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

      UNUserNotificationCenter.current().delegate = self

   }
然后可以在ViewController类中调用此函数,不要忘记在ViewController中再次导入UserNotifications

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) 
{
    completionHandler([.alert, .badge, .sound])
}

用上述代码更新此代码

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler:
        @escaping (UNNotificationPresentationOptions) -> Void) {
        print("Handle push from foreground")
        print("\(notification.request.content.userInfo)")
        completionHandler([.alert, .badge, .sound])

        return
    }

你为什么不明白?它只是没有显示出来。看你是最幸福的:)