2017-09-20 8 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/app-in-foreground-app-in-fore-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)が必要です。 iOS10用の –

+0

です質問はiOS 8に関するものです。[こちら](https://stackoverflow.com/questions/14872088/get-push-notification-while-app-in-foreground-ios) – Honey

+0

@Honeyは、私のコードに従ってください – anuj

関連する問題