2017-09-20 157 views
2

我想在前臺狀態顯示推送通知iOS 8.0。我在後臺狀態接收通知,但未處於前臺狀態。如何在前臺iOS Swift 3.0中接收推送通知?

+2

的可能的複製[註冊以便在Xcode 8/3.0夫特推送通知?](https://stackoverflow.com/questions/37956482/registering-for-push-notifications-in-xcode-8-swift -3-0) – Hexfire

+0

@Hexfire我想在iOS 8.0的前臺狀態中顯示推送通知,即時消息接收後臺狀態的通知 – anuj

+0

[在前臺iOS中獲取推送通知時應用程序可能重複](https://stackoverflow.com/問題/ 14872088/get-push-notification-while-app-in-foreground-ios) – Honey

回答

1
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://stackoverflow.com/questions/14872088/get-push-notification-while-app-in-foreground-ios) – Honey

+0

@Honey你能告訴我我將如何按照我的代碼執行 – anuj