如何在前台iOS Swift 3.0中接收推送通知?

问题描述:

我想在前台状态显示推送通知iOS 8.0。我在后台状态接收通知,但未处于前台状态。如何在前台iOS Swift 3.0中接收推送通知?

+2

的可能的复制[注册以便在Xcode 8/3.0夫特推送通知?](https://*.com/questions/37956482/registering-for-push-notifications-in-xcode-8-swift -3-0) – Hexfire

+0

@Hexfire我想在iOS 8.0的前台状态中显示推送通知,即时消息接收后台状态的通知 – anuj

+0

[在前台iOS中获取推送通知时应用程序可能重复](https://*.com/问题/ 14872088/get-push-notification-while-app-in-foreground-ios) – Honey

extension AppDelegate: UNUserNotificationCenterDelegate { 

    // Receive displayed notifications for iOS 10 devices. 
    func userNotificationCenter(_ center: UNUserNotificationCenter, 
           willPresent notification: UNNotification, 
           withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { 

     completionHandler(UNNotificationPresentationOptions.alert) 

     //completionHandler([.alert,.badge]) 
     let userInfo = notification.request.content.userInfo 
     if let messageID = userInfo[gcmMessageIDKey] { 

     } 

     // Print full message. 
     print(userInfo) 
     completionHandler([]) 
    } 

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

     // Print message ID. 
     if let messageID = userInfo[gcmMessageIDKey] { 
      print("Message ID: \(messageID)") 
     } 

     // Print full message. 
     print(userInfo) 
     completionHandler() 
    } 
} 
+0

completionHandler(UNNotificationPresentationOptions.alert)是你需要的。 –

+0

这就是iOS10。问题是关于iOS 8.请参阅[这里](https://*.com/questions/14872088/get-push-notification-while-app-in-foreground-ios) – Honey

+0

@Honey你能告诉我我将如何按照我的代码执行 – anuj