菜鸟教程小白 发表于 2022-12-11 19:15:33

ios - 如何提示用户接受 AppDelegate 之外的推送通知?


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

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

<pre><code>import UIKit
import Firebase
import FirebaseMessaging
import UserNotifications

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

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: ?) -&gt; 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: , fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -&gt; Void) {

      if application.applicationState == UIApplicationState.active {

            var pushNotificationMessage = &#34;&#34;
            if let aps = userInfo[&#34;aps&#34;] as? NSDictionary {
                if let alert = aps[&#34;alert&#34;] as? NSDictionary {
                  if let message = alert[&#34;message&#34;] as? NSString {
                        pushNotificationMessage = message as String
                  }
                } else if let alert = aps[&#34;alert&#34;] as? NSString {
                  pushNotificationMessage = alert as String
                }
            }

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

            let defaultAction = UIAlertAction(title: &#34;OK&#34;, style: .default, handler: {
                (alert: UIAlertAction!) -&gt; Void in
            })
            defaultAction.setValue(Constants.activePushNotificationOKColor, forKey: &#34;titleTextColor&#34;)

            notificationAlert.addAction(defaultAction)
            self.window?.rootViewController?.present(notificationAlert, animated: true, completion: nil)
      }
    }
}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>您可以将代码移到需要授权的 <code>FIRApp.configure()</code> 之后;您可以使用 <code>UIApplication.shared</code></p> 获取应用程序的引用</p>
                                   
                                                <p style="font-size: 20px;">关于ios - 如何提示用户接受 AppDelegate 之外的推送通知?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/42815777/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/42815777/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 如何提示用户接受 AppDelegate 之外的推送通知?