IOS 在前台推送通知 Swift 4.2
IOS push notifications in foreground Swift 4.2
我正在开发 IOS 应用程序,我正在使用 Parse SDK 处理推送通知。当我的背部处于后台时,我能够收到通知。现在我想接收通知,即使我的应用程序在前台。我为它搜索了很多代码,但它不适合我当前的 AppDelegate class。
即使我的应用程序在前台,如何获取推送通知?
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Thread.sleep(forTimeInterval: 3.0)
// Override point for customization after application launch.
let configuration = ParseClientConfiguration {
[=12=].applicationId = "asdasdasd"
[=12=].clientKey = "asdasdasdas"
[=12=].server = "https://parseapi.back4app.com"
}
Parse.initialize(with: configuration)
UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) {
(granted, error) in
print("Permission granted: \(granted)")
guard granted else { return }
self.getNotificationSettings()
}
return true
}
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
{
completionHandler([.alert, .badge, .sound])
}
func getNotificationSettings() {
UNUserNotificationCenter.current().getNotificationSettings { (settings) in
print("Notification settings: \(settings)")
guard settings.authorizationStatus == .authorized else {
return
}
DispatchQueue.main.async(execute: {
UIApplication.shared.registerForRemoteNotifications()
})
}
}
func application(_ application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
createInstallationOnParse(deviceTokenData: deviceToken)
}
func applicationWillResignActive(_ application: UIApplication) {
}
func applicationDidEnterBackground(_ application: UIApplication) {
}
func applicationWillEnterForeground(_ application: UIApplication) {
}
func applicationDidBecomeActive(_ application: UIApplication) {
}
func applicationWillTerminate(_ application: UIApplication) {
}
func application(_ application: UIApplication,
didFailToRegisterForRemoteNotificationsWithError error: Error) {
print("Failed to register: \(error)")
}
}
}
我从同一个论坛找到了这个解决方案,但我无法将以下代码片段放入我当前的代码中:
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo
// Print full message.
print("tap on on forground app",userInfo)
completionHandler()
}
自 iOS10 起,苹果允许用户在前台处理推送通知显示。你可以在 willPresent
方法中处理它。并在 completionHandler 中处理推送通知,传递您想要的选项,即警报、徽章和声音。
确认您要使用 appDelegate UNUserNotificationCenterDelegate
将其放入您的 didFinishLaunchingWithOptions
方法中
UNUserNotificationCenter.current().delegate = self
之前
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) {
(granted, error) in
print("Permission granted: \(granted)")
guard granted else { return }
self.getNotificationSettings()
}
并添加此方法
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
{
completionHandler([.alert, .badge, .sound])
}
iOS 14 次更新
.alert 已经贬值,您现在应该使用 .banner,如下所示:
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
{
completionHandler([.banner, .badge, .sound])
}
我正在开发 IOS 应用程序,我正在使用 Parse SDK 处理推送通知。当我的背部处于后台时,我能够收到通知。现在我想接收通知,即使我的应用程序在前台。我为它搜索了很多代码,但它不适合我当前的 AppDelegate class。
即使我的应用程序在前台,如何获取推送通知?
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Thread.sleep(forTimeInterval: 3.0)
// Override point for customization after application launch.
let configuration = ParseClientConfiguration {
[=12=].applicationId = "asdasdasd"
[=12=].clientKey = "asdasdasdas"
[=12=].server = "https://parseapi.back4app.com"
}
Parse.initialize(with: configuration)
UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) {
(granted, error) in
print("Permission granted: \(granted)")
guard granted else { return }
self.getNotificationSettings()
}
return true
}
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
{
completionHandler([.alert, .badge, .sound])
}
func getNotificationSettings() {
UNUserNotificationCenter.current().getNotificationSettings { (settings) in
print("Notification settings: \(settings)")
guard settings.authorizationStatus == .authorized else {
return
}
DispatchQueue.main.async(execute: {
UIApplication.shared.registerForRemoteNotifications()
})
}
}
func application(_ application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
createInstallationOnParse(deviceTokenData: deviceToken)
}
func applicationWillResignActive(_ application: UIApplication) {
}
func applicationDidEnterBackground(_ application: UIApplication) {
}
func applicationWillEnterForeground(_ application: UIApplication) {
}
func applicationDidBecomeActive(_ application: UIApplication) {
}
func applicationWillTerminate(_ application: UIApplication) {
}
func application(_ application: UIApplication,
didFailToRegisterForRemoteNotificationsWithError error: Error) {
print("Failed to register: \(error)")
}
}
}
我从同一个论坛找到了这个解决方案,但我无法将以下代码片段放入我当前的代码中:
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo
// Print full message.
print("tap on on forground app",userInfo)
completionHandler()
}
自 iOS10 起,苹果允许用户在前台处理推送通知显示。你可以在 willPresent
方法中处理它。并在 completionHandler 中处理推送通知,传递您想要的选项,即警报、徽章和声音。
确认您要使用 appDelegate UNUserNotificationCenterDelegate
将其放入您的 didFinishLaunchingWithOptions
方法中
UNUserNotificationCenter.current().delegate = self
之前
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) {
(granted, error) in
print("Permission granted: \(granted)")
guard granted else { return }
self.getNotificationSettings()
}
并添加此方法
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
{
completionHandler([.alert, .badge, .sound])
}
iOS 14 次更新 .alert 已经贬值,您现在应该使用 .banner,如下所示:
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
{
completionHandler([.banner, .badge, .sound])
}