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

时间:2017-03-15 16:40:32

标签: ios swift firebase push-notification firebase-cloud-messaging

我的应用加载到视图控制器中,告诉用户他们应该接受推送通知的原因。直到他们按下此视图控制器上的“继续”按钮后才会提示他们接受推送通知。这意味着我需要在初始视图控制器中的某处包含代码,让我们将其称为 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)
        }
    }
}

2 个答案:

答案 0 :(得分:1)

您可以在需要提出授权的FIRApp.configure()之后移动代码;您可以使用UIApplication.shared

获取应用程序的参考

答案 1 :(得分:0)

当您致电application.registerForRemoteNotifications()时,系统会显示有关推送通知的提示,因此,调用此功能时,您会在需要时显示提示。

要访问您的UIApplication,您可以在Swift3中使用UIApplication.shared或在Swift2中使用UIApplication.sharedApplication()