Ios 应用程序打开时如何获取推送通知?
我正在使用swift 4.2。当应用程序处于后台时,我会收到通知,但当应用程序处于活动状态时,我不会收到通知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
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
}
你为什么不明白?它只是没有显示出来。看你是最幸福的:)