如何在前台iOS Swift 3.0中接收推送通知?
答
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()
}
}
的可能的复制[注册以便在Xcode 8/3.0夫特推送通知?](https://stackoverflow.com/questions/37956482/registering-for-push-notifications-in-xcode-8-swift -3-0) – Hexfire
@Hexfire我想在iOS 8.0的前台状态中显示推送通知,即时消息接收后台状态的通知 – anuj
[在前台iOS中获取推送通知时应用程序可能重复](https://stackoverflow.com/问题/ 14872088/get-push-notification-while-app-in-foreground-ios) – Honey