Usm*_*han 0 foreground apple-push-notifications ios swift
我正在开发 IOS 应用程序,我正在使用 Parse SDK 处理推送通知。当我的背部在后台时,我能够收到通知。现在,即使我的应用程序在前台,我也想收到通知。我为它搜索了很多代码,但它不适合我当前的 AppDelegate 类。
即使我的应用程序在前台,如何获取推送通知?
@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 {
$0.applicationId = "asdasdasd"
$0.clientKey = "asdasdasdas"
$0.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)")
}
}
}
Run Code Online (Sandbox Code Playgroud)
我从同一个论坛找到了这个解决方案,但我无法将以下代码片段放在我当前的代码中:
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()
}
Run Code Online (Sandbox Code Playgroud)
从 iOS 10 开始,苹果允许用户在前台处理推送通知演示。你可以在willPresent方法中处理它。并在completionHandler 中处理推送通知,传递您想要的选项,即警报、徽章和声音。
确认您要使用 appDelegate UNUserNotificationCenterDelegate
把它放在你的didFinishLaunchingWithOptions方法中
UNUserNotificationCenter.current().delegate = self
Run Code Online (Sandbox Code Playgroud)
前
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) {
(granted, error) in
print("Permission granted: \(granted)")
guard granted else { return }
self.getNotificationSettings()
}
Run Code Online (Sandbox Code Playgroud)
并添加此方法
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
{
completionHandler([.alert, .badge, .sound])
}
Run Code Online (Sandbox Code Playgroud)
iOS 14 更新 .alert 已贬值,您现在应该使用 .banner,如下所示:
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
{
completionHandler([.banner, .badge, .sound])
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2439 次 |
| 最近记录: |