OGeek|极客世界-中国程序员成长平台

标题: ios - 如何提示用户接受 AppDelegate 之外的推送通知? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 19:15
标题: ios - 如何提示用户接受 AppDelegate 之外的推送通知?

我的应用加载到一个 View Controller 中,该 Controller 告诉用户为什么他们应该接受推送通知。直到他们按下此 View Controller 上的继续按钮后,我才希望提示他们接受推送通知。这意味着我需要在我的初始 View Controller 中的某处包含代码,让我们称之为 BeforePromptController。我正在使用 Firebase,以防万一。

这是我在 AppDelegate 中的工作代码(但是我想更改它,以便在用户按下 BeforePromptController 中的继续按钮后出现提示)。我试图在这里只包含 AppDelegate 的重要部分..

import UIKit
import Firebase
import FirebaseMessaging
import UserNotifications

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, FIRMessagingDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        FIRApp.configure()

        if #available(iOS 10.0, *) {
            let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
            UNUserNotificationCenter.current().requestAuthorization(
                options: authOptions,
                completionHandler: {_, _ in })

            // For iOS 10 display notification (sent via APNS)
            UNUserNotificationCenter.current().delegate = self
            // For iOS 10 data message (sent via FCM)
            FIRMessaging.messaging().remoteMessageDelegate = self

        } else {
            let settings: UIUserNotificationSettings =
                UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
            application.registerUserNotificationSettings(settings)
        }        

        application.registerForRemoteNotifications()

        return true
    }



    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {

        if application.applicationState == UIApplicationState.active {

            var pushNotificationMessage = ""
            if let aps = userInfo["aps"] as? NSDictionary {
                if let alert = aps["alert"] as? NSDictionary {
                    if let message = alert["message"] as? NSString {
                        pushNotificationMessage = message as String
                    }
                } else if let alert = aps["alert"] as? NSString {
                    pushNotificationMessage = alert as String
                }
            }

            let notificationAlert = UIAlertController(title: nil, message: pushNotificationMessage, preferredStyle: .alert)

            let defaultAction = UIAlertAction(title: "OK", style: .default, handler: {
                (alert: UIAlertAction!) -> Void in
            })
            defaultAction.setValue(Constants.activePushNotificationOKColor, forKey: "titleTextColor")

            notificationAlert.addAction(defaultAction)
            self.window?.rootViewController?.present(notificationAlert, animated: true, completion: nil)
        }
    }
}



Best Answer-推荐答案


您可以将代码移到需要授权的 FIRApp.configure() 之后;您可以使用 UIApplication.shared

获取应用程序的引用

关于ios - 如何提示用户接受 AppDelegate 之外的推送通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42815777/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4